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

在Winform开发中,作者遇到使用SqlDataReader时出现‘未将对象应用设置到对象实例’的错误。问题在于SqlDataReader不能直接通过new操作符实例化,而应通过SqlCommand的ExecuteReader方法获取。网上建议使用DataTable作为替代,但作者决定暂不解决SqlDataReader的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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) <

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值