您的位置:首页 > 数据库

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

2011-09-02 10:34 423 查看
在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以后再说吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: