Example #1
0
        private bool PropertiesOnCommand(bool throwNotSupported)
        {
            if (null != _icommandText)
            {
                return(true);
            }
            Debug.Assert(!_isPrepared, "null command isPrepared");

            OleDbConnection connection = _connection;

            if (null == connection)
            {
                connection.CheckStateOpen(ODB.Properties);
            }
            if (!_trackingForClose)
            {
                _trackingForClose = true;
                connection.AddWeakReference(this, OleDbReferenceCollection.CommandTag);
            }
            _icommandText = connection.ICommandText();

            if (null == _icommandText)
            {
                if (throwNotSupported || HasParameters())
                {
                    throw ODB.CommandTextNotSupported(connection.Provider, null);
                }
                return(false);
            }

            using (DBPropSet propSet = CommandPropertySets())
            {
                if (null != propSet)
                {
                    UnsafeNativeMethods.ICommandProperties icommandProperties = ICommandProperties();
                    OleDbHResult hr = icommandProperties.SetProperties(propSet.PropertySetCount, propSet);

                    if (hr < 0)
                    {
                        SafeNativeMethods.Wrapper.ClearErrorInfo();
                    }
                }
            }
            return(true);
        }
 private bool PropertiesOnCommand(bool throwNotSupported)
 {
     if (this._icommandText == null)
     {
         OleDbConnection connection = this._connection;
         if (connection == null)
         {
             connection.CheckStateOpen("Properties");
         }
         if (!this._trackingForClose)
         {
             this._trackingForClose = true;
             connection.AddWeakReference(this, 1);
         }
         this._icommandText = connection.ICommandText();
         if (this._icommandText == null)
         {
             if (throwNotSupported || this.HasParameters())
             {
                 throw ODB.CommandTextNotSupported(connection.Provider, null);
             }
             return(false);
         }
         using (DBPropSet set = this.CommandPropertySets())
         {
             if (set != null)
             {
                 System.Data.Common.UnsafeNativeMethods.ICommandProperties properties = this.ICommandProperties();
                 Bid.Trace("<oledb.ICommandProperties.SetProperties|API|OLEDB> %d#\n", this.ObjectID);
                 OleDbHResult result = properties.SetProperties(set.PropertySetCount, set);
                 Bid.Trace("<oledb.ICommandProperties.SetProperties|API|OLEDB|RET> %08X{HRESULT}\n", result);
                 if (result < OleDbHResult.S_OK)
                 {
                     SafeNativeMethods.Wrapper.ClearErrorInfo();
                 }
             }
         }
     }
     return(true);
 }
Example #3
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);
        }