Hi,
I am trying to understand DbConnection and DbCommand, and the proper way to dispose those objects after use.
Following is the code snippet I have. By using "using statement" on DbConnection and DbCommand, would it be sufficient? I am trying to prevent possible memory leak.
2nd question,
Do I have to Dispose DbCommand object?
thanks a lot
DbProviderFactory fac = DbProviderFactories.GetFactory(this.DatabaseProviderName);
using (DbConnection dbConn = fac.CreateConnection())
{
dbConn.ConnectionString = this.ConnectionString;
using (DbCommand comm = fac.CreateCommand())
{
comm.CommandText = "select * from aTable";
comm.Connection = dbConn;
DataTable targetTable = new DataTable();
DbDataAdapter facDA = fac.CreateDataAdapter();
facDA.SelectCommand = comm;
facDA.Fill(targetTable);
//assuming Adapter would open / close connection (right assumption?)
//do something with the datatable
}
}