C# SqlConnection.Close() 里面的 using 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18588049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SqlConnection.Close() inside using statement
提问by Etrit
I'm using this code:
我正在使用此代码:
public void InsertMember(Member member)
{
string INSERT = "INSERT INTO Members (Name, Surname, EntryDate) VALUES (@Name, @Surname, @EntryDate)";
using (sqlConnection = new SqlConnection(sqlConnectionString_WORK))
{
sqlConnection.Open();
using (SqlCommand sqlCommand = new SqlCommand(INSERT, sqlConnection))
{
sqlCommand.Parameters.Add("@Name", SqlDbType.VarChar).Value = member.Name;
sqlCommand.Parameters.Add("@Surname", SqlDbType.VarChar).Value = member.Surname;
sqlCommand.Parameters.Add("@EntryDate", SqlDbType.Date).Value = member.EntryDate;
sqlCommand.ExecuteNonQuery();
}
}
}
Is it wrong if I don't add sqlConnection.Close();
before disposing it? I mean. It's not showing any errors, no problems at all. Is it better to Close it first? If yes, why?
sqlConnection.Close();
处理前不添加是不是有问题?我的意思是。它没有显示任何错误,根本没有问题。最好先关闭它吗?如果是,为什么?
回答by Darren
No need to Close or Dispose
the using
block will take care of that for you.
不需要Close or Dispose
的using
模块会照顾的,对于你。
As stated from MSDN:
正如MSDN 所述:
The following example creates a SqlConnection, opens it, displays some of its properties. The connection is automatically closed at the end of the using block.
下面的示例创建一个 SqlConnection,打开它,显示它的一些属性。连接在 using 块结束时自动关闭。
private static void OpenSqlConnection(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
Console.WriteLine("State: {0}", connection.State);
}
}
回答by Alex
No, it is not wrong. The sqlConnection will close the connection after it will pass usingblock and call Dispose method. SqlConnection.Dispose() equal to SqlConnection.Close() method.
不,没有错。sqlConnection 在通过using块并调用 Dispose 方法后将关闭连接。SqlConnection.Dispose() 等于 SqlConnection.Close() 方法。
From MSDN: If the SqlConnection goes out of scope, it won't be closed. Therefore, you must explicitly close the connection by calling Close or Dispose. Close and Dispose are functionally equivalent.
来自 MSDN:如果 SqlConnection 超出范围,它不会被关闭。因此,您必须通过调用 Close 或 Dispose 显式关闭连接。Close 和 Dispose 在功能上是等效的。
回答by Hexie
You are using a Using
which will Dispose()
the object for you.
您正在使用 aUsing
这将为Dispose()
您提供对象。
If you take the connection outside of the Using
statement, then yes - you would need to close the connection when finished.
如果您将连接置于Using
语句之外,那么是的 - 您需要在完成后关闭连接。
回答by Grant Thomas
According to MSDN documentation for the Close
method:
you must explicitly close the connection by calling Closeor Dispose. Closeand Disposeare functionally equivalent.
您必须通过调用Close或Dispose显式关闭连接。Close和Dispose在功能上是等效的。
Therefore, calling Dispose
(implicitly so, even, using using
) will cover your bases, as it were.
因此,调用Dispose
(暗示如此,甚至使用using
)将覆盖您的基础,就像它一样。
It's worth noting, too, I think,though not specific to your case, that Close
will always effectively be called when the thing is wrapped in a using
statement - which might notbe the case should it be omitted and an exception occur without the proper try
/catch
/finally
handling.
同样值得注意的是,我认为,虽然不是特定于您的情况,Close
但当事物被包装在using
语句中时,它总是会被有效地调用-如果省略它并且在没有适当的情况下发生异常,则情况可能并非如此try
/ catch
/finally
处理。
回答by Ehsan
Is it wrong if I don't add sqlConnection.Close(); before disposing it
不加sqlConnection.Close()有错吗?在处理它之前
No, it is not as long as you are using your connection within Using
. When you will leave the using scope, Dispose
will be called for sql connection. which will close the existing connectionand free-up all the resources as well.
不,只要您在Using
. 当您离开 using 范围时,Dispose
将调用 sql 连接。这将关闭现有连接并释放所有资源。
回答by V4Vendetta
The using statement is a try finally block and in your case the final block would have a connection.Dispose()
call. So you don't really need a independent connection.Close()
statement there.
using 语句是一个 try finally 块,在您的情况下,final 块将有一个connection.Dispose()
调用。所以你真的不需要在connection.Close()
那里发表独立声明。
The advantage is that this ensures the disposal even in case of an exception since the finally block will always run.
优点是即使在发生异常的情况下也能确保处理,因为 finally 块将始终运行。
try
{
sqlConnection.Open();
// ....
}
finally
{
if(sqlConnection != null)
sqlConnection.Dispose();
}
回答by Suraj Singh
The using statement ensures that Dispose is called
even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block
; in fact, this is how the using statement is translated by the compiler. MSDN
的using statement ensures that Dispose is called
,而你是调用对象的方法时,即使异常。You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block
; 事实上,这就是编译器如何翻译using 语句。MSDN
So ultimately your code line
所以最终你的代码行
using (sqlConnection = new SqlConnection(sqlConnectionString_WORK))
will be converted into a normal try finally block
by compiler calling IDisposable object in the finally
将try finally block
通过编译器调用转换为正常IDisposable object in the finally