1、用异步方式进行查询
SqlConnection con;
SqlCommand com;try
{
con = new SqlConnection("UID=sa;Password=123;Initial Catalog=AD;Data Source=192.168.1.1;Asynchronous Processing=true");
con.Open();
com = new SqlCommand("select top 100 * from tb_user", con);
com.BeginExecuteReader(new AsyncCallback(
delegate(IAsyncResult ar)
{
if (ar.IsCompleted)
{
SqlCommand tempCom = (SqlCommand)ar.AsyncState;
SqlDataReader tempDr = tempCom.EndExecuteReader(ar);
DataTable dt = new DataTable();
dt.Load(tempDr);
tempDr.Close();
}
}
), com);
}
catch (Exception ex)
{
MessageBox.Show("程序发生错误,信息: " + ex.Message);
}
2、在合适的地方调用 com.Cancel();