本文整理汇总了C#中System.Data.SqlClient.SqlConnection.InsertAsync方法的典型用法代码示例。如果您正苦于以下问题:C# SqlConnection.InsertAsync方法的具体用法?C# SqlConnection.InsertAsync怎么用?C# SqlConnection.InsertAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.SqlConnection
的用法示例。
在下文中一共展示了SqlConnection.InsertAsync方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Process
private async Task Process(string fid, string com)
{
// NOTE: Throw is null, only for now (should log error later is dest and flight are both null)
if (string.IsNullOrWhiteSpace(fid))
{
throw new NotImplementedException();
}
int flightId;
int.TryParse(fid, out flightId);
if (flightId < 1)
{
throw new NotImplementedException();
}
decimal commission;
decimal.TryParse(com, out commission);
// Model
var conversion = new DatabaseConversion
{
AddedDate = DateTimeOffset.Now,
// TODO: Shit
Flight = flightId < 1 ? (int?)null : flightId,
Destination = null,
// TODO: Shit
Value = commission == decimal.Zero ? (decimal?)null : commission
};
using (var sql = new SqlConnection(_conn))
{
await sql.OpenAsync();//.ConfigureAwait(false);
var response = await sql.InsertAsync(conversion);//.ConfigureAwait(false);
if (response < 1)
{
// TODO: Log error
}
sql.Close();
}
// SignalR
// TODO: [Optimization] If there are no clients connected to SignalR, don't push event message.
// TODO: Refactor to own method (singleton or simpleinjector).
_conversionHubContext.Clients
.All
.newConversion(new EmberConversion
{
Id = conversion.Id,
AddedDate = conversion.AddedDate,
Flight = conversion.Flight,
Destination = conversion.Destination,
Value = conversion.Value
});
}
示例2: ProcessAdvertClick
// TODO: There is no sanitization on message data.
private async Task ProcessAdvertClick(Event evnt)
{
Trace.WriteLine("ProcessEvent");
int result;
// Database
// TODO: Refactor to own method.
// TODO: Automapper.
// TODO: SQL connection shouldn't be initialized this way.
using (var sql = new SqlConnection(_conn))
{
await sql.OpenAsync();//.ConfigureAwait(false);
// TODO: Sanitize this input (the default '0' for null isn't good)
// TODO: Should handle bad campaign or flight ids
// TODO: Parse methods will throw on error, convert to TryParse.
// TODO: Events should be isolated into it's own DB
result = await sql.InsertAsync(new DatabaseEvent
{
Time = evnt.Time.FromUnixTimeMilliseconds(),
UserId = Guid.Parse(evnt.UserId),
UserAgent = evnt.UserAgent,
UserLanguage = evnt.UserLanguage,
UserHostAddress = evnt.UserHostAddress == null ? null : IPAddress.Parse(evnt.UserHostAddress).GetAddressBytes(),
UserProxyAddress = evnt.UserProxyAddress == null ? null : IPAddress.Parse(evnt.UserProxyAddress).GetAddressBytes(),
Referer = evnt.Referer,
Flight = string.IsNullOrWhiteSpace(evnt.Flight) ? 0 : int.Parse(evnt.Flight),
Destination = string.IsNullOrWhiteSpace(evnt.Destination) ? 0 : int.Parse(evnt.Destination)
});//.ConfigureAwait(false);
sql.Close();
}
if (result < 1)
{
// TODO: Log error and raise hell
throw new NotImplementedException();
}
// SignalR
// TODO: [Optimization] If there are no clients connected to SignalR, don't push event message.
// TODO: Refactor to own method (singleton or simpleinjector).
// TODO: Automapper(?).
// BUG: Needs to send click cost (should cache flights in memory and pull click cost [for speed])
Trace.WriteLine("Sending _eventHubContext");
_eventHubContext.Clients
.All
.newEvent(new EmberEvent
{
Id = result,
Time = evnt.Time.FromUnixTimeMilliseconds(),
UserId = evnt.UserId,
UserAgent = evnt.UserAgent,
UserLanguage = evnt.UserLanguage,
UserHostAddress = evnt.UserHostAddress,
UserProxyAddress = evnt.UserProxyAddress,
// TODO: Fix this, should pull from the Flight
UserClickCost = 0.25M,
Referer = evnt.Referer,
Flight = evnt.Flight,
Destination = evnt.Destination
});
Trace.WriteLine("Sent _eventHubContext");
}
示例3: 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."};
}