从方法返回DataReader? [英] Return DataReader from a method?

查看:70
本文介绍了从方法返回DataReader?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很难从方法返回DataReader,因为在使用DataReader时必须打开Connection,在DataReader关闭后,Connection很难关闭。



  public   static  OleDbDataReader GetReader( string  sqlstr)
{
using (OleDbConnection) conn = new OleDbConnection(connstr))
{
OleDbCommand comm = new OleDbCommand (sqlstr,conn);
尝试
{
conn.Open();
OleDbDataReader read = comm.ExecuteReader();
return read;
}
catch (System.Data.OleDb.OleDbException e)
{
throw new 异常(e.Message);
}
}
}





我认为DataReader比返回要快得多DataTable如果你只需要一行数据。



我想知道返回DataReader或DataTable哪个更好?

解决方案

SAK在异常处理上是正确的。当你在发布的代码片段中做你正在做的事情时,你实际上正在破坏堆栈跟踪,丢失了宝贵的调试信息。



除此之外,如果所有你要做的就是重新抛出一般的异常,捕获的代码无法分辨抛出哪种异常,因此无法适应可能的问题。



在你的代码中,只需将try / catch块一起移除,让任何异常从调用链向上传播到调用者可能具有的任何可能的处理程序。





现在,对于已发布的问题的答案......都没有!对于单个记录,DataTable太重了。对于DataReader,您必须依赖调用者在完成后关闭Reader。太复杂了,它要求调用代码知道必须处理资源。它还必须知道哪个数据字段在哪个列中,并且对于Reader和DataTable使用这样的类型,blah,blah,blah。



这个解决方案很容易......专门为了将这些数据传回给调用者而创建一个类。这完全隔离了调用代码中的数据库详细信息,您不必担心调用者没有正确地执行某些操作来释放资源。您所做的只是,在您发布的方法中,从数据库中读取数据,创建此模型类的新实例,将数据复制到其中,关闭数据库连接并将该类实例返回给调用者。

I find it's hard to return a DataReader from a method, because the Connection must open when using DataReader , after DataReader close, Connection is hard to close.

public static OleDbDataReader GetReader(string sqlstr)
{
    using (OleDbConnection conn = new OleDbConnection(connstr))
    {
        OleDbCommand comm = new OleDbCommand(sqlstr, conn);
        try
        {
            conn.Open();
            OleDbDataReader read = comm.ExecuteReader();
            return read;
        }
        catch (System.Data.OleDb.OleDbException e)
        {
            throw new Exception(e.Message);
        }
    }
  }



I think DataReader is much quicker than return DataTable if you only need one row of data.

I want to know return DataReader or DataTable which one is better?

解决方案

SAK is correct on the Exception handling. When doing what you're doing in the posted code snippet, you're actually destroying the stack trace, losing precious debugging information.

On top of that, if all you're going to do is re-throw the generic Exception, the catching code can't tell which kind of Exception was thrown and, hence, can't adapt to the possible problem.

In your code, just remove the try/catch block all together and let any exceptions propagate back up the call chain to any possible handler the caller may have in place.


Now, for the answer to the posted question... neither! For a single record, a DataTable is too heavy-weight. For a DataReader, you have to rely on the caller to close the Reader when it's done. Too complicated and it requires the calling code to know that is has to dispose of resources. It also has to know which data field is in which column and using such-and-such type, blah, blah, blah, for both the Reader and the DataTable.

The solution to this is easy...create a class specifically for the purpose of transferring this data back to the caller. This completely insulates the database details from the calling code and you don't have to worry about the caller not doing something properly to free resources. All you do is, in the method that you posted, read the data from the database, create a new instance of this model class, copy the data into it, close the database connection and return that class instance to the caller.


这篇关于从方法返回DataReader?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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