Imports System.Data.sqlClient
Imports System.Threading
Public Class FrmMain
Private Sub btnLoad_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnLoad.Click
Dim cn As New sqlConnection’定义连结字符串,并且要注意加ASYNCHRONOUS PROCESSING=TRUE
cn.ConnectionString = "Data Source=WANGLI;Initial Catalog=PlcData;Persist Security Info=True;User ID=sa;pwd=sa;Asynchronous processing=true"
cn.Open()
Dim strsql As String = "select * from tbdata"
Dim mycommand As New sqlCommand(strsql,cn)
Dim itfAsynch As IAsyncResult ’定义一个异步读取变量
itfAsynch = mycommand.BeginExecuteReader(CommandBehavior.CloseConnection) ’执行异步读写
While Not itfAsynch.IsCompleted’ 等待执行,
Console.WriteLine("Working on main thread...")
Thread.Sleep(1000)
End While
Console.WriteLine()
Dim dt As New DataTable ’执行完结后将数据写入datatable中
Dim MydataReader As sqlDataReader = mycommand.EndExecuteReader(itfAsynch)
'While MydataReader.Read
' Console.WriteLine("序号:" & MydataReader(0).ToString)
'End While
dt.Load(MydataReader)
DataGridView1.DataSource = dt
MydataReader.Close()
Console.ReadLine()
End Sub End Class
原文链接:https://www.f2er.com/vb/261033.html