如下两段异步查询的代码,第一段代码不用回调方法直接异步查询,可以顺利返回查询数据,并绑定到GridView1上;第2段代码运行后,前台的GridView绑不上用来接值的SqlDataReader对象dr,经过跟踪后发现此SqlDataReader对象 dr.hasRow属性为真,求高人帮忙看下代码 看看问题是出现在哪1步了 ,谢谢~ 两段代码如下 1: private void DoSomething()
{
SqlDataReader dr;
IAsyncResult res;
SqlConnection con=new SqlConnection("若干语句.....");
SqlCommand com=con.CreateCommand();
com.CommandType=Command.Text;
com.CommandText="若干语句.....";
conn.open();
res=com.BeginExcuteReader();
dr=com.EndExcuteReader(res);
this.GridView1.DataSource=dr;
this.GridView1.DataBind();
}
2: private SqlConnection con=null;
private void DoSomething()
{ con=new SqlConnection("若干语句.....");
SqlCommand com=con.CreateCommand();
com.CommandType=Command.Text;
com.CommandText="若干语句.....";
conn.open();
com.BeginExcuteReader(new AsyncCallback(Back),com); } private void Back(IAsyncResult res)
{
SqlCommand com=(SqlCommand)res.AsyncState;
SqlDataReader dr=com.EndExcuteReader(res);
this.GridView1.DataSource=dr;
this.GridView1.DataBind();
this.con.Close();
}
public void button_click(....){
//实例委托
AsyncEventHandler asy = new AsyncEventHandler(Bind); //异步调用开始,没有回调函数和AsyncState,都为null
IAsyncResult ia = asy.BeginInvoke(null, null);}public void Bind(){
SqlConnection con=new SqlConnection("若干语句.....");
SqlCommand com=con.CreateCommand();
com.CommandType=Command.Text;
com.CommandText="若干语句.....";
conn.open();
res=com.BeginExcuteReader();
dr=com.EndExcuteReader(res);
this.GridView1.DataSource=dr; //此处好像不能直接操作 GridView,要用委托,new dele....语法忘了
this.GridView1.DataBind();
}