override public void Close() {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<oledb.OleDbDataReader.Close|API> %d#\n", ObjectID);
            try {
                OleDbConnection con = _connection;
                OleDbCommand cmd = _command;
                Bindings bindings = _parameterBindings;
                _connection = null;
                _command = null;
                _parameterBindings = null;

                _isClosed = true;

                DisposeOpenResults();
                _hasRows = false;

                if ((null != cmd) && cmd.canceling) { // MDAC 68964                
                    DisposeNativeMultipleResults();
                    
                    if (null != bindings) {
                        bindings.CloseFromConnection();
                        bindings = null;
                    }
                }
                else {
                    UnsafeNativeMethods.IMultipleResults multipleResults = _imultipleResults;
                    _imultipleResults = null;

                    if (null != multipleResults) {
                        // if we don't have a cmd, same as a cancel (don't call NextResults) which is ADODB behavior

                        try {
                            // tricky code path is an exception is thrown
                            // causing connection to do a ResetState and connection.Close
                            // resulting in OleDbCommand.CloseFromConnection
                            if ((null != cmd) && !cmd.canceling) { // MDAC 71435
                                IntPtr affected = IntPtr.Zero;
                                OleDbException nextResultsFailure = NextResults(multipleResults, null, cmd, out affected);
                                _recordsAffected = AddRecordsAffected(_recordsAffected, affected);
                                if (null != nextResultsFailure) {
                                    throw nextResultsFailure;
                                }
                            }
                        }
                        finally {
                            if (null != multipleResults) {
                                Marshal.ReleaseComObject(multipleResults);
                            }
                        }
                    }
                }

                if ((null != cmd) && (0 == _depth)) {
                    // return bindings back to the cmd after closure of root DataReader
                    cmd.CloseFromDataReader(bindings); // MDAC 52283
                }

                if (null != con) {
                    con.RemoveWeakReference(this);

                    // if the DataReader is Finalized it will not close the connection
                    if (IsCommandBehavior(CommandBehavior.CloseConnection)) {
                        con.Close();
                    }
                }

                // release unmanaged objects
                RowHandleBuffer rowHandleNativeBuffer = _rowHandleNativeBuffer;
                _rowHandleNativeBuffer = null;
                if (null != rowHandleNativeBuffer) {
                    rowHandleNativeBuffer.Dispose();
                }
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }
        }
 private void CreateAccessors(bool allowMultipleAccessor)
 {
     Bindings[] bindingsArray = this.CreateBindingsFromMetaData(allowMultipleAccessor);
     System.Data.Common.UnsafeNativeMethods.IAccessor iaccessor = this.IAccessor();
     for (int i = 0; i < bindingsArray.Length; i++)
     {
         OleDbHResult hr = bindingsArray[i].CreateAccessor(iaccessor, 2);
         if (hr < OleDbHResult.S_OK)
         {
             this.ProcessResults(hr);
         }
     }
     if (IntPtr.Zero == this._rowHandleFetchCount)
     {
         this._rowHandleFetchCount = new IntPtr(1);
         object propertyValue = this.GetPropertyValue(0x49);
         if (propertyValue is int)
         {
             this._rowHandleFetchCount = new IntPtr((int) propertyValue);
             if ((ADP.PtrZero == this._rowHandleFetchCount) || (20 <= ((int) this._rowHandleFetchCount)))
             {
                 this._rowHandleFetchCount = new IntPtr(20);
             }
         }
         else if (propertyValue is long)
         {
             this._rowHandleFetchCount = new IntPtr((long) propertyValue);
             if ((ADP.PtrZero == this._rowHandleFetchCount) || (20L <= ((long) this._rowHandleFetchCount)))
             {
                 this._rowHandleFetchCount = new IntPtr(20);
             }
         }
     }
     if (this._rowHandleNativeBuffer == null)
     {
         this._rowHandleNativeBuffer = new RowHandleBuffer(this._rowHandleFetchCount);
     }
 }
        private void CreateAccessors(bool allowMultipleAccessor) {
            Debug.Assert(null == _bindings, "CreateAccessor: dbBindings already exists");
            Debug.Assert(null != _irowset, "CreateAccessor: no IRowset available");
            Debug.Assert(null != _metadata && 0 < _metadata.Length, "no columns");

            Bindings[] dbBindings = CreateBindingsFromMetaData(allowMultipleAccessor);

            UnsafeNativeMethods.IAccessor iaccessor = IAccessor();
            for (int i = 0; i < dbBindings.Length; ++i) {
                OleDbHResult hr = dbBindings[i].CreateAccessor(iaccessor, ODB.DBACCESSOR_ROWDATA);
                if (hr < 0) {
                    ProcessResults(hr);
                }
            }

            if (IntPtr.Zero == _rowHandleFetchCount) {
                _rowHandleFetchCount = new IntPtr(1);

                object maxRows = GetPropertyValue(ODB.DBPROP_MAXROWS);
                if (maxRows is Int32) {
                    _rowHandleFetchCount = new IntPtr((int) maxRows);
                    if ((ADP.PtrZero == _rowHandleFetchCount) || (20 <= (int)_rowHandleFetchCount)) {
                        _rowHandleFetchCount = new IntPtr(20);
                    }
                }
                else if (maxRows is Int64) {
                    _rowHandleFetchCount = new IntPtr((long) maxRows);
                    if ((ADP.PtrZero == _rowHandleFetchCount) || (20 <= (long)_rowHandleFetchCount)) {
                        _rowHandleFetchCount = new IntPtr(20);
                    }
                }
            }
            if (null == _rowHandleNativeBuffer) {
                _rowHandleNativeBuffer = new RowHandleBuffer(_rowHandleFetchCount);
            }
        }
 public override void Close()
 {
     IntPtr ptr2;
     Bid.ScopeEnter(out ptr2, "<oledb.OleDbDataReader.Close|API> %d#\n", this.ObjectID);
     try
     {
         OleDbConnection connection = this._connection;
         OleDbCommand command = this._command;
         Bindings bindings = this._parameterBindings;
         this._connection = null;
         this._command = null;
         this._parameterBindings = null;
         this._isClosed = true;
         this.DisposeOpenResults();
         this._hasRows = false;
         if ((command != null) && command.canceling)
         {
             this.DisposeNativeMultipleResults();
             if (bindings != null)
             {
                 bindings.CloseFromConnection();
                 bindings = null;
             }
         }
         else
         {
             UnsafeNativeMethods.IMultipleResults imultipleResults = this._imultipleResults;
             this._imultipleResults = null;
             if (imultipleResults != null)
             {
                 try
                 {
                     if ((command != null) && !command.canceling)
                     {
                         IntPtr zero = IntPtr.Zero;
                         OleDbException exception = NextResults(imultipleResults, null, command, out zero);
                         this._recordsAffected = AddRecordsAffected(this._recordsAffected, zero);
                         if (exception != null)
                         {
                             throw exception;
                         }
                     }
                 }
                 finally
                 {
                     if (imultipleResults != null)
                     {
                         Marshal.ReleaseComObject(imultipleResults);
                     }
                 }
             }
         }
         if ((command != null) && (this._depth == 0))
         {
             command.CloseFromDataReader(bindings);
         }
         if (connection != null)
         {
             connection.RemoveWeakReference(this);
             if (this.IsCommandBehavior(CommandBehavior.CloseConnection))
             {
                 connection.Close();
             }
         }
         RowHandleBuffer buffer = this._rowHandleNativeBuffer;
         this._rowHandleNativeBuffer = null;
         if (buffer != null)
         {
             buffer.Dispose();
         }
     }
     finally
     {
         Bid.ScopeLeave(ref ptr2);
     }
 }