我正在尝试理解DbConnection和DbCommand,以及在使用后正确处理这些对象的方法。
以下是我拥有的代码片段。 通过在DbConnection和DbCommand上使用“using语句”,是否足够?我正在尝试防止可能的内存泄漏。
第二个问题,
我必须释放DbCommand对象吗?
非常感谢。
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
}
}