Ends an asynchronous invocation of the
System.Data.Common.DbCommandBase.ExecuteReader method.
In the example below an easiest way of executing an asynchronous operation is shown. First, all components are set up and a connection is opened. Second, a data retrieval operation is started. The operation state (done or in progress) is checked and a report is written to the console. At last query results are sent to
DbDataReaderBase and rendered to the console.
public void Async_Exec(DbConnectionBase myConnection)
{
DbCommandBase myCommand = (DbCommandBase)myConnection.CreateCommand();
myCommand.CommandText = "SELECT Id, FirstName, LastName FROM Client";
Console.WriteLine("Starting asynchronous retrieval of data...");
myConnection.Open();
IAsyncResult cres = myCommand.BeginExecuteReader(null, null, CommandBehavior.Default);
if (cres.IsCompleted)
Console.WriteLine("Completed.");
else
Console.WriteLine("Have to wait for operation to complete...");
DbDataReader myReader = myCommand.EndExecuteReader(cres);
try
{
while (myReader.Read())
{
Console.WriteLine(myReader.GetString(0) + " " + myReader.GetString(1) + " " + myReader.GetString(2));
}
}
finally
{
myReader.Close();
myConnection.Close();
}
}
Public Sub Async_Exec(ByVal myConnection As DbConnectionBase)
Dim myCommand As DbCommandBase = myConnection.CreateCommand()
myCommand.CommandText = "SELECT Id, FirstName, LastName FROM Client"
Console.WriteLine("Starting asynchronous retrieval of data...")
myConnection.Open()
Dim cres As IAsyncResult = myCommand.BeginExecuteReader(Nothing, Nothing, CommandBehavior.Default)
If cres.IsCompleted Then
Console.WriteLine("Completed.")
Else
Console.WriteLine("Have to wait for operation to complete...")
End If
Dim myReader As DbDataReader = myCommand.EndExecuteReader(cres)
Try
While myReader.Read()
Console.WriteLine(String.Concat(myReader.GetString(0), " ", myReader.GetString(1), " ", myReader.GetString(2)))
End While
Finally
myReader.Close()
myConnection.Close()
End Try
End Sub