ADO.NET2.0 异步处理的三种方式-函数回调法
2006-06-20 22:47
453 查看
下面的代码用内嵌sql语句方式通过调用BeginExecuteReader取出前五条数据,并且把callback的代理传给这个方法。不需要其他的处理,当这个异步调用结束时回调函数将被触发,并且取出结果集显示在屏幕上。
<%@ Page Language=”C#” %>
<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Data.SqlClient” %>
<%@ Import Namespace=”System.Configuration” %>
<script runat=”server”>
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection DBCon;
SqlCommand Command = new SqlCommand();
SqlAsyncResult ASyncResult;
DBCon = new SqlConnection();
Command = new SqlCommand();
DBCon.ConnectionString =
ConfigurationManager.ConnectionStrings[“DSN_NorthWind”].ConnectionString;
// Selecting top 5 records from the Orders table
Command.CommandText =
“SELECT TOP 5 Customers.CompanyName, Customers.ContactName, “ +
“ Orders.OrderID, Orders.OrderDate, “ +
“ Orders.RequiredDate, Orders.ShippedDate “ +
“ FROM Orders, Customers “ +
“ WHERE Orders.CustomerID = Customers.CustomerID “ +
“ ORDER BY Customers.CompanyName, Customers.ContactName “;
Command.CommandType = CommandType.Text;
Command.Connection = DBCon;
DBCon.Open();
// Starting the asynchronous processing
AsyncResult = Command.BeginExecuteReader(new AsyncCallback(CBMethod),
CommandBehavior.CloseConnection);
}
public void CBMethod(SQLAsyncResult ar)
{
SqlDataReader OrdersReader;
// Retrieving result from the asynchronous process
OrdersReader = ar.EndExecuteReader(ar);
// Displaying result on the screen
gvOrders.DataSource = OrdersReader;
gvOrders.DataBind();
}
</script>
回调函数可以让你在代码的其他部分来处理命令执行的结果。这种特性在命令执行过程比一般的长但是你不想让用户等待过程调用结束时会非常有用。
<%@ Page Language=”C#” %>
<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Data.SqlClient” %>
<%@ Import Namespace=”System.Configuration” %>
<script runat=”server”>
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection DBCon;
SqlCommand Command = new SqlCommand();
SqlAsyncResult ASyncResult;
DBCon = new SqlConnection();
Command = new SqlCommand();
DBCon.ConnectionString =
ConfigurationManager.ConnectionStrings[“DSN_NorthWind”].ConnectionString;
// Selecting top 5 records from the Orders table
Command.CommandText =
“SELECT TOP 5 Customers.CompanyName, Customers.ContactName, “ +
“ Orders.OrderID, Orders.OrderDate, “ +
“ Orders.RequiredDate, Orders.ShippedDate “ +
“ FROM Orders, Customers “ +
“ WHERE Orders.CustomerID = Customers.CustomerID “ +
“ ORDER BY Customers.CompanyName, Customers.ContactName “;
Command.CommandType = CommandType.Text;
Command.Connection = DBCon;
DBCon.Open();
// Starting the asynchronous processing
AsyncResult = Command.BeginExecuteReader(new AsyncCallback(CBMethod),
CommandBehavior.CloseConnection);
}
public void CBMethod(SQLAsyncResult ar)
{
SqlDataReader OrdersReader;
// Retrieving result from the asynchronous process
OrdersReader = ar.EndExecuteReader(ar);
// Displaying result on the screen
gvOrders.DataSource = OrdersReader;
gvOrders.DataBind();
}
</script>
回调函数可以让你在代码的其他部分来处理命令执行的结果。这种特性在命令执行过程比一般的长但是你不想让用户等待过程调用结束时会非常有用。
相关文章推荐
- ADO.NET2.0 异步处理的三种方式-轮循检测法
- ADO.NET2.0 异步处理的三种方式-wait方法
- ADO.NET中异步处理的方式——函数回调方式
- ADO.NET2.0 异步处理
- 使用C#创建webservice及三种调用方式 (ASP.NETweb编程常用到的27个函数集)
- ASP.NET 2.0中的客户端回调机制与ajax方式比较
- 通过ado.net获取数据得三种方式
- ADO.NET连接SQL2000的三种方式在连接时间方面的不同
- ADO.Net中通过HashTable对SqlParameter赋值的三种方法和返回DataTable的两种方式
- 在 ASP.NET 网页中不经过回发而以编程方式实现客户端回调
- C/C++语言中函数参数传递的三种方式
- C语言中函数参数传递的三种方式
- 项目一,C++语言中函数的三种传递方式
- ASP.net 2.0 中的客户端回调浅析
- 函数的三种定义方式
- 第二周 项目1-C/C++语言中函数参数传递的三种方式
- ADO.NET 中数据库连接方式
- 第二周【项目1】 函数参数传递的三种方式
- ADO.NET 2.0 中的架构
- ADO.NET 2.0 中的架构