コード例 #1
0
ファイル: OleDbCommand.cs プロジェクト: yuyixiaoxiang/runtime
        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);
        }
コード例 #2
0
        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);
        }