‘进行详细说明
Imports System.Data.SqlClient Imports System.Threading Module Program Sub Main() ’输入异步操作结果
Console.WriteLine("***** Fun with ASNYC Data Readers *****" & vbLf) ' Create and open a connection that is async-aware. '创建并打开一个异步连接
Dim cn As New SqlConnection() cn.ConnectionString = "Data Source=(local)" & _ ";Integrated Security=SSPI;" & _ "Initial Catalog=newdb;Asynchronous Processing=true" ‘ Asynchronous Processing=true 这个是必须的
cn.Open() ' Create a SQL command object that waits for approx 2 seconds. '两秒后执行SQL
Dim strSQL As String = "WaitFor Delay '00:00:02';Select * From tbdata" Dim myCommand As New SqlCommand(strSQL,cn) ' Execute the reader on a second thread. ' 定义异步线程并启用第二线程
Dim itfAsynch As IAsyncResult itfAsynch = myCommand.BeginExecuteReader(CommandBehavior.CloseConnection)
'当线程执行时,执行其它工作 ' Do something while other thread works. While Not itfAsynch.IsCompleted Console.WriteLine("Working on main thread...") 'Thread.Sleep(1000) End While Console.WriteLine() ’所有完成后,读出所有数据
' All done! Get reader and loop over results. Dim myDataReader As SqlDataReader = myCommand.EndExecuteReader(itfAsynch) While myDataReader.Read() Console.WriteLine("-> 序号: {0},临时号: {1},车船号: {2}.",_ myDataReader("Id").ToString().Trim(),_ myDataReader("序号").ToString().Trim(),_ myDataReader("车船号").ToString().Trim()) End While myDataReader.Close() Console.ReadLine() End Sub End Module