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


相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...