Ejemplo n.º 1
0
        private void DisposeNativeMultipleResults() {
            UnsafeNativeMethods.IMultipleResults imultipleResults = _imultipleResults;
            _imultipleResults = null;

            if (null != imultipleResults) {
                Marshal.ReleaseComObject(imultipleResults);
            }
        }
Ejemplo n.º 2
0
 internal void InitializeIMultipleResults(object result) {
     Initialize();
     _imultipleResults = (UnsafeNativeMethods.IMultipleResults) result; // maybe null if no results
 }
Ejemplo n.º 3
0
        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);
            }
        }
Ejemplo n.º 4
0
        private OleDbDataReader ExecuteReaderInternal(CommandBehavior behavior, string method)
        {
            OleDbDataReader dataReader         = null;
            OleDbException  nextResultsFailure = null;
            int             state = ODB.InternalStateClosed;

            try
            {
                ValidateConnectionAndTransaction(method);

                if (0 != (CommandBehavior.SingleRow & behavior))
                {
                    // CommandBehavior.SingleRow implies CommandBehavior.SingleResult
                    behavior |= CommandBehavior.SingleResult;
                }

                object executeResult;
                int    resultType;

                switch (CommandType)
                {
                case 0:     // uninitialized CommandType.Text
                case CommandType.Text:
                case CommandType.StoredProcedure:
                    resultType = ExecuteCommand(behavior, out executeResult);
                    break;

                case CommandType.TableDirect:
                    resultType = ExecuteTableDirect(behavior, out executeResult);
                    break;

                default:
                    throw ADP.InvalidCommandType(CommandType);
                }

                if (_executeQuery)
                {
                    try
                    {
                        dataReader = new OleDbDataReader(_connection, this, 0, this.commandBehavior);

                        switch (resultType)
                        {
                        case ODB.ExecutedIMultipleResults:
                            dataReader.InitializeIMultipleResults(executeResult);
                            dataReader.NextResult();
                            break;

                        case ODB.ExecutedIRowset:
                            dataReader.InitializeIRowset(executeResult, ChapterHandle.DB_NULL_HCHAPTER, _recordsAffected);
                            dataReader.BuildMetaInfo();
                            dataReader.HasRowsRead();
                            break;

                        case ODB.ExecutedIRow:
                            dataReader.InitializeIRow(executeResult, _recordsAffected);
                            dataReader.BuildMetaInfo();
                            break;

                        case ODB.PrepareICommandText:
                            if (!_isPrepared)
                            {
                                PrepareCommandText(2);
                            }
                            OleDbDataReader.GenerateSchemaTable(dataReader, _icommandText, behavior);
                            break;

                        default:
                            Debug.Assert(false, "ExecuteReaderInternal: unknown result type");
                            break;
                        }
                        executeResult  = null;
                        _hasDataReader = true;
                        _connection.AddWeakReference(dataReader, OleDbReferenceCollection.DataReaderTag);

                        // command stays in the executing state until the connection
                        // has a datareader to track for it being closed
                        state = ODB.InternalStateOpen;
                    }
                    finally
                    {
                        if (ODB.InternalStateOpen != state)
                        {
                            this.canceling = true;
                            if (null != dataReader)
                            {
                                ((IDisposable)dataReader).Dispose();
                                dataReader = null;
                            }
                        }
                    }
                    Debug.Assert(null != dataReader, "ExecuteReader should never return a null DataReader");
                }
                else
                { // optimized code path for ExecuteNonQuery to not create a OleDbDataReader object
                    try
                    {
                        if (ODB.ExecutedIMultipleResults == resultType)
                        {
                            UnsafeNativeMethods.IMultipleResults multipleResults = (UnsafeNativeMethods.IMultipleResults)executeResult;

                            // may cause a Connection.ResetState which closes connection
                            nextResultsFailure = OleDbDataReader.NextResults(multipleResults, _connection, this, out _recordsAffected);
                        }
                    }
                    finally
                    {
                        try
                        {
                            if (null != executeResult)
                            {
                                Marshal.ReleaseComObject(executeResult);
                                executeResult = null;
                            }
                            CloseFromDataReader(ParameterBindings);
                        }
                        catch (Exception e)
                        {
                            // UNDONE - should not be catching all exceptions!!!
                            if (!ADP.IsCatchableExceptionType(e))
                            {
                                throw;
                            }
                            if (null != nextResultsFailure)
                            {
                                nextResultsFailure = new OleDbException(nextResultsFailure, e);
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                }
            }
            finally
            { // finally clear executing state
                try
                {
                    if ((null == dataReader) && (ODB.InternalStateOpen != state))
                    {
                        ParameterCleanup();
                    }
                }
                catch (Exception e)
                {
                    // UNDONE - should not be catching all exceptions!!!
                    if (!ADP.IsCatchableExceptionType(e))
                    {
                        throw;
                    }
                    if (null != nextResultsFailure)
                    {
                        nextResultsFailure = new OleDbException(nextResultsFailure, e);
                    }
                    else
                    {
                        throw;
                    }
                }
                if (null != nextResultsFailure)
                {
                    throw nextResultsFailure;
                }
            }
            return(dataReader);
        }
 private void DisposeNativeMultipleResults()
 {
     UnsafeNativeMethods.IMultipleResults o = this._imultipleResults;
     this._imultipleResults = null;
     if (o != null)
     {
         Marshal.ReleaseComObject(o);
     }
 }
 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);
     }
 }
 internal void InitializeIMultipleResults(object result)
 {
     this.Initialize();
     this._imultipleResults = (UnsafeNativeMethods.IMultipleResults) result;
 }
        private OleDbDataReader ExecuteReaderInternal(CommandBehavior behavior, string method)
        {
            OleDbDataReader dataReader = null;
            OleDbException  previous   = null;
            int             num2       = 0;

            try
            {
                object obj2;
                int    num;
                this.ValidateConnectionAndTransaction(method);
                if ((CommandBehavior.SingleRow & behavior) != CommandBehavior.Default)
                {
                    behavior |= CommandBehavior.SingleResult;
                }
                switch (this.CommandType)
                {
                case ((System.Data.CommandType) 0):
                case System.Data.CommandType.Text:
                case System.Data.CommandType.StoredProcedure:
                    num = this.ExecuteCommand(behavior, out obj2);
                    break;

                case System.Data.CommandType.TableDirect:
                    num = this.ExecuteTableDirect(behavior, out obj2);
                    break;

                default:
                    throw ADP.InvalidCommandType(this.CommandType);
                }
                if (this._executeQuery)
                {
                    try
                    {
                        dataReader = new OleDbDataReader(this._connection, this, 0, this.commandBehavior);
                        switch (num)
                        {
                        case 0:
                            dataReader.InitializeIMultipleResults(obj2);
                            dataReader.NextResult();
                            break;

                        case 1:
                            dataReader.InitializeIRowset(obj2, ChapterHandle.DB_NULL_HCHAPTER, this._recordsAffected);
                            dataReader.BuildMetaInfo();
                            dataReader.HasRowsRead();
                            break;

                        case 2:
                            dataReader.InitializeIRow(obj2, this._recordsAffected);
                            dataReader.BuildMetaInfo();
                            break;

                        case 3:
                            if (!this._isPrepared)
                            {
                                this.PrepareCommandText(2);
                            }
                            OleDbDataReader.GenerateSchemaTable(dataReader, this._icommandText, behavior);
                            break;
                        }
                        obj2 = null;
                        this._hasDataReader = true;
                        this._connection.AddWeakReference(dataReader, 2);
                        num2 = 1;
                        return(dataReader);
                    }
                    finally
                    {
                        if (1 != num2)
                        {
                            this.canceling = true;
                            if (dataReader != null)
                            {
                                dataReader.Dispose();
                                dataReader = null;
                            }
                        }
                    }
                }
                try
                {
                    if (num == 0)
                    {
                        UnsafeNativeMethods.IMultipleResults imultipleResults = (UnsafeNativeMethods.IMultipleResults)obj2;
                        previous = OleDbDataReader.NextResults(imultipleResults, this._connection, this, out this._recordsAffected);
                    }
                }
                finally
                {
                    try
                    {
                        if (obj2 != null)
                        {
                            Marshal.ReleaseComObject(obj2);
                            obj2 = null;
                        }
                        this.CloseFromDataReader(this.ParameterBindings);
                    }
                    catch (Exception exception3)
                    {
                        if (!ADP.IsCatchableExceptionType(exception3))
                        {
                            throw;
                        }
                        if (previous == null)
                        {
                            throw;
                        }
                        previous = new OleDbException(previous, exception3);
                    }
                }
            }
            finally
            {
                try
                {
                    if ((dataReader == null) && (1 != num2))
                    {
                        this.ParameterCleanup();
                    }
                }
                catch (Exception exception2)
                {
                    if (!ADP.IsCatchableExceptionType(exception2))
                    {
                        throw;
                    }
                    if (previous == null)
                    {
                        throw;
                    }
                    previous = new OleDbException(previous, exception2);
                }
                if (previous != null)
                {
                    throw previous;
                }
            }
            return(dataReader);
        }