前端之家收集整理的这篇文章主要介绍了
vb.net数据库异步操作(二),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
‘进行详细说明
Imports System.Data.sqlClient
Imports System.Threading
Module Program
Sub Main()
’输入异步操作结果
Console.WriteLine("***** Fun with ASNYC Data Readers *****" & vbLf)
' Create and open a connection that is async-aware.
'创建并打开一个异步连接
Dim cn As New sqlConnection()
cn.ConnectionString = "Data Source=(local)" & _
";Integrated Security=SSPI;" & _
"Initial Catalog=newdb;Asynchronous Processing=true"
‘ Asynchronous Processing=true 这个是必须的
cn.Open()
' Create a sql command object that waits for approx 2 seconds.
'两秒后执行sql
Dim strsql As String = "WaitFor Delay '00:00:02';Select * From tbdata"
Dim myCommand As New sqlCommand(strsql,cn)
' Execute the reader on a second thread.
' 定义异步线程并启用第二线程
Dim itfAsynch As IAsyncResult
itfAsynch = myCommand.BeginExecuteReader(CommandBehavior.CloseConnection)
'当线程执行时,执行其它工作
' Do something while other thread works.
While Not itfAsynch.IsCompleted
Console.WriteLine("Working on main thread...")
'Thread.Sleep(1000)
End While
Console.WriteLine()
’所有完成后,读出所有数据
' All done! Get reader and loop over results.
Dim myDataReader As sqlDataReader = myCommand.EndExecuteReader(itfAsynch)
While myDataReader.Read()
Console.WriteLine("-> 序号: {0},临时号: {1},车船号: {2}.",_
myDataReader("Id").ToString().Trim(),_
myDataReader("序号").ToString().Trim(),_
myDataReader("车船号").ToString().Trim())
End While
myDataReader.Close()
Console.ReadLine()
End Sub
End Module