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