Commands are normally executed synchronously,that causes all process to wait until command has been executed.For long running commands this gonna be a problem.In version 2.0 now we can write Asynchronous Commands as executing in sub threads..Here is the one code snippet to demonstrate....
Note:look how connection string property generated with the Asynchronous Processing attribute set to true....
string ver = string.Empty;
DateTime dtStart = DateTime.Now;
SqlConnectionStringBuilder cnSettings =
new SqlConnectionStringBuilder("DataSource=Pubs;"
+ " Asynchronous Processing=true;"
+ " Integrated Security=true"
+ "Max Pool Size=5");
using (SqlConnection cn1 = new SqlConnection(cnSettings.ConnectionString))
{
using (SqlConnection cn2 = new SqlConnection(cnSettings.ConnectionString))
{
using (SqlCommand cmd1 = cn1.CreateCommand())
{
using (SqlCommand cmd2 = cn2.CreateCommand())
{
cmd1.CommandText = "WaitFor Delay '00:00:10' Select '1st Query
";
cmd2.CommandText = "WaitFor Delay '00:00:10' Select '1st Query
";
cn1.Open();
cn2.Open();
IAsyncResult ar1 = cmd1.BeginExecuteReader();
IAsyncResult ar2 = cmd2.BeginExecuteReader();
ar1.AsyncWaitHandle.WaitOne();
SqlDataReader dr1 = cmd1.EndExecuteReader(ar1);
while (dr1.Read())
{
ver += dr1[0].ToString();
}
dr1.Close();
ar2.AsyncWaitHandle.WaitOne();
SqlDataReader dr2 = cmd1.EndExecuteReader(ar1);
while (dr2.Read())
{
ver += dr2[0].ToString();
}
dr2.Close();
}
}
No comments:
Post a Comment