下面是reflector看到的代码, 这里的实现看的我一头雾水, 它应该是从socket取得来的数据吧,  socket  ---------(中间过程)-------------->    SqlDataReader.GetString(...)取数据
这个中间过程怎么样的?Can someone explain more? Thanks in advance.
public override bool Read()
{
    return this.ReadInternal(true);
} private bool ReadInternal(bool setTimeout)
{
    bool flag;
    SqlStatistics statistics = null;
    IntPtr ptr;
    Bid.ScopeEnter(out ptr, "<sc.SqlDataReader.Read|API> %d#", this.ObjectID);
    RuntimeHelpers.PrepareConstrainedRegions();
    try
    {
        statistics = SqlStatistics.StartTimer(this.Statistics);
        if (this._parser == null)
        {
            goto Label_017B;
        }
        if (setTimeout)
        {
            this.SetTimeout();
        }
        if (this._dataReady)
        {
            this.CleanPartialRead();
        }
        this._dataReady = false;
        SqlBuffer.Clear(this._data);
        this._nextColumnHeaderToRead = 0;
        this._nextColumnDataToRead = 0;
        this._columnDataBytesRemaining = -1L;
        if (this._haltRead)
        {
            goto Label_016A;
        }
        if (this.HasMoreRows())
        {
            while (this._stateObj._pendingData)
            {
                if (this._altRowStatus != ALTROWSTATUS.AltRow)
                {
                    this._dataReady = this._parser.Run(RunBehavior.ReturnImmediately, this._command, this, null, this._stateObj);
                    if (!this._dataReady)
                    {
                        continue;
                    }
                }
                else
                {
                    this._altRowStatus = ALTROWSTATUS.Done;
                    this._dataReady = true;
                }
                break;
            }
            if (this._dataReady)
            {
                this._haltRead = this.IsCommandBehavior(CommandBehavior.SingleRow);
                return true;
            }
        }
        if (!this._stateObj._pendingData)
        {
            this.CloseInternal(false);
        }
        goto Label_018E;
    Label_010E:
        this._dataReady = this._parser.Run(RunBehavior.ReturnImmediately, this._command, this, null, this._stateObj);
    Label_012E:
        if (this._stateObj._pendingData && !this._dataReady)
        {
            goto Label_010E;
        }
        if (this._dataReady)
        {
            this.CleanPartialRead();
        }
        this._dataReady = false;
        SqlBuffer.Clear(this._data);
        this._nextColumnHeaderToRead = 0;
    Label_016A:
        if (this.HasMoreRows())
        {
            goto Label_012E;
        }
        this._haltRead = false;
        goto Label_018E;
    Label_017B:
        if (this.IsClosed)
        {
            throw ADP.DataReaderClosed("Read");
        }
    Label_018E:
        return false;
    }
    catch (OutOfMemoryException exception3)
    {
        this._isClosed = true;
        SqlConnection connection3 = this._connection;
        if (connection3 != null)
        {
            connection3.Abort(exception3);
        }
        throw;
    }
    catch (StackOverflowException exception2)
    {
        this._isClosed = true;
        SqlConnection connection2 = this._connection;
        if (connection2 != null)
        {
            connection2.Abort(exception2);
        }
        throw;
    }
    catch (ThreadAbortException exception)
    {
        this._isClosed = true;
        SqlConnection connection = this._connection;
        if (connection != null)
        {
            connection.Abort(exception);
        }
        throw;
    }
    finally
    {
        SqlStatistics.StopTimer(statistics);
        Bid.ScopeLeave(ref ptr);
    }
    return flag;
}  

解决方案 »

  1.   

    private bool ReadInternal(bool setTimeout)
    {
        bool flag;
        SqlStatistics statistics = null;
        IntPtr ptr;
        Bid.ScopeEnter(out ptr, "<sc.SqlDataReader.Read|API> %d#", this.ObjectID);
        RuntimeHelpers.PrepareConstrainedRegions();
        try
        {
            statistics = SqlStatistics.StartTimer(this.Statistics);
            if (this._parser == null)
            {
                if (this.IsClosed)
            {
                throw ADP.DataReaderClosed("Read");
            }
            return false;
            }
            if (setTimeout)
            {
                this.SetTimeout();
            }
    //清除数据、初始化数据
            if (this._dataReady)
            {
                this.CleanPartialRead();
            }
            this._dataReady = false;
            SqlBuffer.Clear(this._data);
            this._nextColumnHeaderToRead = 0;
            this._nextColumnDataToRead = 0;
            this._columnDataBytesRemaining = -1L;
    //如果是停止读取(已经读到最后一条记录了或者EOF)了
            if (this._haltRead) 
            {
                if (this.HasMoreRows())
            {
               while (this._stateObj._pendingData && !this._dataReady) //注意此处变为循环了,运行到第一条记录处或者BOF
            {
                this._dataReady = this._parser.Run(RunBehavior.ReturnImmediately, this._command, this, null, this._stateObj);
            }
            if (this._dataReady)
            {
                this.CleanPartialRead();
            }
            this._dataReady = false;
            SqlBuffer.Clear(this._data);
            this._nextColumnHeaderToRead = 0;
            }
            this._haltRead = false;
            return false;
            }        if (this.HasMoreRows())
            {
                while (this._stateObj._pendingData)
                {
                    if (this._altRowStatus != ALTROWSTATUS.AltRow)
                    {
                        this._dataReady = this._parser.Run(RunBehavior.ReturnImmediately, this._command, this, null, this._stateObj);
                        if (!this._dataReady)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        this._altRowStatus = ALTROWSTATUS.Done;
                        this._dataReady = true;
                    }
                    break;
                }
                if (this._dataReady)
                {
                    this._haltRead = this.IsCommandBehavior(CommandBehavior.SingleRow);
                    return true;
                }
            }
            if (!this._stateObj._pendingData)
            {
                this.CloseInternal(false);
            }
            return false; 
        }
        catch (OutOfMemoryException exception3)
        {
            this._isClosed = true;
            SqlConnection connection3 = this._connection;
            if (connection3 != null)
            {
                connection3.Abort(exception3);
            }
            throw;
        }
        catch (StackOverflowException exception2)
        {
            this._isClosed = true;
            SqlConnection connection2 = this._connection;
            if (connection2 != null)
            {
                connection2.Abort(exception2);
            }
            throw;
        }
        catch (ThreadAbortException exception)
        {
            this._isClosed = true;
            SqlConnection connection = this._connection;
            if (connection != null)
            {
                connection.Abort(exception);
            }
            throw;
        }
        finally
        {
            SqlStatistics.StopTimer(statistics);
            Bid.ScopeLeave(ref ptr);
        }
        return flag;
    }
      

  2.   

    即有两种情况:一是已经读到最后一条记录了,返回false
    二是正常读取下一条记录,填充数据到this._data,然后返回true