本文整理汇总了C#中System.Data.SqlClient.SqlConnectionStringBuilder.AsynchronousProcessing属性的典型用法代码示例。如果您正苦于以下问题:C# SqlConnectionStringBuilder.AsynchronousProcessing属性的具体用法?C# SqlConnectionStringBuilder.AsynchronousProcessing怎么用?C# SqlConnectionStringBuilder.AsynchronousProcessing使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Data.SqlClient.SqlConnectionStringBuilder
的用法示例。
在下文中一共展示了SqlConnectionStringBuilder.AsynchronousProcessing属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System.Data.SqlClient;
using System.Threading;
class Program
{
static void Main()
{
// Create a SqlConnectionStringBuilder instance,
// and ensure that it is set up for asynchronous processing.
SqlConnectionStringBuilder builder =
new SqlConnectionStringBuilder(GetConnectionString());
// Asynchronous method calls won't work unless you
// have added this option, or have added
// the clause "Asynchronous Processing=true"
// to the connection string.
builder.AsynchronousProcessing = true;
string commandText =
"UPDATE Production.Product SET ReorderPoint = ReorderPoint + 1 " +
"WHERE ReorderPoint IS NOT Null;" +
"WAITFOR DELAY '0:0:3';" +
"UPDATE Production.Product SET ReorderPoint = ReorderPoint - 1 " +
"WHERE ReorderPoint IS NOT Null";
RunCommandAsynchronously(commandText, builder.ConnectionString);
Console.WriteLine("Press any key to finish.");
Console.ReadLine();
}
private static string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
return "Data Source=(local);Integrated Security=SSPI;" +
"Initial Catalog=AdventureWorks";
}
private static void RunCommandAsynchronously(string commandText,
string connectionString)
{
// Given command text and connection string, asynchronously execute
// the specified command against the connection. For this example,
// the code displays an indicator as it's working, verifying the
// asynchronous behavior.
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
int count = 0;
SqlCommand command = new SqlCommand(commandText, connection);
connection.Open();
IAsyncResult result = command.BeginExecuteNonQuery();
while (!result.IsCompleted)
{
Console.WriteLine("Waiting {0}.", count);
// Wait for 1/10 second, so the counter
// doesn't consume all available resources
// on the main thread.
Thread.Sleep(100);
count += 1;
}
Console.WriteLine("Command complete. Affected {0} rows.",
command.EndExecuteNonQuery(result));
}
catch (SqlException ex)
{
Console.WriteLine(
"Error {0}: System.Data.SqlClient.SqlConnectionStringBuilder",
ex.Number, ex.Message);
}
catch (InvalidOperationException ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
catch (Exception ex)
{
// You might want to pass these errors
// back out to the caller.
Console.WriteLine("Error: {0}", ex.Message);
}
}
}
}
开发者ID:.NET开发者,项目名称:System.Data.SqlClient,代码行数:84,代码来源:SqlConnectionStringBuilder.AsynchronousProcessing