可能重复: Will一个使用块关闭数据库连接?
是 db.Close()
不必要的下面?
使用(的DbConnection DB = GetDbConnection())
{
//进行数据存取东西
// ...
db.Close();
}
解决方案
有任何需要关闭的DbConnection如果使用子句中使用?
没有任何有需要的,如果一个使用子句用于关闭的DbConnection?
和
是的,这是没有必要的,因为在这里使用两端连接将部署意味着关闭和释放所有内存时。
由于 DBConnection的
工具的IDisposable
界面,贴心的功能是存在于处置
的方法 DBConnection的
。
但如果有的线路已经接近行后那么它是非常有用的。
使用(的DbConnection DB = GetDbConnection())
{
//进行数据存取东西
// ...
db.Close(); //无用
}
但在这里它是有用的。
使用(的DbConnection DB = GetDbConnection())
{
//进行数据存取东西
// ...
db.Close(); //有用
//一些较code
}
在这种情况下,你可以做
使用(的DbConnection DB = GetDbConnection())
{
//进行数据存取东西
// ...
}
//一些较code这是previously使用一节内。
Possible Duplicate: Will a using block close a database connection?
Is db.Close()
unnecessary in the following?
using (DbConnection db = GetDbConnection())
{
// do data-access stuff
// ...
db.Close();
}
解决方案
Is there any need to close a DbConnection if a using clause is used?
No there any need to close a DbConnection if a using clause is used?
and
Yes it is unnecessary in here because when using ends connection will dispose meaning closing and releasing all memory.
Since DBConnection
implements IDisposable
interface, close function is there in the Dispose
method of DBConnection
.
But if some lines are after close line then it is useful
using (DbConnection db = GetDbConnection())
{
// do data-access stuff
// ...
db.Close(); //Useless
}
But here it is useful
using (DbConnection db = GetDbConnection())
{
// do data-access stuff
// ...
db.Close(); //Useful
// Some more code
}
In that case you can do
using (DbConnection db = GetDbConnection())
{
// do data-access stuff
// ...
}
// Some more code which was previously inside using section.