private static string connectionString = RBAC.Dal.DataRootBase.ConnectionString;
private SqlConnection mConnection = new SqlConnection(connectionString);
#region
/// <summary>
/// 当点击执行查询时发生(异步操作)
/// 执行数据库waitfor delay延时存储过程
/// 或者waitfor time定时存储过程
/// </summary>
private void Button_DoSearch_Click(object sender, EventArgs e)
{
SqlCommand command = new SqlCommand("pro_StoreDelay", mConnection);
command.CommandType = CommandType.StoredProcedure;
mConnection.Open();
AsyncCallback callBack = new AsyncCallback(HandleCallback);//注册回调方法
//开始执行异步查询,将Command作为参数传递到回调函数以便执行End操作
command.BeginExecuteReader(callBack, command); //异步查询 回调
//command.BeginExecuteNonQuery(null, command); //直接执行 无回调
}
#endregion
#region
/// <summary>
/// 异步查询的回调方法
/// </summary>
/// <param name="MyResult">异步操作状态</param>
private void HandleCallback(IAsyncResult MyResult)
{
try
{
//SqlCommand command = (SqlCommand)MyResult.AsyncState;
//SqlDataReader reader = command.EndExecuteReader(MyResult);
//DataTable dataTable = new DataTable();
//dataTable.Load(reader);
//reader.Dispose();
//command.Dispose();
}
catch (Exception ex)
{
}
finally
{
if (mConnection != null)
{
mConnection.Close(); //回调后关闭连接
}
}
}
#endregion
转载于:https://my.oschina.net/smartsmile/blog/815507