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


C# SqlConnection.ExecuteScalarAsync方法代码示例

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


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

示例1: GetRandomWodIdAsync

 public async Task<long> GetRandomWodIdAsync()
 {
     using (var conn = new SqlConnection(AppConfig.ConnectionString))
     {
         return await conn.ExecuteScalarAsync<long>("SELECT TOP 1 [Id] FROM [Wod] ORDER BY NEWID()");
     }
 }
开发者ID:APErebus,项目名称:wodbot,代码行数:7,代码来源:Repository.cs

示例2: CreateAlbum

 public async Task<long> CreateAlbum(NewAlbum album)
 {
     using (var connection = new SqlConnection(_connectionString))
     {
         var cmd = new CommandDefinition(SqlStatements.InsertAlbum, new { artistId = album.ArtistId, albumArtId = album.AlbumArtId, name = album.Name, releaseDate = album.ReleaseDate });
         await connection.OpenAsync();
         var albumArtId = await connection.ExecuteScalarAsync<long>(cmd);
         return albumArtId;
     }
 }
开发者ID:webley,项目名称:album-tracker,代码行数:10,代码来源:AlbumDataAccess.cs

示例3: CreateArtist

        /// <summary>
        /// Insert a new artist in the database, or return its ID if it already exists.
        /// </summary>
        /// <param name="artist">The artist to insert.</param>
        /// <returns>The ID of the new artist inserted, or the existing ID if it is a duplicate.</returns>
        public async Task<long> CreateArtist(NewArtist artist)
        {
            //var existingArtist = await GetArtistByName(artist.Name);
            //if (existingArtist != null)
            //{
            //    return existingArtist.Id;
            //}

            using (var connection = new SqlConnection(_connectionString))
            {
                var cmd = new CommandDefinition(SqlStatements.InsertArtist, new { name = artist.Name, country = artist.Country });
                await connection.OpenAsync();
                var artistId = await connection.ExecuteScalarAsync<long>(cmd);
                return artistId;
            }
        }
开发者ID:webley,项目名称:album-tracker,代码行数:21,代码来源:ArtistDataAccess.cs

示例4: CreateAlbumTrack

 public async Task<long> CreateAlbumTrack(NewAlbumTrack albumTrack)
 {
     using (var connection = new SqlConnection(_connectionString))
     {
         var cmd = new CommandDefinition(SqlStatements.InsertAlbumTrack, new
         {
             albumId = albumTrack.AlbumId,
             trackNumber = albumTrack.TrackNumber,
             name = albumTrack.Name,
             durationMs = albumTrack.LengthInMs
         });
         await connection.OpenAsync();
         var trackId = await connection.ExecuteScalarAsync<long>(cmd);
         return trackId;
     }
 }
开发者ID:webley,项目名称:album-tracker,代码行数:16,代码来源:AlbumTrackDataAccess.cs

示例5: AppendStream

        public async Task AppendStream(string streamId, int expectedVersion, IEnumerable<StreamEvent> streamEvents)
        {
            Guard.NullOrWhiteSpace(() => streamId);
            Guard.NullOrDefault(() => streamEvents);

            await _log.Debug("Appending stream {@streamId} with {@events}", streamId, streamEvents.ToArray());

            // create DataTable to send as a TVP
            var newStreamEventsTable = new DataTable();

            newStreamEventsTable.Columns.Add("Id", typeof(Guid));
            newStreamEventsTable.Columns.Add("Type", typeof(string));
            newStreamEventsTable.Columns.Add("Data", typeof(string));
            newStreamEventsTable.Columns.Add("Metadata", typeof(string));
            newStreamEventsTable.Columns.Add("StreamVersion", typeof(int));

            var eventVersion     = expectedVersion;
            var fallbackCommitId = Guid.NewGuid();

            newStreamEventsTable.BeginLoadData();

            foreach(var se in streamEvents) {
                se.EnrichMetadata(streamId, eventVersion++, fallbackCommitId);

                newStreamEventsTable.Rows.Add(
                    se.Metadata[EventMetadataKeys.Id],
                    se.Event.GetType().Name,
                    _serializer.Serialize(se.Event),
                    _serializer.Serialize(se.Metadata),
                    eventVersion);
            }

            newStreamEventsTable.EndLoadData();

            // create parameters
            var parameters = new DynamicParameters();

            parameters.AddDynamicParams(new {
                StreamId              = streamId,
                ExpectedStreamVersion = expectedVersion,
                StreamEvents          = newStreamEventsTable.AsTableValuedParameter("StreamEvents")
            });

            int actualVersion;

            // execute operation
            using(var connection = new SqlConnection(_settings.ConnectionString)) {
                actualVersion = await connection
                    .ExecuteScalarAsync<int>(
                        sql        : "AppendStream", 
                        param      : parameters, 
                        commandType: CommandType.StoredProcedure)
                    .ConfigureAwait(false);
            }

            // if the actual version is different from the expected version
            if(actualVersion != eventVersion) {
                throw new StreamConcurrencyException(streamId, expectedVersion, actualVersion);
            }

            await _log.Information("Events appended to stream {@streamId}", streamId);

            // dispatch events
            await _dispatcher.DispatchStreamEvents(streamEvents);
        }
开发者ID:Aenima-Framework,项目名称:Aenima,代码行数:65,代码来源:DapperEventStore.cs

示例6: CreateUserAsync

        public async Task<CreateUserResult> CreateUserAsync(ApplicationUser user)
        {
            int? id = 0;
            string error = null;

            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                int existingId = await connection.ExecuteScalarAsync<int>("SELECT COUNT(*) FROM dbo.Users WHERE [email protected]", new {Email = user.Email});
                if (existingId == 0)
                {
                    id = await connection.InsertAsync(user);
                }
                else
                {
                    error = "Email address already exists.";
                }
            }

            if (id > 0)
            {
                return new CreateUserResult {IsSuccess = true};
            }

            return new CreateUserResult {IsSuccess = false, ErrorMessage = error ?? "Error inserting user."};
        }
开发者ID:ryanrodemoyer,项目名称:BackendTemplate,代码行数:25,代码来源:UsersController.cs


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