关闭DataReader时自动关闭连接 [英] Close connection automatically while Closing DataReader
本文介绍了关闭DataReader时自动关闭连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我想在关闭数据读取器时自动关闭数据库连接.
借助Data Reader,我可以实现该功能吗?
我不想调用Connection.Close Method()
谢谢
Hi guys
i want to close my Data Base connection automatically when i close my Data reader.
Is there any property of Data Reader with the help of which i can achieve that.
I dont want to call Connection.Close Method()
Thanks
推荐答案
using语句如何?
为确保始终关闭连接,请在using块内部打开连接,如以下代码片段所示.这样可以确保在代码退出块时自动关闭连接.
How about the using statement?
To ensure that connections are always closed, open the connection inside of a using block, as shown in the following code fragment. Doing so ensures that the connection is automatically closed when the code exits the block.
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
干杯
Cheers
这篇关于关闭DataReader时自动关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文