注册 登录
编程论坛 ASP.NET技术论坛

ado.net使用回调方式处理异步进程,到底哪里出错了?

ruihua 发布于 2007-10-04 00:13, 875 次点击

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillGridViewByCallback();
}
}

public void FillGridViewByCallback()
{
string connString = "Database=Northwind;Server=.;User Id=sa;Password=;Asynchronous Processing=true;MultipleActiveResultSets=True";
SqlConnection connNorthwind = new SqlConnection(connString);
SqlCommand commProducts = new SqlCommand("select * from Products", connNorthwind);
SqlCommand ordersComm = new SqlCommand("select * from orders", connNorthwind);
connNorthwind.Open();
commProducts.BeginExecuteReader(new AsyncCallback(productsCallbackMethod), commProducts, CommandBehavior.CloseConnection);
ordersComm.BeginExecuteReader(new AsyncCallback(ordersCallbackMethod), ordersComm, CommandBehavior.CloseConnection);
connNorthwind.Close();
}

public void productsCallbackMethod(IAsyncResult ar)
{
SqlCommand commProducts = ar.AsyncState as SqlCommand;
SqlDataReader productsReader = commProducts.EndExecuteReader(ar);
gvProducts.DataSource = productsReader;
gvProducts.DataBind();
}

public void ordersCallbackMethod(IAsyncResult ar)
{
SqlCommand commOrders = ar.AsyncState as SqlCommand;
SqlDataReader ordersReader = commOrders.EndExecuteReader(ar);
gvOrders.DataSource = ordersReader;
gvOrders.DataBind();
}
}

程序执行无任何显示,并出现异步操作已完成。请大虾们帮忙,感谢!

[此贴子已经被作者于2007-10-4 0:14:58编辑过]

2 回复
#2
bygg2007-10-04 18:45
commProducts.BeginExecuteReader(new AsyncCallback(productsCallbackMethod), commProducts, CommandBehavior.CloseConnection);

public void productsCallbackMethod(IAsyncResult ar)
上面没有参数,下面有?

绑定数据用DataSet吧
#3
ruihua2007-10-08 10:43
SqlDataReader也可以用作数据源的
1