微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐