关闭SQLDataReaders - 如何判断他们是否关闭? [英] closing SQLDataReaders - how to tell if they are closed?

查看:150
本文介绍了关闭SQLDataReaders - 如何判断他们是否关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我有一些网站连接池问题,我正在跟踪他们。我知道一件事要寻找的是确保任何SQLDataReader得到关闭,我已经走了,并确保他们是。在我脑海中弹出的一个问题是关于返回SQLDataReaders的方法以及它们如何关闭(或不关闭)。

I'm finding that I'm having some website connection pool issues and I'm in the process of tracking them down. I know one thing to look for is to make sure that any SQLDataReaders get closed and I've gone though and made sure they are. One question that popped up in my head was about methods that return SQLDataReaders and how they get closed (or not).

下面是我如何设置事物和一些示例方法:

So here's how I have things setup and some example methods:

public static SqlDataReader ExecuteReader(SqlCommand cmd)
{
    SqlConnection c = new SqlConnection(Properties.Settings.Default.DatabaseConn);
    cmd.Connection = c;
    c.Open();
    return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}

那么我有一个使用'ExecuteReader >

then I have a method that uses the 'ExecuteReader()'

public static SqlDataReader GetData()
{
  SqlCommand Query = new SqlCommand("select * from SomeTable");
  return ExecuteReader(Query);
}



现在说我有另一个方法调用GetData。我简单地简化了东西。

Now say I have another method that calls 'GetData'. I simplified things obviously.

public static SqlDataReader GetMoreData()
{
  return GetData;
}

所以我的问题是,当我像这样调用'GetMoreData' >

So my question is, when I call 'GetMoreData' like this

SqlDataReader dr = GetMoreData();
//do some stuff with 'dr'
dr.close();

我的所有SqlDataReaders和连接是否正确关闭?

Are all my SqlDataReaders and connections being closed properly?

感谢!

推荐答案

b

SqlDataReader 实现 IDisposable 接口。
在每个实现 IDisposable 的类中,您应该调用 Dispose 或使用
,以便释放资源,在这种情况下关闭阅读器和底层连接。

Description

The SqlDataReader implements the IDisposable interface. On every class that implements IDisposable you should call Dispose or use using in order to free resources, in this case close the reader and the underlying connection.


strong> IDisposable接口定义释放分配的资源的方法。

IDisposable Interface Defines a method to release allocated resources.



示例



Sample

using(SqlDataReader dr = GetMoreData()) 
{
    try
    {   
       // do your stuff
    }
    catch(Exception ex)
    {
       // handle the exception
    }
} // the reader will get closed here

SqlDataReader dr;
try
{   
    dr = GetMoreData();
    // do your stuff
}
catch(Exception ex)
{
   // handle the exception
}
finally
{
   // close the reader
   dr.Dispose();
}



编辑



尼斯评论: JotaBe


但是如果他实现了一个返回DataReader的方法,该方法的调用者。

but if he implements a method that returns a DataReader, the using should be used int the method's caller. So there's no way to warranty that the DataReader is closed.

我不建议返回 SqlDataReader 但是如果你想这样做,你需要这样做

I dont recommend to return a SqlDataReader but if you want to do this you need to do this

SqlDataReader reader;
try
{
   reader = methodThatReturnsAReader();
}
catch(Exception ex)
{
   // handle the exception
}
finally
{
   // close the reader
   reader.Dispose();
}



更多信息




  • MSDN - IDisposable介面

  • MSDN - SqlDataReader

  • More Information

    • MSDN - IDisposable Interface
    • MSDN - SqlDataReader Class
    • 这篇关于关闭SQLDataReaders - 如何判断他们是否关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆