从ADO.NET 2.0开始,SqlCommand就新增了几个异步方法执行SQL命令。相对于同步执行方式,它使主线程不需要等待数据库的返回结果,在使用复杂性查询或 批量插入时将有效提高主线程的效率。使用异步SqlCommand的时候,请注意把ConnectionString 的 Asynchronous Processing 设置为 true 。
由于使用方式相似,此处就以 BeginExecuteNonQuery 为例子,介绍一下异步SqlCommand的使用。首先建立connectionString,注意把Asynchronous Processing设置为true来启动异步命令,然后把SqlCommand.CommandText设置为 WAITFOR DELAY "0:0:3" 来虚拟数据库操作。再通过BeginExecuteNonQuery启动异步操作,利用轮询方式监测操作情况。最后在操作完成后使用 EndExecuteNonQuery完成异步操作。
1 class Program
2 {
3 //把Asynchronous Processing设置为true
4 static string connectionString = "Data Source=LESLIE-PC;Initial Catalog=Business;“+
5 "Integrated Security=True;Asynchronous Processing=true";
6
7 static void Main(string[] args)
8 {
9 //把CLR线程池最大线程数设置为1000
10 ThreadPool.SetMaxThreads(1000, 1000);
11 ThreadPoolMessage("Start");
12
13 //使用WAITFOR DELAY命令来虚拟操作
14 SqlConnection connection = new SqlConnection(connectionString);
15 SqlCommand command = new SqlCommand("WAITFOR DELAY '0:0:3';", connection);
16 connection.Open();
17
18 //启动异步SqlCommand操作,利用轮询方式监测操作
19 IAsyncResult result = command.BeginExecuteNonQuery();
20 ThreadPoolMessage("BeginRead");
21 while (!result.AsyncWaitHandle.WaitOne(500))
22 Console.WriteLine("Main thread do work........");
23
24 //结束异步SqlCommand
25 int count= command.EndExecuteNonQuery(result);
26 ThreadPoolMessage("\nCompleted");
27 Console.ReadKey();
28 }
29
30 //显示线程池现状
31 static void ThreadPoolMessage(string data)
32 {
33 int a, b;
34 ThreadPool.GetAvailableThreads(out a, out b);
35 string message = string.Format("{0}\n CurrentThreadId is {1}\n "+
36 "WorkerThreads is:{2} CompletionPortThreads is :{3}\n",
37 data, Thread.CurrentThread.ManagedThreadId, a.ToString(), b.ToString());
38 Console.WriteLine(message);
39 }
40 }
class Program
{
//把Asynchronous Processing设置为true
static string connectionString = "Data Source=LESLIE-PC;Initial Catalog=Business;”+
“Integrated Security=True;Asynchronous Processing=true";
static void Main(string[] args)
{
//把CLR线程池最大线程数设置为1000
ThreadPool.SetMaxThreads(1000, 1000);
ThreadPoolMessage("Start");
//使用WAITFOR DELAY命令来虚拟操作
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("WAITFOR DELAY '0:0:3';", connection);
connection.Open();
//启动异步SqlCommand操作,并把SqlCommand对象传递到回调函数
IAsyncResult result = command.BeginExecuteNonQuery(
new AsyncCallback(AsyncCallbackMethod),command);
Console.ReadKey();
}
static void AsyncCallbackMethod(IAsyncResult result)
{
Thread.Sleep(200);
ThreadPoolMessage("AsyncCallback");
SqlCommand command = (SqlCommand)result.AsyncState;
int count=command.EndExecuteNonQuery(result);
command.Connection.Close();
}
//显示线程池现状
static void ThreadPoolMessage(string data)
{
int a, b;
ThreadPool.GetAvailableThreads(out a, out b);
string message = string.Format("{0}\n CurrentThreadId is {1}\n "+
"WorkerThreads is:{2} CompletionPortThreads is :{3}\n",
data, Thread.CurrentThread.ManagedThreadId, a.ToString(), b.ToString());
Console.WriteLine(message);
}
}
亿恩-天使(QQ:530997) 电话 037160135991 服务器租用,托管欢迎咨询。