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 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|