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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值