C# Combining using statement with IAsyncReader

51 views Asked by At

I'm looking to combine an async fill operation with disposable.

class G 
{
    SqlConnection connection
    SqlCommand command;

    IAyncResult Begin(string connectionString)
    {
     this.connection = new SqlConnection(connectionString);
     this.command = connection.CreateCommand();
     result = command.BeginExecuteReader();
    }
    
    void End(IAsyncResult result)
    {
     SqlDataReader reader = this.command.EndExecuteReader();
     DataTable table = new DataTable(); // ..just as an example
     table.Load(reader);
     reader.Close();
     connection.Close();
    }
}

The problem is that the connection never gets disposed.
I am trying to figure out how to wrap this in a using statement or dispose of the connection in another way.
Should I create an outer class with a connection that calls this class or can it be done somewhere in here ?

0

There are 0 answers