本文整理汇总了C#中SqlCommand.ExecuteNonQueryAsync方法的典型用法代码示例。如果您正苦于以下问题:C# SqlCommand.ExecuteNonQueryAsync方法的具体用法?C# SqlCommand.ExecuteNonQueryAsync怎么用?C# SqlCommand.ExecuteNonQueryAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SqlCommand
的用法示例。
在下文中一共展示了SqlCommand.ExecuteNonQueryAsync方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FailureTest
public static void FailureTest()
{
string connStr = DataTestClass.SQL2005_Northwind;
bool failure = false;
bool taskCompleted = false;
SqlCommand com = new SqlCommand("select * from Orders");
SqlConnection con = new SqlConnection(connStr + ";pooling=false");
com.Connection = con;
con.Open();
Task<int> nonQueryTask = com.ExecuteNonQueryAsync();
try
{
com.ExecuteNonQueryAsync().Wait(TaskTimeout);
}
catch (AggregateException agrEx)
{
agrEx.Handle(
(ex) =>
{
Assert.True(ex is InvalidOperationException, "FAILED: Thrown exception for ExecuteNonQueryAsync was not an InvalidOperationException");
failure = true;
return true;
});
}
Assert.True(failure, "FAILED: No exception thrown after trying second ExecuteNonQueryAsync.");
failure = false;
taskCompleted = nonQueryTask.Wait(TaskTimeout);
Assert.True(taskCompleted, "FAILED: ExecuteNonQueryAsync Task did not complete successfully.");
Task<SqlDataReader> readerTask = com.ExecuteReaderAsync();
try
{
com.ExecuteReaderAsync().Wait(TaskTimeout);
}
catch (AggregateException agrEx)
{
agrEx.Handle(
(ex) =>
{
Assert.True(ex is InvalidOperationException, "FAILED: Thrown exception for ExecuteReaderAsync was not an InvalidOperationException: " + ex);
failure = true;
return true;
});
}
Assert.True(failure, "FAILED: No exception thrown after trying second ExecuteReaderAsync.");
taskCompleted = readerTask.Wait(TaskTimeout);
Assert.True(taskCompleted, "FAILED: ExecuteReaderAsync Task did not complete successfully.");
readerTask.Result.Dispose();
con.Close();
}
示例2: SendMessage
public static async Task SendMessage(string connectionString, string queue, string messageBody, Dictionary<string, string> headers)
{
var insertSql = [email protected]"INSERT INTO [{queue}] (
[Id],
[Recoverable],
[Headers],
[Body])
VALUES (
@Id,
@Recoverable,
@Headers,
@Body)";
using (var scope = new TransactionScope())
{
using (var connection = new SqlConnection(connectionString))
{
await connection.OpenAsync()
.ConfigureAwait(false);
using (var command = new SqlCommand(insertSql, connection))
{
var parameters = command.Parameters;
command.CommandType = CommandType.Text;
parameters.Add("Id", SqlDbType.UniqueIdentifier).Value = Guid.NewGuid();
var serializeHeaders = Newtonsoft.Json.JsonConvert.SerializeObject(headers);
parameters.Add("Headers", SqlDbType.VarChar).Value = serializeHeaders;
parameters.Add("Body", SqlDbType.VarBinary).Value = Encoding.UTF8.GetBytes(messageBody);
parameters.Add("Recoverable", SqlDbType.Bit).Value = true;
await command.ExecuteNonQueryAsync()
.ConfigureAwait(false);
}
}
scope.Complete();
}
}
示例3: PlaceOrder
static async Task PlaceOrder()
{
#region MessagePayload
var message = @"{
$type: 'PlaceOrder',
OrderId: 'Order from ADO.net sender'
}";
#endregion
#region SendingUsingAdoNet
var connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=samples;Integrated Security=True";
using (var connection = new SqlConnection(connectionString))
{
await connection.OpenAsync()
.ConfigureAwait(false);
var insertSql = "INSERT INTO [Samples.SqlServer.NativeIntegration] ([Id],[Recoverable],[Headers],[Body]) VALUES (@Id,@Recoverable,@Headers,@Body)";
using (var command = new SqlCommand(insertSql, connection))
{
command.CommandType = CommandType.Text;
command.Parameters.Add("Id", SqlDbType.UniqueIdentifier).Value = Guid.NewGuid();
command.Parameters.Add("Headers", SqlDbType.VarChar).Value = "";
command.Parameters.Add("Body", SqlDbType.VarBinary).Value = Encoding.UTF8.GetBytes(message);
command.Parameters.Add("Recoverable", SqlDbType.Bit).Value = true;
await command.ExecuteNonQueryAsync()
.ConfigureAwait(false);
}
}
#endregion
}
示例4: DeleteQueue
public static async Task DeleteQueue(SqlConnection connection, string schema, string queueName)
{
var sql = @"
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[{0}].[{1}]') AND type in (N'U'))
DROP TABLE [{0}].[{1}]";
var deleteScript = string.Format(sql, schema, queueName);
using (var command = new SqlCommand(deleteScript, connection))
{
await command.ExecuteNonQueryAsync()
.ConfigureAwait(false);
}
}
示例5: ExecuteNonQuery
async Task<object> ExecuteNonQuery(string connectionString, string commandString, IDictionary<string, object> parameters)
{
using (var connection = new SqlConnection(connectionString))
{
using (var command = new SqlCommand(commandString, connection))
{
this.AddParamaters(command, parameters);
await connection.OpenAsync();
return await command.ExecuteNonQueryAsync();
}
}
}
示例6: CreateQueue
public static async Task CreateQueue(SqlConnection connection, string schema, string queueName)
{
var createQueueScript =
@"IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[{0}].[{1}]') AND type in (N'U'))
BEGIN
CREATE TABLE [{0}].[{1}](
[Id] [uniqueidentifier] NOT NULL,
[CorrelationId] [varchar](255),
[ReplyToAddress] [varchar](255),
[Recoverable] [bit] NOT NULL,
[Expires] [datetime],
[Headers] [varchar](max) NOT NULL,
[Body] [varbinary](max),
[RowVersion] [bigint] IDENTITY(1,1) NOT NULL
) ON [PRIMARY];
CREATE CLUSTERED INDEX [Index_RowVersion] ON [{0}].[{1}]
(
[RowVersion] ASC
) ON [PRIMARY]
END";
var sql = string.Format(createQueueScript, schema, queueName);
using (var command = new SqlCommand(sql, connection))
{
await command.ExecuteNonQueryAsync()
.ConfigureAwait(false);
}
}