vb.net数据库异步操作(二)
发布时间:2020-12-16 22:49:02 所属栏目:大数据 来源:网络整理
导读:‘进行详细说明 Imports System.Data.SqlClientImports System.ThreadingModule Program Sub Main() ’输入异步操作结果 Console.WriteLine("***** Fun with ASNYC Data Readers *****" vbLf) ' Create and open a connection that is async-aware. '创建并
‘进行详细说明 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |