首页  ·  知识 ·  云计算
ADO.NET中异步处理的方式——函数回调方式
scottckt  博客园   综合  编辑:dezai   图片来源:网络
text-align: left函数回调方式 text-align: left&
函数回调方式

用代码说明
 

在页面上放Gridview用于显示

<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvData" runat="server">
</asp:GridView>
</div>
</form>
</body>

 

 

使用函数回调法方式取出数据
 

 

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace AdoAsyncDB
{
/// <summary>
/// 函数回调法
/// </summary>
public partial class CallBackMehtod : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetCallBackData();
}

private void GetCallBackData()
{
SqlConnection DBCon;
SqlCommand Command
= new SqlCommand();
IAsyncResult AsyncResult;

DBCon
= new SqlConnection();
DBCon.ConnectionString
= ConfigurationManager.ConnectionStrings["NorthWindDB"].ConnectionString;
Command.CommandText
= @"select top 20 companyName,contactName,city,postalCode from dbo.Customers";
Command.CommandType
= CommandType.Text;
Command.Connection
= DBCon;
DBCon.Open();
AsyncResult
= Command.BeginExecuteReader(new AsyncCallback(CallBackMethod), Command);
System.Threading.Thread.Sleep(
1000);
DBCon.Close();
}

public void CallBackMethod(IAsyncResult IResult)
{
SqlCommand Command
= (SqlCommand)IResult.AsyncState;
SqlDataReader OrdersReader
= Command.EndExecuteReader(IResult);
gvData.DataSource
=
OrdersReader;
gvData.DataBind();
}

}
}


 

 


 

代码下载:AdoAsyncDB.rar
 

本文作者:scottckt 来源:博客园
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读