ADO.NET2.0异步查询方法学习
2008-03-01 13:44
363 查看
public string ConStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
...{
if (!IsPostBack)
...{
BindData();
//BindMultiData();
//BindMultiDataArray();
//BindDataWithCallBack();
}
}
//异步查询的poll方式,最普通的方式
private void BindData()
...{
string QueryStr = "SELECT * FROM customers";
using (SqlConnection Con = new SqlConnection(ConStr))
...{
SqlCommand Cmd = new SqlCommand(QueryStr, Con);
IAsyncResult ASynResult;
SqlDataReader Da;
try
...{
Con.Open();
//begin方法返回一个IAsyncResult对象,用来检查执行是否完成
ASynResult = Cmd.BeginExecuteReader(CommandBehavior.CloseConnection);
while (!ASynResult.IsCompleted)
...{
Response.Write("异步查询</br>");
ASynResult.AsyncWaitHandle.WaitOne(3000, true);
//System.Threading.Thread.Sleep(10);
}
Da = Cmd.EndExecuteReader(ASynResult);
GridView1.DataSource = Da;
GridView1.DataBind();
}
catch (Exception ex)
...{
Response.Write(ex.Message);
}
}
}
//异步查询的wait方式,使用多个等待句柄来异步查询,必须等待所有进程完成处理结果集
private void BindMultiData()
...{
string CusQueryStr = "SELECT * FROM customers WHERE CompanyName = 'Alfreds Futterkiste'";
string SupQueryStr = "SELECT Customers.CompanyName, Customers.ContactName, " +
"Orders.OrderID, Orders.OrderDate, " +
"Orders.RequiredDate, Orders.ShippedDate " +
"FROM Orders, Customers " +
"WHERE Orders.CustomerID = Customers.CustomerID " +
"AND Customers.CompanyName = 'Alfreds Futterkiste' " +
"ORDER BY Customers.CompanyName, Customers.ContactName";
using (SqlConnection MyCon = new SqlConnection(ConStr))
...{
SqlCommand CusCmd = new SqlCommand(CusQueryStr, MyCon);
SqlCommand SupCmd = new SqlCommand(SupQueryStr, MyCon);
SqlDataReader CusDr;
SqlDataReader SupDr;
IAsyncResult CusIsynResult;
IAsyncResult SupIsynResult;
//创建句柄数组
System.Threading.WaitHandle[] WHandles = new System.Threading.WaitHandle[2];
System.Threading.WaitHandle CusHandle;
System.Threading.WaitHandle SupHandle;
MyCon.Open();
CusIsynResult = CusCmd.BeginExecuteReader(CommandBehavior.CloseConnection);
SupIsynResult = SupCmd.BeginExecuteReader(CommandBehavior.CloseConnection);
CusHandle = CusIsynResult.AsyncWaitHandle;
SupHandle = SupIsynResult.AsyncWaitHandle;
//将等待句柄赋给句柄数组
WHandles[0] = CusHandle;
WHandles[1] = SupHandle;
//将数组传给waitall方法,等待所以的异步查询完成
System.Threading.WaitHandle.WaitAll(WHandles);
CusDr = CusCmd.EndExecuteReader(CusIsynResult);
SupDr = SupCmd.EndExecuteReader(SupIsynResult);
GridView1.DataSource = CusDr;
GridView1.DataBind();
GridView2.DataSource = SupDr;
GridView2.DataBind();
MyCon.Dispose();
CusCmd.Dispose();
SupCmd.Dispose();
}
}
//采用WaitAny方式,优点是不用等待所有进程都完成才处理结果集
private void BindMultiDataArray()
...{
string CusQueryStr = "SELECT * FROM customers WHERE CompanyName = 'Alfreds Futterkiste'";
string SupQueryStr = "SELECT Customers.CompanyName, Customers.ContactName, " +
"Orders.OrderID, Orders.OrderDate, " +
"Orders.RequiredDate, Orders.ShippedDate " +
"FROM Orders, Customers " +
"WHERE Orders.CustomerID = Customers.CustomerID " +
"AND Customers.CompanyName = 'Alfreds Futterkiste' " +
"ORDER BY Customers.CompanyName, Customers.ContactName";
using (SqlConnection MyCon = new SqlConnection(ConStr))
...{
SqlCommand CusCmd = new SqlCommand(CusQueryStr, MyCon);
SqlCommand SupCmd = new SqlCommand(SupQueryStr, MyCon);
SqlDataReader CusDr;
SqlDataReader SupDr;
IAsyncResult CusIsynResult;
IAsyncResult SupIsynResult;
System.Threading.WaitHandle[] WHandles = new System.Threading.WaitHandle[2];
System.Threading.WaitHandle CusHandle;
System.Threading.WaitHandle SupHandle;
int WHindex;
MyCon.Open();
CusIsynResult = CusCmd.BeginExecuteReader(CommandBehavior.CloseConnection);
SupIsynResult = SupCmd.BeginExecuteReader(CommandBehavior.CloseConnection);
CusHandle = CusIsynResult.AsyncWaitHandle;
SupHandle = SupIsynResult.AsyncWaitHandle;
WHandles[0] = CusHandle;
WHandles[1] = SupHandle;
for (int i = 0; i < WHandles.Length; i++)
...{
//waitany好处在于不必等待所有异步操作完成
WHindex = System.Threading.WaitHandle.WaitAny(WHandles);
switch (WHindex)
...{
case 0:
CusDr = CusCmd.EndExecuteReader(CusIsynResult);
GridView1.DataSource = CusDr;
GridView1.DataBind();
break;
case 1:
SupDr = SupCmd.EndExecuteReader(SupIsynResult);
GridView2.DataSource = SupDr;
GridView2.DataBind();
break;
}
}
MyCon.Dispose();
CusCmd.Dispose();
SupCmd.Dispose();
}
}
//通过回调来实现异步查询
private void BindDataWithCallBack()
...{
string SupQueryStr = "SELECT Customers.CompanyName, Customers.ContactName, " +
"Orders.OrderID, Orders.OrderDate, " +
"Orders.RequiredDate, Orders.ShippedDate " +
"FROM Orders, Customers " +
"WHERE Orders.CustomerID = Customers.CustomerID " +
"AND Customers.CompanyName = 'Alfreds Futterkiste' " +
"ORDER BY Customers.CompanyName, Customers.ContactName";
using (SqlConnection MyCon = new SqlConnection(ConStr))
...{
SqlCommand SupCmd = new SqlCommand(SupQueryStr, MyCon);
SqlDataReader SupDr;
IAsyncResult SupIsynResult;
MyCon.Open();
AsyncCallback Callback = new AsyncCallback(CallBackMethod);
SupIsynResult = SupCmd.BeginExecuteReader(Callback, SupCmd,CommandBehavior.CloseConnection);
System.Threading.Thread.Sleep(100);
MyCon.Dispose();
SupCmd.Dispose();
}
}
//回调方法
public void CallBackMethod(IAsyncResult IResult)
...{
SqlCommand Command = (SqlCommand)IResult.AsyncState;
SqlDataReader dr = Command.EndExecuteReader(IResult);
GridView1.DataSource = dr;
GridView1.DataBind();
}
}
本文来自: 中国自学编程网(www.zxbc.cn) 详细出处参考:http://www.zxbc.cn/html/adonet/1713241149778_2.html
永不言败:高级程序员,网站架构师。从事软件开发多年,,承接B/S架构相关项目。有意者请联系QQ:20028205
protected void Page_Load(object sender, EventArgs e)
...{
if (!IsPostBack)
...{
BindData();
//BindMultiData();
//BindMultiDataArray();
//BindDataWithCallBack();
}
}
//异步查询的poll方式,最普通的方式
private void BindData()
...{
string QueryStr = "SELECT * FROM customers";
using (SqlConnection Con = new SqlConnection(ConStr))
...{
SqlCommand Cmd = new SqlCommand(QueryStr, Con);
IAsyncResult ASynResult;
SqlDataReader Da;
try
...{
Con.Open();
//begin方法返回一个IAsyncResult对象,用来检查执行是否完成
ASynResult = Cmd.BeginExecuteReader(CommandBehavior.CloseConnection);
while (!ASynResult.IsCompleted)
...{
Response.Write("异步查询</br>");
ASynResult.AsyncWaitHandle.WaitOne(3000, true);
//System.Threading.Thread.Sleep(10);
}
Da = Cmd.EndExecuteReader(ASynResult);
GridView1.DataSource = Da;
GridView1.DataBind();
}
catch (Exception ex)
...{
Response.Write(ex.Message);
}
}
}
//异步查询的wait方式,使用多个等待句柄来异步查询,必须等待所有进程完成处理结果集
private void BindMultiData()
...{
string CusQueryStr = "SELECT * FROM customers WHERE CompanyName = 'Alfreds Futterkiste'";
string SupQueryStr = "SELECT Customers.CompanyName, Customers.ContactName, " +
"Orders.OrderID, Orders.OrderDate, " +
"Orders.RequiredDate, Orders.ShippedDate " +
"FROM Orders, Customers " +
"WHERE Orders.CustomerID = Customers.CustomerID " +
"AND Customers.CompanyName = 'Alfreds Futterkiste' " +
"ORDER BY Customers.CompanyName, Customers.ContactName";
using (SqlConnection MyCon = new SqlConnection(ConStr))
...{
SqlCommand CusCmd = new SqlCommand(CusQueryStr, MyCon);
SqlCommand SupCmd = new SqlCommand(SupQueryStr, MyCon);
SqlDataReader CusDr;
SqlDataReader SupDr;
IAsyncResult CusIsynResult;
IAsyncResult SupIsynResult;
//创建句柄数组
System.Threading.WaitHandle[] WHandles = new System.Threading.WaitHandle[2];
System.Threading.WaitHandle CusHandle;
System.Threading.WaitHandle SupHandle;
MyCon.Open();
CusIsynResult = CusCmd.BeginExecuteReader(CommandBehavior.CloseConnection);
SupIsynResult = SupCmd.BeginExecuteReader(CommandBehavior.CloseConnection);
CusHandle = CusIsynResult.AsyncWaitHandle;
SupHandle = SupIsynResult.AsyncWaitHandle;
//将等待句柄赋给句柄数组
WHandles[0] = CusHandle;
WHandles[1] = SupHandle;
//将数组传给waitall方法,等待所以的异步查询完成
System.Threading.WaitHandle.WaitAll(WHandles);
CusDr = CusCmd.EndExecuteReader(CusIsynResult);
SupDr = SupCmd.EndExecuteReader(SupIsynResult);
GridView1.DataSource = CusDr;
GridView1.DataBind();
GridView2.DataSource = SupDr;
GridView2.DataBind();
MyCon.Dispose();
CusCmd.Dispose();
SupCmd.Dispose();
}
}
//采用WaitAny方式,优点是不用等待所有进程都完成才处理结果集
private void BindMultiDataArray()
...{
string CusQueryStr = "SELECT * FROM customers WHERE CompanyName = 'Alfreds Futterkiste'";
string SupQueryStr = "SELECT Customers.CompanyName, Customers.ContactName, " +
"Orders.OrderID, Orders.OrderDate, " +
"Orders.RequiredDate, Orders.ShippedDate " +
"FROM Orders, Customers " +
"WHERE Orders.CustomerID = Customers.CustomerID " +
"AND Customers.CompanyName = 'Alfreds Futterkiste' " +
"ORDER BY Customers.CompanyName, Customers.ContactName";
using (SqlConnection MyCon = new SqlConnection(ConStr))
...{
SqlCommand CusCmd = new SqlCommand(CusQueryStr, MyCon);
SqlCommand SupCmd = new SqlCommand(SupQueryStr, MyCon);
SqlDataReader CusDr;
SqlDataReader SupDr;
IAsyncResult CusIsynResult;
IAsyncResult SupIsynResult;
System.Threading.WaitHandle[] WHandles = new System.Threading.WaitHandle[2];
System.Threading.WaitHandle CusHandle;
System.Threading.WaitHandle SupHandle;
int WHindex;
MyCon.Open();
CusIsynResult = CusCmd.BeginExecuteReader(CommandBehavior.CloseConnection);
SupIsynResult = SupCmd.BeginExecuteReader(CommandBehavior.CloseConnection);
CusHandle = CusIsynResult.AsyncWaitHandle;
SupHandle = SupIsynResult.AsyncWaitHandle;
WHandles[0] = CusHandle;
WHandles[1] = SupHandle;
for (int i = 0; i < WHandles.Length; i++)
...{
//waitany好处在于不必等待所有异步操作完成
WHindex = System.Threading.WaitHandle.WaitAny(WHandles);
switch (WHindex)
...{
case 0:
CusDr = CusCmd.EndExecuteReader(CusIsynResult);
GridView1.DataSource = CusDr;
GridView1.DataBind();
break;
case 1:
SupDr = SupCmd.EndExecuteReader(SupIsynResult);
GridView2.DataSource = SupDr;
GridView2.DataBind();
break;
}
}
MyCon.Dispose();
CusCmd.Dispose();
SupCmd.Dispose();
}
}
//通过回调来实现异步查询
private void BindDataWithCallBack()
...{
string SupQueryStr = "SELECT Customers.CompanyName, Customers.ContactName, " +
"Orders.OrderID, Orders.OrderDate, " +
"Orders.RequiredDate, Orders.ShippedDate " +
"FROM Orders, Customers " +
"WHERE Orders.CustomerID = Customers.CustomerID " +
"AND Customers.CompanyName = 'Alfreds Futterkiste' " +
"ORDER BY Customers.CompanyName, Customers.ContactName";
using (SqlConnection MyCon = new SqlConnection(ConStr))
...{
SqlCommand SupCmd = new SqlCommand(SupQueryStr, MyCon);
SqlDataReader SupDr;
IAsyncResult SupIsynResult;
MyCon.Open();
AsyncCallback Callback = new AsyncCallback(CallBackMethod);
SupIsynResult = SupCmd.BeginExecuteReader(Callback, SupCmd,CommandBehavior.CloseConnection);
System.Threading.Thread.Sleep(100);
MyCon.Dispose();
SupCmd.Dispose();
}
}
//回调方法
public void CallBackMethod(IAsyncResult IResult)
...{
SqlCommand Command = (SqlCommand)IResult.AsyncState;
SqlDataReader dr = Command.EndExecuteReader(IResult);
GridView1.DataSource = dr;
GridView1.DataBind();
}
}
本文来自: 中国自学编程网(www.zxbc.cn) 详细出处参考:http://www.zxbc.cn/html/adonet/1713241149778_2.html
永不言败:高级程序员,网站架构师。从事软件开发多年,,承接B/S架构相关项目。有意者请联系QQ:20028205
相关文章推荐
- ADO.NET Entity Framework 学习初级篇5--ObjectQuery查询及方法
- [新手入门]快速学习 ADO.NET Entity Framework系列文章 #3 -- LINQ-to-SQL、EntitySQL、查询产生器方法(Query builder)三种语法
- ACCP学习旅程之-----使用C#开发数据库应用程序(第七章 用ADO.NET查询和操作数据库)
- 【ADO.NET】数据库查询SQLCommand方法汇总
- ADO.NET Entity Framework学习笔记 ESQL查询语句
- C#学习笔记(六)-使用ADO .NET查询和操作数据库
- 黑马程序员之ADO.NET学习笔记:IP地址归属地查询。查询结果:北京移动[海淀]。
- SPRING.NET 1.3.2 学习19--方法注入之查询方法注入
- 使用ADO.NET类及方法实现数据库的查询并返回一个Object 数组
- [新手入门]快速学习 ADO.NET Entity Framework系列文章 #4 -- 数据新增、删除、修改(ObkectContext的 .SaveChange()方法)
- ADO.NET 的几个示例二,带连接数据查询方法
- ADO.NET数据查询方法
- 黑马程序员_学习日记49_618数据库开发及ADO.Net(多条件搜索、case…when…语句、索引Index、子查询、分页、Join语句)
- ADO.NET Entity Framework 学习高级篇1—改善EF代码的方法(上)
- Jquery如何序列化form表单数据为JSON对象 C# ADO.NET中设置Like模糊查询的参数 从客户端出现小于等于公式符号引发检测到有潜在危险的Request.Form 值 jquery调用iframe里面的方法 Js根据Ip地址自动判断是哪个城市 【我们一起写框架】MVVM的WPF框架(三)—数据控件 设计模式之简单工厂模式(C#语言描述)
- ADO.NET Entity Framework 学习高级篇2—改善EF代码的方法(下)
- ado.net entity framework使用不同的方法查询数据的不同性能
- SQL学习之查询技巧 查询表是否存在的两种方法
- 执行数据库命令Command对象——ADO.NET学习&应用笔记之三
- ADO.NET在开发中的部分使用方法与技巧