本文整理汇总了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()");
}
}
示例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;
}
}
示例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;
}
}
示例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;
}
}
示例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);
}
示例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."};
}