是否有任何需要,如果使用子句用于关闭的DbConnection? [英] Is there any need to close a DbConnection if a using clause is used?

查看:195
本文介绍了是否有任何需要,如果使用子句用于关闭的DbConnection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能重复:
  <一href="http://stackoverflow.com/questions/5243398/will-a-using-block-close-a-database-connection">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.

这篇关于是否有任何需要,如果使用子句用于关闭的DbConnection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆