我有以下ado.net代码,如果我已经使用使用来换我的DBCommand,我必须明确地关闭连接下?
谢谢,
public static void ExecuteSQL(int Id)
{
Database db;
const string sqlCommand = "Stored Procedure Name";
try
{
db = DatabaseFactory.CreateDatabase();
using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
{
db.AddInParameter(dbCommand, "p_Id", DbType.Int32, Id);
db.ExecuteNonQuery(dbCommand);
**//do I have to close connection explicitely here??**
if (dbCommand.Connection.State != ConnectionState.Closed)
{
dbCommand.Connection.Close();
}
dbCommand.Connection.Dispose();
}
}
catch (Exception ex)
{
Logger.Log.Error(ex.StackTrace, ex);
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
是的,如果您在using块中包含的所有内容都是DbCommand,那么您将必须显式关闭DbConnection,就像您目前在代码中所做的那样.然而,仅仅打电话就足够了Dispose.你不需要同时调用Close和Dispose.
| 归档时间: |
|
| 查看次数: |
5519 次 |
| 最近记录: |