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
        BindData()
    End Sub


    Private Sub BindData()
        Dim ConStr As String = "Data Source=.;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Pwd=sa;Asynchronous Processing=true"
        Dim QueryStr As String = "select * from Customers"
        Using con As SqlConnection = New SqlConnection(ConStr)
            Dim cmd As SqlCommand = New SqlCommand(QueryStr, con)
            Dim AsynResult As IAsyncResult
            Dim Dr As SqlDataReader
            Try
                con.Open()
                AsynResult = cmd.BeginExecuteReader(CommandBehavior.CloseConnection)

                While (Not AsynResult.IsCompleted)
                    Console.WriteLine("异步查询方式:")
                    AsynResult.AsyncWaitHandle.WaitOne(1000, True)
                    'system.threading .thread sleep(3000);
                End While

                Dr = cmd.EndExecuteReader(AsynResult)

                Dim bs As BindingSource = New BindingSource
                bs.DataSource = Dr
                DataGridView1.DataSource = bs

                'While Dr.Read
                '    Console.WriteLine(Dr.Item(1).ToString)
                'End While


            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try

        End Using
    End Sub
End Class

你可能感兴趣的:(vs.net书籍,vb.net,button,processing,exception,string,security)