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数据库异步操作(二)