VB.NET 异步读取数据方法之二

前端之家收集整理的这篇文章主要介绍了VB.NET 异步读取数据方法之二前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

猜你在找的VB相关文章