当前位置: 首页>>代码示例>>C#>>正文


C# SqlConnection.QueryAsync方法代码示例

本文整理汇总了C#中System.Data.SqlClient.SqlConnection.QueryAsync方法的典型用法代码示例。如果您正苦于以下问题:C# SqlConnection.QueryAsync方法的具体用法?C# SqlConnection.QueryAsync怎么用?C# SqlConnection.QueryAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Data.SqlClient.SqlConnection的用法示例。


在下文中一共展示了SqlConnection.QueryAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetAlbum

        public async Task<Album> GetAlbum(long albumId)
        {
            Album albumAggregate;

            using(var connection = new SqlConnection(_connectionString))
            {
                var cmd = new CommandDefinition(SqlStatements.SelectAlbumAggregate, new { albumId = albumId });
                await connection.OpenAsync();
                var albums = await connection.QueryAsync<Album, Artist, AlbumArt, Album>(cmd, (album, artist, art) => { album.Artist = artist; album.AlbumArt = art; return album; });
                albumAggregate = albums.FirstOrDefault();

                if (albumAggregate == null)
                    return null;

                cmd = new CommandDefinition(SqlStatements.SelectAlbumTracksByAlbum, new { albumId = albumId });
                var tracks = await connection.QueryAsync<AlbumTrack>(cmd);
                var trackList = tracks.OrderBy(track => track.TrackNumber)
                    .Select(track => new Track { Id = track.Id, Name = track.Name, DuationMs = track.DurationMs })
                    .ToList();

                albumAggregate.TrackList = trackList;
            }

            return albumAggregate;
        }
开发者ID:webley,项目名称:album-tracker,代码行数:25,代码来源:AlbumDataAccess.cs

示例2: GetAllAlbumsDapperAsync

        public async Task<IEnumerable<Album>> GetAllAlbumsDapperAsync()
        {
            List<Album> albums;
            using (var con = new SqlConnection(this.connectionString))
            {
                con.Open();
                var watch = new Stopwatch();
                watch.Start();

                var result = await con.QueryAsync<Album, Genre, Artist, Album>(
                    "[dbo].[HoleAlleAlben]",
                    (album, genre, artist) =>
                    {
                        album.Genre = genre;
                        album.Artist = artist;
                        return album;
                    },
                    commandType: CommandType.StoredProcedure,
                    splitOn: "GenreId, ArtistId");
                albums = result.ToList();
                watch.Stop();

                this.logger.LogInformation("SQL query {0} was done in {1} ms.", new object[] { "[dbo].[HoleAlleAlben] Dapper.net Async", watch.ElapsedMilliseconds});
                con.Close();
            }

            return albums;
        }
开发者ID:JensKlambauer,项目名称:MVC6_Testprojects,代码行数:28,代码来源:StoreRepository.cs

示例3: GetAll

 public async Task<IEnumerable<ProposalReadModel>> GetAll()
 {
     using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString))
     {
         return await connection.QueryAsync<ProposalReadModel>("select Id, Status, CreatedBy, CreatedOn, UpdatedOn from Proposals;");
     }
 }
开发者ID:kutynko,项目名称:contingent,代码行数:7,代码来源:ProposalsRepository.cs

示例4: GetLogins

 public async Task<IEnumerable<Login>> GetLogins()
 {
     IEnumerable<Login> logins;
     using (var conn = new SqlConnection(_connectionString))
     {
         logins = await conn.QueryAsync<Login>("select * from dbo.Login");
     }
     return logins;
 }
开发者ID:SaberZA,项目名称:turbulent-garbanzo,代码行数:9,代码来源:LoginRepository.cs

示例5: GetNoteByID

        public async Task<Note> GetNoteByID( int id ) {
            using ( SqlConnection conn = new SqlConnection( connstring ) ) {
                string query = "select n.NoteID, n.NoteTypeID, s.SubName, n.Submitter, n.Message, n.AppliesToUsername, n.Url, n.Timestamp, n.ParentSubreddit "
                        + " from Notes n inner join Subreddits s on s.SubredditID = n.SubredditID "
                        + " where n.NoteID = @noteid";

                return ( await conn.QueryAsync<Note>( query, new { noteid = id } ) ).Single();
            }
        }
开发者ID:CrustyJew,项目名称:SnooNotes,代码行数:9,代码来源:NotesDAL.cs

示例6: GetNoteTypesForSubs

        public async Task<IEnumerable<NoteType>> GetNoteTypesForSubs( IEnumerable<string> subredditNames ) {
            using ( SqlConnection con = new SqlConnection( constring ) ) {
                string query = "select nt.NoteTypeID,s.SubName,nt.DisplayName,nt.ColorCode,nt.DisplayOrder,nt.Bold,nt.Italic from NoteTypes nt "
                        + " inner join Subreddits s on s.SubredditID = nt.SubredditID"
                        + " where s.SubName in @subs and nt.Disabled = 0";

                return await con.QueryAsync<NoteType>( query, new { subs = subredditNames } );
            }
        }
开发者ID:CrustyJew,项目名称:SnooNotes,代码行数:9,代码来源:NoteTypesDAL.cs

示例7: GetUploads

 public async Task<IEnumerable<UploadDto>> GetUploads(int parentId)
 {
     var q = @" SELECT * FROM  [dbo].[Upload] WHERE [email protected]";
     using (var con = new SqlConnection(ConnectionString))
     {
         con.Open();
         return await con.QueryAsync<UploadDto>(q,new { parentId });
     }
 }
开发者ID:kshyju,项目名称:ProjectPlanningTool,代码行数:9,代码来源:IUploadRepository.cs

示例8: UserHasNotes

 public async Task<bool> UserHasNotes(IEnumerable<string> subnames, string username ) {
     using ( SqlConnection conn = new SqlConnection( connstring ) ) {
         string query = "select count(*) "
                + " from Notes n inner join Subreddits s on s.SubredditID = n.SubredditID "
                + " where s.SubName in @subnames and n.AppliesToUsername = @username";
         int count = (await conn.QueryAsync<int>( query, new { subnames, username } )).FirstOrDefault();
         return count > 0;
     }
 }
开发者ID:CrustyJew,项目名称:SnooNotes,代码行数:9,代码来源:NotesDAL.cs

示例9: GetNotesForSubs

        public async Task<IEnumerable<Note>> GetNotesForSubs( IEnumerable<string> subnames, IEnumerable<string> users ) {
            using ( SqlConnection conn = new SqlConnection( connstring ) ) {
                string query = "select n.NoteID, n.NoteTypeID, s.SubName, n.Submitter, n.Message, n.AppliesToUsername, n.Url, n.Timestamp, n.ParentSubreddit "
                        + " from Notes n inner join Subreddits s on s.SubredditID = n.SubredditID "
                        + " where s.SubName in @subnames and n.AppliesToUsername in @users";

                return await conn.QueryAsync<Note>( query, new { subnames, users } );
            }
        }
开发者ID:CrustyJew,项目名称:SnooNotes,代码行数:9,代码来源:NotesDAL.cs

示例10: AllAsync

 public async Task<IEnumerable<TestData>> AllAsync()
 {
     using (SqlConnection connection = new SqlConnection(AppConfiguration.ConnectionString))
     {
         connection.Open();
         IEnumerable<TestData> result = await connection.QueryAsync<TestData>(getAllCommandText);
         return result;
     }
 }
开发者ID:rhwy,项目名称:AspNetSiteTest,代码行数:9,代码来源:DapperTestRepo.cs

示例11: GetUsersWithNotes

        public async Task<IEnumerable<string>> GetUsersWithNotes( IEnumerable<string> subnames ) {
            using ( SqlConnection conn = new SqlConnection( connstring ) ) {
                string query = "select distinct n.AppliesToUsername "
                       + " from Notes n inner join Subreddits s on s.SubredditID = n.SubredditID "
                       + " where s.SubName in @subnames";

                return await conn.QueryAsync<string>( query, new { subnames } );
            }
        }
开发者ID:CrustyJew,项目名称:SnooNotes,代码行数:9,代码来源:NotesDAL.cs

示例12: QueryDatabase

 static async Task<IEnumerable<dynamic>> QueryDatabase()
 {
     using (var connection = new SqlConnection("Data Source=localhost;Initial Catalog=Test;User ID=sa;Password=password;"))
     {
         await connection.OpenAsync();
         var data = await connection.QueryAsync<dynamic>("select * from dbo.DataSet");
         return data;
     }
 }
开发者ID:vgrigoriu,项目名称:ServiceStackTests,代码行数:9,代码来源:Program.cs

示例13: GetNoteType

 public async Task<NoteType> GetNoteType( int id ) {
     using ( SqlConnection con = new SqlConnection( constring ) ) {
         string query = "select nt.NoteTypeID,s.SubName,nt.DisplayName,nt.ColorCode,nt.DisplayOrder,nt.Bold,nt.Italic from NoteTypes nt "
                 + " inner join Subreddits s on s.SubredditID = nt.SubredditID"
                 + " where NoteTypeID = @id";
         NoteType ntype = ( await con.QueryAsync<NoteType>( query, new { @id } ) ).First();
         return ntype;
     }
 }
开发者ID:CrustyJew,项目名称:SnooNotes,代码行数:9,代码来源:NoteTypesDAL.cs

示例14: GetAsync

        public async Task<Blog> GetAsync()
        {
            var sql = $"SELECT * FROM [Blog];";

            using(var connection = new SqlConnection(ConnectionString))
            {
                var objs = await connection.QueryAsync<Blog>(sql);
                return objs.FirstOrDefault();
            }
        }
开发者ID:kspearrin,项目名称:NPress,代码行数:10,代码来源:BlogRepository.cs

示例15: GetPriorities

        public async Task<IEnumerable<NameValueItem>> GetPriorities()
        {
            using (var con = new SqlConnection(ConnectionString))
            {
                con.Open();
                var projects = await con.QueryAsync<NameValueItem>("SELECT * from Priority  WITH (NOLOCK)");
                return projects;
            }

        }
开发者ID:kshyju,项目名称:ProjectPlanningTool,代码行数:10,代码来源:IIssueRepository.cs


注:本文中的System.Data.SqlClient.SqlConnection.QueryAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。