vb.net数据库异步操作(三)

前端之家收集整理的这篇文章主要介绍了vb.net数据库异步操作(三)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Imports System.Data.sqlClient
Imports System.Threading
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
        Dim theConnectionString As String
        theConnectionString = "Data Source=WANGLI;Initial Catalog=PlcData;Persist Security Info=True;User ID=sa;pwd =sa;Asynchronous Processing=true"

       '这个必要加的Asynchronous Processing=true
        Dim theConnection As sqlConnection = New sqlConnection(theConnectionString)

        Dim theCommand As sqlClient.sqlCommand
        theCommand = New sqlCommand("select  * from tbdata",theConnection)
        theConnection.Open()

        Console.WriteLine("connection open and asynchronour processing starting.")

        theCommand.BeginExecuteReader(New AsyncCallback(AddressOf DataReady),theCommand)
    End Sub


     ‘要执行的异步处理过程
    Private Sub DataReady(ByVal asyncResult As IAsyncResult)
        Dim theCommand As sqlCommand = asyncResult.AsyncState
        Dim Dr As sqlDataReader

        Dr = theCommand.EndExecuteReader(asyncResult)


       While Dr.Read
            Console.WriteLine(Dr.GetString(2))
        End While

        theCommand.Connection.Close()
        Console.WriteLine("数据处理结束....")
    End Sub

vb.net数据库异步操作(二) 原文链接:https://www.f2er.com/vb/260915.html

猜你在找的VB相关文章