EndExecuteReader Method (MySqlCommand)
Ends an asynchronous invocation of the
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
MySqlDataReader and rendered to the console.
|
|---|
Public void Async_Exec(MySqlConnection mySqlConnection)
{
MySqlCommand mySqlCommand = new MySqlCommand("SELECT * FROM Test.Dept",mySqlConnection);
Console.WriteLine("Starting asynchronous retrieval of data...");
mySqlConnection.Open();
IAsyncResult cres = mySqlCommand.BeginExecuteReader(null,null,CommandBehavior.Default);
if (cres.IsCompleted)
Console.WriteLine("Completed.");
else
Console.WriteLine("Have to wait for operation to complete...");
MySqlDataReader mySqlReader = mySqlCommand.EndExecuteReader(cres);
try
{
while (mySqlReader.Read())
{
Console.WriteLine(mySqlReader.GetInt32(0) + " " + mySqlReader.GetString(1) + " " + mySqlReader.GetString(2));
}
}
finally
{
mySqlReader.Close();
mySqlConnection.Close();
}
} |
|
|---|
Public Sub Async_Exec(ByVal mySqlConnection As MySqlConnection)
Dim mySqlCommand As New MySqlCommand("SELECT * FROM Test.Dept", mySqlConnection)
Console.WriteLine("Starting asynchronous retrieval of data...")
mySqlConnection.Open()
Dim cres As IAsyncResult = mySqlCommand.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 mySqlReader As MySqlDataReader = mySqlCommand.EndExecuteReader(cres)
Try
While mySqlReader.Read()
Console.WriteLine(String.Concat(mySqlReader.GetInt32(0), " ", mySqlReader.GetString(1), " ", mySqlReader.GetString(2)))
End While
Finally
mySqlReader.Close()
mySqlConnection.Close()
End Try
End Sub |