如何在异常时关闭 DataReader
我的数据层的某些方法中有以下代码:
StringBuilder sb = new StringBuilder();
SqlCommand s = new SqlCommand(sb.ToString(), conn);
try
{
SqlDataReader dr = s.ExecuteReader();
while(dr.Read())
DoSomething(dr);
}
catch (Exception ex)
{
sb.Append(Util.ExceptionRecursive(ex));
}
问题是,如果出现异常,dr 永远不会关闭。当其他方法尝试访问另一个数据读取器时,它会抛出另一个异常,内容类似于“另一个数据读取器已连接到数据库”
我想在任何情况下关闭我的数据读取器。 但这:
sb = new StringBuilder();
SqlCommand s = new SqlCommand(sb.ToString(), conn);
SqlDataReader dr;
try
{
dr = s.ExecuteReader();
while(dr.Read())
DoSomething(dr);
}
catch (Exception ex)
{
sb.Append(Util.ExceptionRecursive(ex));
}
finally
{
dr.Close();
}
不起作用,因为在异常情况下 dr 可能没有数据,并且无法编译。
那我该怎么办呢?
I have the following code in some methods of my Data Layer:
StringBuilder sb = new StringBuilder();
SqlCommand s = new SqlCommand(sb.ToString(), conn);
try
{
SqlDataReader dr = s.ExecuteReader();
while(dr.Read())
DoSomething(dr);
}
catch (Exception ex)
{
sb.Append(Util.ExceptionRecursive(ex));
}
The thing is, dr never closes in case of exception. And when other method tries to access another data reader, it throws another exception that says something like "Another Datareader is connected to the Database"
I want to close my DataReader in any case.
But this:
sb = new StringBuilder();
SqlCommand s = new SqlCommand(sb.ToString(), conn);
SqlDataReader dr;
try
{
dr = s.ExecuteReader();
while(dr.Read())
DoSomething(dr);
}
catch (Exception ex)
{
sb.Append(Util.ExceptionRecursive(ex));
}
finally
{
dr.Close();
}
Won't work because in case of exception dr may have no data, and won't compile.
How should I do it then?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用
using
语句:它生成一个
finally
块以确保您的资源始终被释放。You should use the
using
statement:It generates a
finally
block to ensure that your resource is always disposed.最好的方法可能是这样的:
但是,如果您在 SQL 执行期间期望(或不期望)出现异常,则必须将异常处理代码放在外部:
The best way is probably this:
However, if you're expecting (or not) exceptions during SQL execution, you must place the exception handling code outside:
你可以将其写为:
you can write it as: