Пример #1
0
        void AssertSchemaMapping(SchemaMapping mapping) {
#if DEBUG
            if (_debugHookNonEmptySelectCommand) {
                Debug.Assert(mapping != null && mapping.DataValues != null && mapping.DataTable != null, "Debug hook specifies that non-empty results are not expected");
            }
#endif
        }
Пример #2
0
        private void UpdateRowExecute(RowUpdatedEventArgs rowUpdatedEvent, IDbCommand dataCommand, StatementType cmdIndex)
        {
            Debug.Assert(null != rowUpdatedEvent, "null rowUpdatedEvent");
            Debug.Assert(null != dataCommand, "null dataCommand");
            Debug.Assert(rowUpdatedEvent.Command == dataCommand, "dataCommand differs from rowUpdatedEvent");

            bool insertAcceptChanges = true;
            UpdateRowSource updatedRowSource = dataCommand.UpdatedRowSource;
            if ((StatementType.Delete == cmdIndex) || (0 == (UpdateRowSource.FirstReturnedRecord & updatedRowSource)))
            {
                int recordsAffected = dataCommand.ExecuteNonQuery();
                rowUpdatedEvent.AdapterInit(recordsAffected);
            }
            else if ((StatementType.Insert == cmdIndex) || (StatementType.Update == cmdIndex))
            {
                // we only care about the first row of the first result
                using (IDataReader dataReader = dataCommand.ExecuteReader(CommandBehavior.SequentialAccess))
                {
                    DataReaderContainer readerHandler = DataReaderContainer.Create(dataReader, ReturnProviderSpecificTypes);
                    try
                    {
                        bool getData = false;
                        do
                        {
                            // advance to the first row returning result set
                            // determined by actually having columns in the result set
                            if (0 < readerHandler.FieldCount)
                            {
                                getData = true;
                                break;
                            }
                        } while (dataReader.NextResult());

                        if (getData && (0 != dataReader.RecordsAffected))
                        {
                            SchemaMapping mapping = new SchemaMapping(this, null, rowUpdatedEvent.Row.Table, readerHandler, false, SchemaType.Mapped, rowUpdatedEvent.TableMapping.SourceTable, true, null, null);

                            if ((null != mapping.DataTable) && (null != mapping.DataValues))
                            {
                                if (dataReader.Read())
                                {
                                    if ((StatementType.Insert == cmdIndex) && insertAcceptChanges)
                                    {
                                        rowUpdatedEvent.Row.AcceptChanges();
                                        insertAcceptChanges = false;
                                    }
                                    mapping.ApplyToDataRow(rowUpdatedEvent.Row);
                                }
                            }
                        }
                    }
                    finally
                    {
                        // using Close which can optimize its { while(dataReader.NextResult()); } loop
                        dataReader.Close();

                        // RecordsAffected is available after Close, but don't trust it after Dispose
                        int recordsAffected = dataReader.RecordsAffected;
                        rowUpdatedEvent.AdapterInit(recordsAffected);
                    }
                }
            }
            else
            {
                // StatementType.Select, StatementType.Batch
                Debug.Assert(false, "unexpected StatementType");
            }

            // map the parameter results to the dataSet
            if (((StatementType.Insert == cmdIndex) || (StatementType.Update == cmdIndex))
                && (0 != (UpdateRowSource.OutputParameters & updatedRowSource)) && (0 != rowUpdatedEvent.RecordsAffected))
            {
                if ((StatementType.Insert == cmdIndex) && insertAcceptChanges)
                {
                    rowUpdatedEvent.Row.AcceptChanges();
                }
                ParameterOutput(dataCommand.Parameters, rowUpdatedEvent.Row, rowUpdatedEvent.TableMapping);
            }

            // Only error if RecordsAffect == 0, not -1.  A value of -1 means no count was received from server,
            // do not error in that situation (means 'set nocount on' was executed on server).
            switch (rowUpdatedEvent.Status)
            {
                case UpdateStatus.Continue:
                    switch (cmdIndex)
                    {
                        case StatementType.Update:
                        case StatementType.Delete:
                            if (0 == rowUpdatedEvent.RecordsAffected)
                            {
                                Debug.Assert(null == rowUpdatedEvent.Errors, "Continue - but contains an exception");
                                rowUpdatedEvent.Errors = ADP.UpdateConcurrencyViolation(cmdIndex, rowUpdatedEvent.RecordsAffected, 1, new DataRow[] { rowUpdatedEvent.Row });
                                rowUpdatedEvent.Status = UpdateStatus.ErrorsOccurred;
                            }
                            break;
                    }
                    break;
            }
        }
Пример #3
0
 private int FillLoadDataRow(SchemaMapping mapping) {
     int rowsAddedToDataSet = 0;
     DataReaderContainer dataReader = mapping.DataReader;
     if (_hasFillErrorHandler) {
         while (dataReader.Read()) { // read remaining rows of first and subsequent resultsets
             try {
                 // only try-catch if a FillErrorEventHandler is registered so that
                 // in the default case we get the full callstack from users
                 mapping.LoadDataRowWithClear();
                 rowsAddedToDataSet++;
             }
             catch(Exception e) {
                 // 
                 if (!ADP.IsCatchableExceptionType(e)) {
                     throw;
                 }                    
                 ADP.TraceExceptionForCapture(e);
                 OnFillErrorHandler(e, mapping.DataTable, mapping.DataValues);
             }
         }
     }
     else {
         while (dataReader.Read()) { // read remaining rows of first and subsequent resultset
             mapping.LoadDataRow();
             rowsAddedToDataSet++;
         }
     }
     return rowsAddedToDataSet;
 }
Пример #4
0
        private int FillLoadDataRowChunk(SchemaMapping mapping, int startRecord, int maxRecords) {
            DataReaderContainer dataReader = mapping.DataReader;

            while (0 < startRecord) {
                if (!dataReader.Read()) {
                    // there are no more rows on first resultset
                    return 0;
                }
                --startRecord;
            }

            int rowsAddedToDataSet = 0;
            if (0 < maxRecords) {                
                while ((rowsAddedToDataSet < maxRecords) && dataReader.Read()) {
                    if (_hasFillErrorHandler) {
                        try {
                            mapping.LoadDataRowWithClear();
                            rowsAddedToDataSet++;
                        }
                        catch(Exception e) {
                            // 
                            if (!ADP.IsCatchableExceptionType(e)) {
                                throw;
                            }
                            ADP.TraceExceptionForCapture(e);
                            OnFillErrorHandler(e, mapping.DataTable, mapping.DataValues);                            
                        }
                    }
                    else {
                        mapping.LoadDataRow();
                        rowsAddedToDataSet++;
                    }
                }
                // skip remaining rows of the first resultset
            }
            else {
                rowsAddedToDataSet = FillLoadDataRow(mapping);
            }
            return rowsAddedToDataSet;
        }
Пример #5
0
        internal object FillSchemaFromReader(DataSet dataset, DataTable datatable, SchemaType schemaType, string srcTable, IDataReader dataReader) {
            DataTable[] dataTables = null;
            int schemaCount = 0;
            do {
                DataReaderContainer readerHandler = DataReaderContainer.Create(dataReader, ReturnProviderSpecificTypes);

                AssertReaderHandleFieldCount(readerHandler);
                if (0 >= readerHandler.FieldCount) {
                    continue;
                }
                string tmp = null;
                if (null != dataset) {
                    tmp = DataAdapter.GetSourceTableName(srcTable, schemaCount);
                    schemaCount++; // don't increment if no SchemaTable ( a non-row returning result )
                }
                
                SchemaMapping mapping = new SchemaMapping(this, dataset, datatable, readerHandler, true, schemaType, tmp, false, null, null);

                if (null != datatable) {
                    // do not read remaining results in single DataTable case
                    return mapping.DataTable;
                }
                else if (null != mapping.DataTable) {
                    if (null == dataTables) {
                        dataTables = new DataTable[1] { mapping.DataTable };
                    }
                    else {
                        dataTables = DataAdapter.AddDataTableToArray(dataTables, mapping.DataTable);
                    }
                }
            } while (dataReader.NextResult()); // FillSchema does not capture errors for FillError event

            object value = dataTables;
            if ((null == value) && (null == datatable)) { // WebData 101757
                value = new DataTable[0];
            }
            return value; // null if datatable had no results
        }
 internal object FillSchemaFromReader(DataSet dataset, DataTable datatable, SchemaType schemaType, string srcTable, IDataReader dataReader)
 {
     DataTable[] tables = null;
     int index = 0;
     do
     {
         DataReaderContainer container = DataReaderContainer.Create(dataReader, this.ReturnProviderSpecificTypes);
         if (0 < container.FieldCount)
         {
             string sourceTableName = null;
             if (dataset != null)
             {
                 sourceTableName = GetSourceTableName(srcTable, index);
                 index++;
             }
             SchemaMapping mapping = new SchemaMapping(this, dataset, datatable, container, true, schemaType, sourceTableName, false, null, null);
             if (datatable != null)
             {
                 return mapping.DataTable;
             }
             if (mapping.DataTable != null)
             {
                 if (tables == null)
                 {
                     tables = new DataTable[] { mapping.DataTable };
                 }
                 else
                 {
                     tables = AddDataTableToArray(tables, mapping.DataTable);
                 }
             }
         }
     }
     while (dataReader.NextResult());
     object obj2 = tables;
     if ((obj2 == null) && (datatable == null))
     {
         obj2 = new DataTable[0];
     }
     return obj2;
 }
 private int FillLoadDataRowChunk(SchemaMapping mapping, int startRecord, int maxRecords)
 {
     DataReaderContainer dataReader = mapping.DataReader;
     while (0 < startRecord)
     {
         if (!dataReader.Read())
         {
             return 0;
         }
         startRecord--;
     }
     int num = 0;
     if (0 >= maxRecords)
     {
         return this.FillLoadDataRow(mapping);
     }
     while ((num < maxRecords) && dataReader.Read())
     {
         if (this._hasFillErrorHandler)
         {
             try
             {
                 mapping.LoadDataRowWithClear();
                 num++;
             }
             catch (Exception exception)
             {
                 if (!ADP.IsCatchableExceptionType(exception))
                 {
                     throw;
                 }
                 ADP.TraceExceptionForCapture(exception);
                 this.OnFillErrorHandler(exception, mapping.DataTable, mapping.DataValues);
             }
         }
         else
         {
             mapping.LoadDataRow();
             num++;
         }
     }
     return num;
 }
 private int FillLoadDataRow(SchemaMapping mapping)
 {
     int num = 0;
     DataReaderContainer dataReader = mapping.DataReader;
     if (!this._hasFillErrorHandler)
     {
         while (dataReader.Read())
         {
             mapping.LoadDataRow();
             num++;
         }
         return num;
     }
     while (dataReader.Read())
     {
         try
         {
             mapping.LoadDataRowWithClear();
             num++;
             continue;
         }
         catch (Exception exception)
         {
             if (!ADP.IsCatchableExceptionType(exception))
             {
                 throw;
             }
             ADP.TraceExceptionForCapture(exception);
             this.OnFillErrorHandler(exception, mapping.DataTable, mapping.DataValues);
             continue;
         }
     }
     return num;
 }
 private void UpdateRowExecute(RowUpdatedEventArgs rowUpdatedEvent, IDbCommand dataCommand, StatementType cmdIndex)
 {
     bool flag = true;
     UpdateRowSource updatedRowSource = dataCommand.UpdatedRowSource;
     if ((StatementType.Delete == cmdIndex) || ((UpdateRowSource.FirstReturnedRecord & updatedRowSource) == UpdateRowSource.None))
     {
         int recordsAffected = dataCommand.ExecuteNonQuery();
         rowUpdatedEvent.AdapterInit(recordsAffected);
     }
     else if ((StatementType.Insert == cmdIndex) || (StatementType.Update == cmdIndex))
     {
         using (IDataReader reader = dataCommand.ExecuteReader(CommandBehavior.SequentialAccess))
         {
             DataReaderContainer dataReader = DataReaderContainer.Create(reader, this.ReturnProviderSpecificTypes);
             try
             {
                 bool flag2 = false;
                 do
                 {
                     if (0 < dataReader.FieldCount)
                     {
                         flag2 = true;
                         break;
                     }
                 }
                 while (reader.NextResult());
                 if (flag2 && (reader.RecordsAffected != 0))
                 {
                     SchemaMapping mapping = new SchemaMapping(this, null, rowUpdatedEvent.Row.Table, dataReader, false, SchemaType.Mapped, rowUpdatedEvent.TableMapping.SourceTable, true, null, null);
                     if (((mapping.DataTable != null) && (mapping.DataValues != null)) && reader.Read())
                     {
                         if ((StatementType.Insert == cmdIndex) && flag)
                         {
                             rowUpdatedEvent.Row.AcceptChanges();
                             flag = false;
                         }
                         mapping.ApplyToDataRow(rowUpdatedEvent.Row);
                     }
                 }
             }
             finally
             {
                 reader.Close();
                 int num = reader.RecordsAffected;
                 rowUpdatedEvent.AdapterInit(num);
             }
         }
     }
     if (((StatementType.Insert == cmdIndex) || (StatementType.Update == cmdIndex)) && (((UpdateRowSource.OutputParameters & updatedRowSource) != UpdateRowSource.None) && (rowUpdatedEvent.RecordsAffected != 0)))
     {
         if ((StatementType.Insert == cmdIndex) && flag)
         {
             rowUpdatedEvent.Row.AcceptChanges();
         }
         this.ParameterOutput(dataCommand.Parameters, rowUpdatedEvent.Row, rowUpdatedEvent.TableMapping);
     }
     if (rowUpdatedEvent.Status == UpdateStatus.Continue)
     {
         switch (cmdIndex)
         {
             case StatementType.Update:
             case StatementType.Delete:
                 if (rowUpdatedEvent.RecordsAffected == 0)
                 {
                     rowUpdatedEvent.Errors = ADP.UpdateConcurrencyViolation(cmdIndex, rowUpdatedEvent.RecordsAffected, 1, new DataRow[] { rowUpdatedEvent.Row });
                     rowUpdatedEvent.Status = UpdateStatus.ErrorsOccurred;
                 }
                 return;
         }
     }
 }