如何找到未关闭的DataReader?
我在 ASP.Net 1.1 中维护一个大型应用程序,该应用程序已被许多人(包括学生)更改。显然,代码很少被审查,所以有很多……呃……愚蠢的错误。最有问题的是从未关闭的 DataReader。 而且到处都是,每页最多十个。由于这个项目大约有一百页,轻松有 300 多个类,一想到要找到所有未关闭的 DataReader,我就感到沮丧。
我知道这是没有希望的,但是有没有一种简单的方法可以找到所有这些未关闭的DataReader?一些软件或 Visual Studio 2003 调整...
I maintain a large application in ASP.Net 1.1 that has been altered by many people (including students). Obviously, the code has rarely been reviewed, so it is a lot of... err... stupid bugs. The most problematic are the never closed DataReader.
And there are everywhere, up to ten per page. Since this project is about a hundred pages with easily 300+ classes, I just depressed at the thought of finding all the unclosed DataReader.
I know it's hopeless, but is there an easy way to find all these unclosed DataReader? some software or visual studio 2003 tweak...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么,使用 DataReader 的“正确”方法是
或者
因此,简单的解决方案就是对
SqlDataReader
和IDataReader
进行完整项目搜索。如果它try
那么它很可能使用不正确并且需要修复。
请注意,以下代码:
是不正确的,因为在
// do some
期间引发的异常将使 DataReader 保持打开状态。Well, the "correct" way to use a DataReader is either
or
So, the simple solution is just to do a full-project search for
SqlDataReader
andIDataReader
. If it istry
then it's most likely used incorrectly and needs to be fixed.
Note that the following code:
is incorrect, since an exception thrown during
// do something
will leave the DataReader open.尝试查找和替换下的文件中替换功能。使用正则表达式。
{SqlDataReader.*}$
替换为using(\1) \{
。using({using(.*)} \{
替换为\1
。但是 make确保在开始之前备份文件。
这样,您就可以跳过那些已经有
using
块来关闭数据读取器的文件。Try the replace in files features under Find and Replace. Use Regular expression.
{SqlDataReader.*}$
withusing(\1) \{
.using({using(.*)} \{
with\1
.But make sure you backup your files before you start.
This way, you can skip those that already have a
using
block to close the data reader.