Imports System.Data.SqlClient
Imports System.Threading
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim theConnectionString As String
theConnectionString = "Data Source=WANGLI;Initial Catalog=PlcData;Persist Security Info=True;User ID=sa;pwd =sa;Asynchronous Processing=true"
'这个必要加的Asynchronous Processing=true
Dim theConnection As SqlConnection = New SqlConnection(theConnectionString)
Dim theCommand As SqlClient.SqlCommand
theCommand = New SqlCommand("select * from tbdata", theConnection)
theConnection.Open()
Console.WriteLine("connection open and asynchronour processing starting.")
theCommand.BeginExecuteReader(New AsyncCallback(AddressOf DataReady), theCommand)
End Sub
‘要执行的异步处理过程
Private Sub DataReady(ByVal asyncResult As IAsyncResult)
Dim theCommand As SqlCommand = asyncResult.AsyncState
Dim Dr As SqlDataReader
Dr = theCommand.EndExecuteReader(asyncResult)
While Dr.Read
Console.WriteLine(Dr.GetString(2))
End While
theCommand.Connection.Close()
Console.WriteLine("数据处理结束....")
End Sub
vb.net数据库异步操作(二)