C# using 语句是否执行 try/finally ?
假设我有以下代码:
private void UpdateDB(QuoteDataSet dataSet, Strint tableName)
{
using(SQLiteConnection conn = new SQLiteConnection(_connectionString))
{
conn.Open();
using (SQLiteTransaction transaction = conn.BeginTransaction())
{
using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM " + tableName, conn))
{
using (SQLiteDataAdapter sqliteAdapter = new SQLiteDataAdapter())
{
sqliteAdapter.Update(dataSet, tableName);
}
}
transaction.Commit();
}
}
}
C# 文档指出,使用 using
语句,作用域内的对象将被释放,并且我看到了几个建议我们不需要使用的地方try/finally 子句。
我通常用 try/finally 包围我的连接,并且总是在 finally 子句中关闭连接。根据上面的代码,假设出现异常时连接将被关闭是否合理?
Suppose that I have the following code:
private void UpdateDB(QuoteDataSet dataSet, Strint tableName)
{
using(SQLiteConnection conn = new SQLiteConnection(_connectionString))
{
conn.Open();
using (SQLiteTransaction transaction = conn.BeginTransaction())
{
using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM " + tableName, conn))
{
using (SQLiteDataAdapter sqliteAdapter = new SQLiteDataAdapter())
{
sqliteAdapter.Update(dataSet, tableName);
}
}
transaction.Commit();
}
}
}
The C# documentation states that with a using
statement the object within the scope will be disposed and I've seen several places where it's suggested that we don't need to use try/finally clause.
I usually surround my connections with a try/finally, and I always close the connection in the finally clause. Given the above code, is it reasonable to assume that the connection will be closed if there is an exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是对的;
using
语句编译为try
/finally
块。编译器将
using(resource) 语句;
转换为以下代码:(如果
ResourceType
实现IDisposable<,则转换为
IDisposable
/code> 明确地。You are correct; the
using
statement compiles to atry
/finally
block.The compiler transforms
using(resource) statement;
into the following code:(The cast to
IDisposable
is in caseResourceType
implementsIDisposable
explicitly.是的,您需要使用 try/finally 或 using 语句。你不需要两者。
using 语句 几乎是与 try/finally 相同,只是在 C# 3 中不能重新分配给 using 块内的变量。
以前,您可以重新分配,但原始对象仍然会被释放,而不是新分配的对象,并且您还会收到此编译警告:
Yes, you either need to use a try/finally or a using statement. You don't need both.
A using statement is almost the same as a try/finally except that in C# 3 you can't reassign to the variable inside the using block.
Previously you could reassign but the original object would still be disposed, not the newly assigned object and you would also get this compile warning:
是的,
using
语句几乎只是try ...finally
块的简写。例如,这段代码...
...相当于以下内容...
Yes, the
using
statement is pretty much just shorthand for atry ... finally
block.For example, this code...
...would equate to the following...
您可以假设如果出现异常,连接将被关闭。
You can assume that the connection will be closed if you get an exception.
using() 确保参数中实例化的项将被处理,无论关联代码块中发生什么情况。这包括关闭数据库连接(假设
SQLiteConnection
正确处理其处置)。Using() ensures that the item instantiated within the parameters will be disposed of regardless of that happens within the associated code block. This includes closing the database connection assuming that
SQLiteConnection
handles its disposal correctly.