我们正在使用Microsoft Enterprise库来访问sql server数据库。我们是 有一些关于如何关闭DB Connection.Code的疑问如下。
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory(), false);
Database db = new DatabaseProviderFactory().Create("Dataconnectionstring");
string sqlCommand = "";
DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
dbCommand.CommandTimeout = 0;
try
{
success = Convert.ToInt32(db.ExecuteScalar(dbCommand));
return success;
}
so how can we close database connection.
答案 0 :(得分:0)
执行此操作的一种方法是使用using
语句
实现IDisposable
的所有对象都可以以这种方式使用,当代码到达结束括号时,它调用Dispose()
来清除该对象。
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory(), false);
int success = 0;
using (var db = new DatabaseProviderFactory().Create("Dataconnectionstring"))
{
string sqlCommand = "";
using (var dbCommand = db.GetStoredProcCommand(sqlCommand))
{
dbCommand.CommandTimeout = 0;
try
{
success = Convert.ToInt32(db.ExecuteScalar(dbCommand));
}
}
}
return success;