VB.NET 异步读取数据方法之二
2011-08-08 09:01
267 查看
Imports System.Data.SqlClient
Imports System.Threading
Public Class FrmMain
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
Dim cn As New SqlConnection ’定义连结字符串,并且要注意加ASYNCHRONOUS PROCESSING=TRUE
cn.ConnectionString = "Data Source=WANGLI;Initial Catalog=PlcData;Persist Security Info=True;User ID=sa;pwd=sa;Asynchronous processing=true"
cn.Open()
Dim strSql As String = "select * from tbdata"
Dim mycommand As New SqlCommand(strSql, cn)
Dim itfAsynch As IAsyncResult ’定义一个异步读取变量
itfAsynch = mycommand.BeginExecuteReader(CommandBehavior.CloseConnection) ’执行异步读写
While Not itfAsynch.IsCompleted ’ 等待执行,
Console.WriteLine("Working on main thread...")
Thread.Sleep(1000)
End While
Console.WriteLine()
Dim dt As New DataTable ’执行完结后将数据写入datatable中
Dim MydataReader As SqlDataReader = mycommand.EndExecuteReader(itfAsynch)
'While MydataReader.Read
' Console.WriteLine("序号:" & MydataReader(0).ToString)
'End While
dt.Load(MydataReader)
DataGridView1.DataSource = dt
MydataReader.Close()
Console.ReadLine()
End Sub
End Class
Imports System.Threading
Public Class FrmMain
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
Dim cn As New SqlConnection ’定义连结字符串,并且要注意加ASYNCHRONOUS PROCESSING=TRUE
cn.ConnectionString = "Data Source=WANGLI;Initial Catalog=PlcData;Persist Security Info=True;User ID=sa;pwd=sa;Asynchronous processing=true"
cn.Open()
Dim strSql As String = "select * from tbdata"
Dim mycommand As New SqlCommand(strSql, cn)
Dim itfAsynch As IAsyncResult ’定义一个异步读取变量
itfAsynch = mycommand.BeginExecuteReader(CommandBehavior.CloseConnection) ’执行异步读写
While Not itfAsynch.IsCompleted ’ 等待执行,
Console.WriteLine("Working on main thread...")
Thread.Sleep(1000)
End While
Console.WriteLine()
Dim dt As New DataTable ’执行完结后将数据写入datatable中
Dim MydataReader As SqlDataReader = mycommand.EndExecuteReader(itfAsynch)
'While MydataReader.Read
' Console.WriteLine("序号:" & MydataReader(0).ToString)
'End While
dt.Load(MydataReader)
DataGridView1.DataSource = dt
MydataReader.Close()
Console.ReadLine()
End Sub
End Class
相关文章推荐
- C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(6):服务器端和客户端数据类型的自动转换:复杂类型
- Jquery+asp.net异步无刷新读取数据最简单示例(源码)
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法:服务器端和客户端数据类型的自动转换:复杂类型
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(6):服务器端和客户端数据类型的自动转换:复杂类型
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(9):服务器端和客户端数据类型的自动转换:DataTable和DataSet
- VB.net读写二进制数据方法
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(10):服务器端和客户端数据类型的自动转换:以XML方式序列化数据、小结
- VB.NET中treeview使用方法二与access数据库的数据绑定
- VB.NET中使用代表对方法异步调用
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(7):服务器端和客户端数据类型的自动转换:泛型集合类型
- VB.NET中使用代表对方法异步调用
- ASP.NET中应用Excel:12)其它读取Excel数据的方法(完)
- asp.net中打开Excel上传文件,读取数据的方法
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(5):服务器端和客户端数据类型的自动转换:基本类型和枚举类型
- vb.net启动本地程序,并读入数据的实现方法
- ASP.NET中应用Excel:12)其它读取Excel数据的方法(完)
- [VB.NET]求问高手 进程间大数据的交换 最好方法!
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(9):服务器端和客户端数据类型的自动转换:DataTable和DataSet
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(9):服务器端和客户端数据类型的自动转换:DataTable和DataSet