难道我有它得到处理之前关闭()的SQLConnection? [英] Do I have to Close() a SQLConnection before it gets disposed?
本文介绍了难道我有它得到处理之前关闭()的SQLConnection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
按我的其他<一个href=\"http://stackoverflow.com/questions/1033334/is-there-a-list-of-common-object-that-implement-idisposable-for-the-using-stateme\">question在这里对一次性对象,我们应该叫using块年底前关闭()?
使用(SqlConnection的连接=新的SqlConnection())
使用(的SqlCommand命令=新的SqlCommand())
{
command.CommandText =INSERT INTO YourMom(金额)VALUES(1);
command.CommandType = System.Data.CommandType.Text; connection.Open();
command.ExecuteNonQuery(); //是必要的这一呼吁?
connection.close()时;
}
解决方案
既然你有一个使用块时,的SqlCommand的Dispose方法将被调用,它将关闭连接:
// System.Data.SqlClient.SqlConnection.Dispose拆机
保护覆盖无效的Dispose(BOOL处置)
{
如果(处置)
{
this._userConnectionOptions = NULL;
this._poolGroup = NULL;
this.Close();
}
this.DisposeMe(处置);
base.Dispose(处置);
}
Per my other question here about Disposable objects, should we call Close() before the end of a using block?
using (SqlConnection connection = new SqlConnection())
using (SqlCommand command = new SqlCommand())
{
command.CommandText = "INSERT INTO YourMom (Amount) VALUES (1)";
command.CommandType = System.Data.CommandType.Text;
connection.Open();
command.ExecuteNonQuery();
// Is this call necessary?
connection.Close();
}
解决方案
Since you have a using block, the Dispose method of the SQLCommand will be called and it will close the connection:
// System.Data.SqlClient.SqlConnection.Dispose disassemble
protected override void Dispose(bool disposing)
{
if (disposing)
{
this._userConnectionOptions = null;
this._poolGroup = null;
this.Close();
}
this.DisposeMe(disposing);
base.Dispose(disposing);
}
这篇关于难道我有它得到处理之前关闭()的SQLConnection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文