SqlDataReader未将对象应用设置到对象实例

在winform开发中, 为了实现多层架构做到对数据库的访问单独操作,建了一个SQLServer数据库操作助手类。在此类中有一些函数是返回SqlDataReader类数据的函数。于是在调用的时候会这样子申明:

SqlDataReader datareader = null;

或者:

SqlDataReader datareader = new SqlDataReader();

调试的时候报错:未将对象应用设置到对象实例。

网上的解答是:SqlDataReader的申明必须是SqlDataReader sqlReader = command.ExecuteReader();

 

而且SqlDataReader有构造函数,只是不给用罢了:
internal SqlDataReader(SqlCommand command, CommandBehavior behavior)
{
   this._recordsAffected = -1;
   this.ObjectID = Interlocked.Increment(ref _objectTypeCount);
   this._command = command;
   this._commandBehavior = behavior;
   if (this._command != null)
   {
       this._timeoutSeconds = command.CommandTimeout;
       this._connection = command.Connection;
       if (this._connection != null)
       {
           this._statistics = this._connection.Statistics;
           this._typeSystem = this._connection.TypeSystem;
        }
   }
   this._dataReady = false;
   this._metaDataConsumed = false;
   this._hasRows = false;
   this._browseModeInfoConsumed = false;
}

难道就不能返回SqlDatareader吗?

有人建议改用公共类型DataTable,可以试试。至于SqlDatareader以后再说吧。

你可能感兴趣的:(command,null,sqlserver,WinForm,statistics,behavior)