Ado.Net有三种异步处理方式:Poll方式,Wait方式和函数回调方式。
Poll方式:此方式是启动异步进程,再选择IAsyncResult对象,看看进程何时完成
Wait方式:此方式将等待句柄关联到异步进程上。等待全部或部分进程的完成,以便对它们执行相应的处理。
函数回调方式:是异步调用结束时回调函数将被触发,并且取出结果集显示在屏幕上。
对于异步处理的用途我没有用到过。 网上有人说当大数据量时或多个处理一起执行时等可以使用,这有待以后研究。
Poll方式使用方法
用代码来说明。
在页面上放一个GridView用于取数据。
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvData" runat="server">
</asp:GridView>
</div>
</form>
</body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvData" runat="server">
</asp:GridView>
</div>
</form>
</body>
使用Poll方式取出数据。
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace AdoAsyncDB
{
/// <summary>
/// Poll方式
/// </summary>
public partial class PollMethod : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UsePollMethod();
}
private void UsePollMethod()
{
SqlConnection connection;
SqlCommand sqlcommand = new SqlCommand();
SqlDataReader dataReader;
IAsyncResult asyncResult;
Response.Write("程序已启动……<br/>");
connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthWindDB"].ConnectionString);
sqlcommand.CommandText = "select companyName,contactName,city,postalCode from dbo.Customers";
sqlcommand.CommandType = CommandType.Text;
sqlcommand.Connection = connection;
connection.Open();
DateTime startDatetime = DateTime.Now;
asyncResult = sqlcommand.BeginExecuteReader(CommandBehavior.CloseConnection);
while (!asyncResult.IsCompleted)
{
Response.Write("正在取资料……<br/>");
Response.Flush();
System.Threading.Thread.Sleep(3000);
}
dataReader = sqlcommand.EndExecuteReader(asyncResult);
DateTime endDatetime = DateTime.Now;
TimeSpan timeSpan = endDatetime.Subtract(startDatetime);
Response.Write("资料取出完成,总其用时" + timeSpan.Milliseconds + "毫秒");
Response.Flush();
gvData.DataSource = dataReader;
gvData.DataBind();
connection.Close();
}
}
}
using System.Data.SqlClient;
using System.Configuration;
namespace AdoAsyncDB
{
/// <summary>
/// Poll方式
/// </summary>
public partial class PollMethod : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UsePollMethod();
}
private void UsePollMethod()
{
SqlConnection connection;
SqlCommand sqlcommand = new SqlCommand();
SqlDataReader dataReader;
IAsyncResult asyncResult;
Response.Write("程序已启动……<br/>");
connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthWindDB"].ConnectionString);
sqlcommand.CommandText = "select companyName,contactName,city,postalCode from dbo.Customers";
sqlcommand.CommandType = CommandType.Text;
sqlcommand.Connection = connection;
connection.Open();
DateTime startDatetime = DateTime.Now;
asyncResult = sqlcommand.BeginExecuteReader(CommandBehavior.CloseConnection);
while (!asyncResult.IsCompleted)
{
Response.Write("正在取资料……<br/>");
Response.Flush();
System.Threading.Thread.Sleep(3000);
}
dataReader = sqlcommand.EndExecuteReader(asyncResult);
DateTime endDatetime = DateTime.Now;
TimeSpan timeSpan = endDatetime.Subtract(startDatetime);
Response.Write("资料取出完成,总其用时" + timeSpan.Milliseconds + "毫秒");
Response.Flush();
gvData.DataSource = dataReader;
gvData.DataBind();
connection.Close();
}
}
}
代码下载:AdoAsyncDB.rar
文章汇总: