using System;
using System.Data;
using System.Threading;
using System.Data.SqlClient;
class MainClass {
public static void CallbackHandler(IAsyncResult result) {
using (SqlCommand cmd = result.AsyncState as SqlCommand) {
using (SqlDataReader reader = cmd.EndExecuteReader(result)) {
lock (Console.Out) {
while (reader.Read()) {
Console.WriteLine(" {0} = {1}",
reader["ID"],
reader["FirstName"]);
}
}
}
}
}
public static void Main()
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;Asynchronous Processing=true;";
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT ID, FirstName FROM Employee";
con.Open();
cmd.BeginExecuteReader(CallbackHandler, cmd);
// Continue with other processing.
for (int count = 0; count < 10; count++)
{
lock (Console.Out)
{
Console.WriteLine("{0} : Continue processing...",
DateTime.Now.ToString("HH:mm:ss.ffff"));
}
Thread.Sleep(500);
}
}
}
}
|