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