internal static void Merge(DataSet targetSet, DataRow[] sourceRows, bool preserveChanges, MissingSchemaAction missingSchemaAction)
		{
			if(targetSet == null)
				throw new ArgumentNullException("targetSet");
			if(sourceRows == null)
				throw new ArgumentNullException("sourceRows");

			bool savedEnfoceConstraints = targetSet.EnforceConstraints;
			targetSet.EnforceConstraints = false;

			ArrayList targetTables = new ArrayList();
			for (int i = 0; i < sourceRows.Length; i++) {
				DataRow row = sourceRows[i];
				DataTable sourceTable = row.Table;
				DataTable targetTable = null;
				if (!AdjustSchema(targetSet, sourceTable, missingSchemaAction,ref targetTable)) {
					return;
				}
				if (targetTable != null) {
					checkColumnTypes(targetTable, row.Table);
					MergeRow(targetTable, row, preserveChanges);
					if (!(targetTables.IndexOf(targetTable) >= 0)) {
						targetTables.Add(targetTable);
					}
				}
			}

			targetSet.EnforceConstraints = savedEnfoceConstraints;

			foreach(DataTable table in targetTables) {
				table.ResetIndexes();
			}
		}
        public DataTable GetDataTableBySchemaAction(DataSet dataSet, MissingSchemaAction schemaAction)
        {
            if (dataSet == null)
            {
                throw ADP.ArgumentNull("dataSet");
            }
            string dataSetTable = this.DataSetTable;
            if (ADP.IsEmpty(dataSetTable))
            {
                return null;
            }
            DataTableCollection tables = dataSet.Tables;
            int index = tables.IndexOf(dataSetTable);
            if ((0 <= index) && (index < tables.Count))
            {
                return tables[index];
            }
            switch (schemaAction)
            {
                case MissingSchemaAction.Add:
                case MissingSchemaAction.AddWithKey:
                    return new DataTable(dataSetTable);

                case MissingSchemaAction.Ignore:
                    return null;

                case MissingSchemaAction.Error:
                    throw ADP.MissingTableSchema(dataSetTable, this.SourceTable);
            }
            throw ADP.InvalidMissingSchemaAction(schemaAction);
        }
		internal static void Merge(DataSet targetSet, DataTable sourceTable, bool preserveChanges, MissingSchemaAction missingSchemaAction)
		{
			if(targetSet == null)
				throw new ArgumentNullException("targetSet");
			if(sourceTable == null)
				throw new ArgumentNullException("sourceTable");

			bool savedEnfoceConstraints = targetSet.EnforceConstraints;
			targetSet.EnforceConstraints = false;

			DataTable targetTable = null;
			if (!AdjustSchema(targetSet, sourceTable, missingSchemaAction,ref targetTable)) {
				return;
			}
			if (targetTable != null) {
				checkColumnTypes(targetTable, sourceTable); // check that the colums datatype is the same
				fillData(targetTable, sourceTable, preserveChanges);
			}
			targetSet.EnforceConstraints = savedEnfoceConstraints;
			
			if (!targetSet.EnforceConstraints && targetTable != null) {
				// indexes are still outdated
				targetTable.ResetIndexes();
			}
		}
		protected DataAdapter () 
		{
			acceptChangesDuringFill = true;
			continueUpdateOnError = false;
			missingMappingAction = MissingMappingAction.Passthrough;
			missingSchemaAction = MissingSchemaAction.Add;
			tableMappings = new DataTableMappingCollection ();
		}
Exemple #5
0
        public static DataColumn GetDataColumnBySchemaAction(string sourceColumn, string dataSetColumn, DataTable dataTable, Type dataType, MissingSchemaAction schemaAction)
        {
            if (null == dataTable)
            {
                throw ADP.ArgumentNull(nameof(dataTable));
            }
            if (string.IsNullOrEmpty(dataSetColumn))
            {
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceWarning)
                {
                    Debug.WriteLine("explicit filtering of SourceColumn \"" + sourceColumn + "\"");
                }
#endif
                return null;
            }
            DataColumnCollection columns = dataTable.Columns;
            Debug.Assert(null != columns, "GetDataColumnBySchemaAction: unexpected null DataColumnCollection");

            int index = columns.IndexOf(dataSetColumn);
            if ((0 <= index) && (index < columns.Count))
            {
                DataColumn dataColumn = columns[index];
                Debug.Assert(null != dataColumn, "GetDataColumnBySchemaAction: unexpected null dataColumn");

                if (!string.IsNullOrEmpty(dataColumn.Expression))
                {
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceError)
                    {
                        Debug.WriteLine("schema mismatch on DataColumn \"" + dataSetColumn + "\" which is a computed column");
                    }
#endif
                    throw ADP.ColumnSchemaExpression(sourceColumn, dataSetColumn);
                }
                if ((null == dataType) || (dataType.IsArray == dataColumn.DataType.IsArray))
                {
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceInfo)
                    {
                        Debug.WriteLine("schema match on DataColumn \"" + dataSetColumn + "\"");
                    }
#endif
                    return dataColumn;
                }
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceWarning)
                {
                    Debug.WriteLine("schema mismatch on DataColumn \"" + dataSetColumn + "\" " + dataType.Name + " != " + dataColumn.DataType.Name);
                }
#endif
                throw ADP.ColumnSchemaMismatch(sourceColumn, dataType, dataColumn);
            }

            return CreateDataColumnBySchemaAction(sourceColumn, dataSetColumn, dataTable, dataType, schemaAction);
        }
        private bool _IgnoreNSforTableLookup = false; // Everett Behavior : SQL BU DT 370850

        internal Merger(DataSet dataSet, bool preserveChanges, MissingSchemaAction missingSchemaAction) {
            this.dataSet = dataSet;
            this.preserveChanges = preserveChanges;

            // map AddWithKey -> Add
            if (missingSchemaAction == MissingSchemaAction.AddWithKey)
                this.missingSchemaAction = MissingSchemaAction.Add;
            else
                this.missingSchemaAction = missingSchemaAction;
        }
Exemple #7
0
        private bool _IgnoreNSforTableLookup = false; // Everett Behavior : SQL BU DT 370850

        internal Merger(DataSet dataSet, bool preserveChanges, MissingSchemaAction missingSchemaAction)
        {
            _dataSet = dataSet;
            _preserveChanges = preserveChanges;

            // map AddWithKey -> Add
            _missingSchemaAction = missingSchemaAction == MissingSchemaAction.AddWithKey ?
                MissingSchemaAction.Add :
                missingSchemaAction;
        }
		public DataColumn GetDataColumnBySchemaAction (DataTable dataTable, Type dataType, MissingSchemaAction schemaAction) 
		{
			if (dataTable.Columns.Contains (dataSetColumn))
				return dataTable.Columns [dataSetColumn];
			if (schemaAction == MissingSchemaAction.Ignore)
				return null;
			if (schemaAction == MissingSchemaAction.Error)
				throw new InvalidOperationException (String.Format ("Missing the DataColumn '{0}' in the DataTable '{1}' for the SourceColumn '{2}'", DataSetColumn, dataTable.TableName, SourceColumn));
			return new DataColumn (dataSetColumn, dataType);
		}
Exemple #9
0
        internal Merger(DataTable dataTable, bool preserveChanges, MissingSchemaAction missingSchemaAction)
        {
            _isStandAlonetable = true;
            _dataTable = dataTable;
            _preserveChanges = preserveChanges;

            // map AddWithKey -> Add
            _missingSchemaAction = missingSchemaAction == MissingSchemaAction.AddWithKey ?
                MissingSchemaAction.Add :
                missingSchemaAction;
        }
        internal Merger(DataTable dataTable, bool preserveChanges, MissingSchemaAction missingSchemaAction) {
            isStandAlonetable = true;
            this.dataTable = dataTable;
            this.preserveChanges = preserveChanges;

            // map AddWithKey -> Add
            if (missingSchemaAction == MissingSchemaAction.AddWithKey)
                this.missingSchemaAction = MissingSchemaAction.Add;
            else
                this.missingSchemaAction = missingSchemaAction;
        }
Exemple #11
0
		protected DataAdapter () 
		{
			acceptChangesDuringFill = true;
			continueUpdateOnError = false;
			missingMappingAction = MissingMappingAction.Passthrough;
			missingSchemaAction = MissingSchemaAction.Add;
			tableMappings = new DataTableMappingCollection ();
			acceptChangesDuringUpdate = true;
			fillLoadOption = LoadOption.OverwriteChanges;
			returnProviderSpecificTypes = false;
		}
Exemple #12
0
        // IDataAdapter.MissingSchemaAction
        internal static ArgumentOutOfRangeException InvalidMissingSchemaAction(MissingSchemaAction value)
        {
#if DEBUG
            switch (value)
            {
            case MissingSchemaAction.Add:
            case MissingSchemaAction.Ignore:
            case MissingSchemaAction.Error:
            case MissingSchemaAction.AddWithKey:
                Debug.Fail("valid MissingSchemaAction " + value.ToString());
                break;
            }
#endif
            return(InvalidEnumerationValue(typeof(MissingSchemaAction), (int)value));
        }
Exemple #13
0
        internal Merger(DataSet dataSet, bool preserveChanges, MissingSchemaAction missingSchemaAction)
        {
            this.dataSet         = dataSet;
            this.preserveChanges = preserveChanges;

            // map AddWithKey -> Add
            if (missingSchemaAction == MissingSchemaAction.AddWithKey)
            {
                this.missingSchemaAction = MissingSchemaAction.Add;
            }
            else
            {
                this.missingSchemaAction = missingSchemaAction;
            }
        }
		internal static void Merge(DataSet targetSet, DataSet sourceSet, bool preserveChanges, MissingSchemaAction missingSchemaAction)
		{
			if(targetSet == null)
				throw new ArgumentNullException("targetSet");
			if(sourceSet == null)
				throw new ArgumentNullException("sourceSet");

			bool prevEC = targetSet.EnforceConstraints;
			targetSet.EnforceConstraints = false;
			foreach (DataTable t in sourceSet.Tables)
				MergeManager.Merge(targetSet, t, preserveChanges, missingSchemaAction);

			AdjustSchema(targetSet,sourceSet,missingSchemaAction);
			targetSet.EnforceConstraints = prevEC;
		}
 public DataTable GetDataTableBySchemaAction(DataSet dataSet, MissingSchemaAction schemaAction)
 {
     if (dataSet.Tables.Contains(DataSetTable))
     {
         return(dataSet.Tables [DataSetTable]);
     }
     if (schemaAction == MissingSchemaAction.Ignore)
     {
         return(null);
     }
     if (schemaAction == MissingSchemaAction.Error)
     {
         throw new InvalidOperationException(String.Format("Missing the '{0} DataTable for the '{1}' SourceTable", DataSetTable, SourceTable));
     }
     return(new DataTable(DataSetTable));
 }
Exemple #16
0
        internal Merger(DataTable dataTable, bool preserveChanges, MissingSchemaAction missingSchemaAction)
        {
            isStandAlonetable    = true;
            this.dataTable       = dataTable;
            this.preserveChanges = preserveChanges;

            // map AddWithKey -> Add
            if (missingSchemaAction == MissingSchemaAction.AddWithKey)
            {
                this.missingSchemaAction = MissingSchemaAction.Add;
            }
            else
            {
                this.missingSchemaAction = missingSchemaAction;
            }
        }
		internal static void Merge(DataSet targetSet, DataTable sourceTable, bool preserveChanges, MissingSchemaAction missingSchemaAction)
		{
			if(targetSet == null)
				throw new ArgumentNullException("targetSet");
			if(sourceTable == null)
				throw new ArgumentNullException("sourceTable");
			if (sourceTable.DataSet == targetSet)
				return;

			bool savedEnfoceConstraints = targetSet.EnforceConstraints;
			targetSet.EnforceConstraints = false;

			DataTable targetTable = null;
			if (!AdjustSchema(targetSet, sourceTable, missingSchemaAction,ref targetTable))
				return;
			if (targetTable != null)
				fillData(targetTable, sourceTable, preserveChanges);
			targetSet.EnforceConstraints = savedEnfoceConstraints;
		}
        private void CloneFrom(DataAdapter from)
        {
            _acceptChangesDuringUpdate            = from._acceptChangesDuringUpdate;
            _acceptChangesDuringUpdateAfterInsert = from._acceptChangesDuringUpdateAfterInsert;
            _continueUpdateOnError       = from._continueUpdateOnError;
            _returnProviderSpecificTypes = from._returnProviderSpecificTypes; // WebData 101795
            _acceptChangesDuringFill     = from._acceptChangesDuringFill;
            _fillLoadOption       = from._fillLoadOption;
            _missingMappingAction = from._missingMappingAction;
            _missingSchemaAction  = from._missingSchemaAction;

            if ((null != from._tableMappings) && (0 < from.TableMappings.Count))
            {
                DataTableMappingCollection parameters = this.TableMappings;
                foreach (object parameter in from.TableMappings)
                {
                    parameters.Add((parameter is ICloneable) ? ((ICloneable)parameter).Clone() : parameter);
                }
            }
        }
 public DataSet GetDataSet(string commandText, CommandType commandType, MissingSchemaAction action)
 {
     try
     {
         SqlCommand cmd = new SqlCommand();
         cmd.CommandTimeout = _commandTimeout;
         DbDataAdapter da = new SqlDataAdapter();
         da.MissingSchemaAction = action;
         DataSet ds = new DataSet();
         cmd.Connection   = (SqlConnection)_CN;
         cmd.CommandType  = commandType;
         cmd.CommandText  = commandText;
         da.SelectCommand = cmd;
         da.Fill(ds);
         return(ds);
     }
     catch (Exception e)
     {
         throw (new RaveHRException(e.Message, e, "DataAccessClass", "GetDataSet"));
     }
 }
Exemple #20
0
        protected virtual DataTable FillSchema(DataTable dataTable, SchemaType schemaType, IDbCommand command, CommandBehavior behavior)
        {
            if (dataTable == null)
            {
                throw new ArgumentNullException("DataTable");
            }

            behavior |= CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo;
            if (command.Connection.State == ConnectionState.Closed)
            {
                command.Connection.Open();
                behavior |= CommandBehavior.CloseConnection;
            }

            IDataReader reader = command.ExecuteReader(behavior);

            try
            {
                string tableName = SetupSchema(schemaType, dataTable.TableName);
                if (tableName != null)
                {
                    // FillSchema should add the KeyInfo unless MissingSchemaAction
                    // is set to Ignore or Error.
                    MissingSchemaAction schemaAction = MissingSchemaAction;
                    if (!(schemaAction == MissingSchemaAction.Ignore ||
                          schemaAction == MissingSchemaAction.Error))
                    {
                        schemaAction = MissingSchemaAction.AddWithKey;
                    }

                    BuildSchema(reader, dataTable, schemaType, schemaAction,
                                MissingMappingAction, TableMappings);
                }
            }
            finally
            {
                reader.Close();
            }
            return(dataTable);
        }
Exemple #21
0
        public static async Task <int> UpdateAsync <TAdapter>(TAdapter self, DataSet dataSet, string srcTable, CancellationToken cancellationToken)
            where TAdapter : ICanUpdateAsync, IAdaSchemaMappingAdapter
        {
            if (dataSet is null)
            {
                throw new ArgumentNullException(nameof(dataSet));
            }
            if (srcTable is null)
            {
                throw new ArgumentNullException(nameof(srcTable));
            }
            if (string.IsNullOrEmpty(srcTable))
            {
                throw new ArgumentException(message: "Update: expected a non-empty SourceTable name.", paramName: nameof(srcTable));
            }

            int rowsAffected = 0;

            DataTableMapping tableMapping = self.GetTableMappingBySchemaAction(sourceTableName: srcTable, dataSetTableName: srcTable, mappingAction: self.UpdateMappingAction);

            Debug.Assert(null != tableMapping, "null TableMapping when MissingMappingAction.Error");

            // the ad-hoc scenario of no dataTable just returns
            // ad-hoc scenario is defined as MissingSchemaAction.Add or MissingSchemaAction.Ignore
            MissingSchemaAction schemaAction = self.UpdateSchemaAction;
            DataTable           dataTable    = tableMapping.GetDataTableBySchemaAction(dataSet, schemaAction);

            if (null != dataTable)
            {
                rowsAffected = await UpdateFromDataTableAsync(self, dataTable, tableMapping, cancellationToken).ConfigureAwait(false);
            }
            else if ((self.TableMappings?.Count ?? 0) == 0 || (-1 == self.TableMappings.IndexOf(tableMapping)))
            {
                //throw error since the user didn't explicitly map this tableName to Ignore.
                throw new InvalidOperationException(string.Format("Update unable to find TableMapping['{0}'] or DataTable '{0}'.", srcTable));
            }

            return(rowsAffected);
        }
        public static DataColumn GetDataColumnBySchemaAction(string sourceColumn, string dataSetColumn, DataTable dataTable, Type dataType, MissingSchemaAction schemaAction)
        {
            if (dataTable == null)
            {
                throw ADP.ArgumentNull("dataTable");
            }
            if (ADP.IsEmpty(dataSetColumn))
            {
                return null;
            }
            DataColumnCollection columns = dataTable.Columns;
            int index = columns.IndexOf(dataSetColumn);
            if ((0 <= index) && (index < columns.Count))
            {
                DataColumn column = columns[index];
                if (!ADP.IsEmpty(column.Expression))
                {
                    throw ADP.ColumnSchemaExpression(sourceColumn, dataSetColumn);
                }
                if ((null != dataType) && (dataType.IsArray != column.DataType.IsArray))
                {
                    throw ADP.ColumnSchemaMismatch(sourceColumn, dataType, column);
                }
                return column;
            }
            switch (schemaAction)
            {
                case MissingSchemaAction.Add:
                case MissingSchemaAction.AddWithKey:
                    return new DataColumn(dataSetColumn, dataType);

                case MissingSchemaAction.Ignore:
                    return null;

                case MissingSchemaAction.Error:
                    throw ADP.ColumnSchemaMissing(dataSetColumn, dataTable.TableName, sourceColumn);
            }
            throw ADP.InvalidMissingSchemaAction(schemaAction);
        }
Exemple #23
0
        public static void DataAdapter()
        {
            SqlConnection  cn         = new SqlConnection();;
            SqlDataAdapter da         = new SqlDataAdapter(selectCommandText: "Select * from Aircraft", selectConnection: cn);
            var            sqlbuilder = new SqlCommandBuilder(da);
            bool           acceptChangesDuringFill   = da.AcceptChangesDuringFill;
            bool           acceptChangesDuringUpdate = da.AcceptChangesDuringUpdate;
            bool           continueUpdateOnError     = da.ContinueUpdateOnError;
            SqlCommand     sqlcmdDelete = da.DeleteCommand;
            SqlCommand     sqlcmdInsert = da.InsertCommand;
            SqlCommand     sqlcmdSelect = da.SelectCommand;
            SqlCommand     sqlcmdUpdate = da.UpdateCommand;
            LoadOption     loadOption   = da.FillLoadOption;

            MissingMappingAction mapingAction          = da.MissingMappingAction; //passthroug 1, ignore 2, error 3
            MissingSchemaAction  schemaAction          = da.MissingSchemaAction;  // add, ignore, error ,AddwithKey
            bool shouldReturnProvider                  = da.ReturnProviderSpecificTypes;
            DataTableMappingCollection dtMapCollection = da.TableMappings;

            //dtMapCollection[0].

            Console.WriteLine("*********DataAdapter**************");
            Console.WriteLine("");
            Console.WriteLine("   acceptChangesDuringFill {0,10}", acceptChangesDuringFill);
            Console.WriteLine("   acceptChangesDuringUpdate {0,10}", acceptChangesDuringUpdate);
            Console.WriteLine("   ContinueUpdateOnError {0,10}", continueUpdateOnError);
            Console.WriteLine("   DeleteCommand {0}", sqlcmdDelete);
            Console.WriteLine("   InsertCommand {0}", sqlcmdInsert);
            Console.WriteLine("   SelectCommand {0}", sqlcmdSelect);
            Console.WriteLine("   UpdateCommand {0}", sqlcmdUpdate);
            Console.WriteLine("   FillLoadOption {0}", loadOption);
            Console.WriteLine("   MissingMappingAction {0}", mapingAction);
            Console.WriteLine("   MissingSchemaAction {0}", schemaAction);
            Console.WriteLine("   ReturnProviderSpecificTypes {0}", shouldReturnProvider);
            Console.WriteLine("   TableMappings {0}", dtMapCollection);
        }
		// adjust the table schema according to the missingschemaaction param.
		// return false if adjusting fails.
		private static bool AdjustSchema(DataSet targetSet, DataTable sourceTable, MissingSchemaAction missingSchemaAction, ref DataTable newTable)
		{
			string tableName = sourceTable.TableName;
			
			// if the source table not exists in the target dataset
			// we act according to the missingschemaaction param.
			int tmp = targetSet.Tables.IndexOf(tableName);
			// we need to check if it is equals names
			if (tmp != -1 && !targetSet.Tables[tmp].TableName.Equals(tableName))
				tmp = -1;
			if (tmp == -1) {
				if (missingSchemaAction == MissingSchemaAction.Ignore) {
					return true;
				}
				if (missingSchemaAction == MissingSchemaAction.Error) {
					throw new ArgumentException("Target DataSet missing definition for "+ tableName + ".");
				}
				
				DataTable cloneTable = (DataTable)sourceTable.Clone();
				targetSet.Tables.Add(cloneTable);
				tableName = cloneTable.TableName;
			}								
			
			DataTable table = targetSet.Tables[tableName];
			
			for (int i = 0; i < sourceTable.Columns.Count; i++) {
				DataColumn sourceColumn = sourceTable.Columns[i];
				// if a column from the source table doesn't exists in the target table
				// we act according to the missingschemaaction param.
				DataColumn targetColumn = table.Columns[sourceColumn.ColumnName];
				if(targetColumn == null) {
					if (missingSchemaAction == MissingSchemaAction.Ignore) {
						continue;
					}
					if (missingSchemaAction == MissingSchemaAction.Error) {
						throw new ArgumentException(("Column '" + sourceColumn.ColumnName + "' does not belong to table Items."));
					}
					
					targetColumn = new DataColumn(sourceColumn.ColumnName, sourceColumn.DataType, sourceColumn.Expression, sourceColumn.ColumnMapping);
					table.Columns.Add(targetColumn);
				}

				if (sourceColumn.Unique) {
					try {
						targetColumn.Unique = sourceColumn.Unique;
					}
					catch(Exception e){
//						Console.WriteLine("targetColumn : {0}   targetTable : {1} ",targetColumn.ColumnName,table.TableName);
						foreach(DataRow row in table.Rows) {
//							Console.WriteLine(row[targetColumn]);
						}
						throw e;
					}
				}

				if(sourceColumn.AutoIncrement) {
					targetColumn.AutoIncrement = sourceColumn.AutoIncrement;
					targetColumn.AutoIncrementSeed = sourceColumn.AutoIncrementSeed;
					targetColumn.AutoIncrementStep = sourceColumn.AutoIncrementStep;
				}
			}

			if (!AdjustPrimaryKeys(table, sourceTable)) {
				return false;
			}

			newTable = table;
			return true;
		}
Exemple #25
0
        private object[] SetupSchemaWithKeyInfo(MissingMappingAction mappingAction, MissingSchemaAction schemaAction, bool gettingData, DataColumn parentChapterColumn, object chapterValue)
        {
            DbSchemaRow[] sortedSchemaRows = DbSchemaRow.GetSortedSchemaRows(this._schemaTable, this._dataReader.ReturnProviderSpecificTypes);
            if (sortedSchemaRows.Length == 0)
            {
                this._dataTable = null;
                return null;
            }
            bool flag = (this._dataTable.PrimaryKey.Length == 0 && this._dataTable.Rows.Count == 0) || 0 == this._dataTable.Columns.Count;
            DataColumn[] array = null;
            int num = 0;
            bool flag2 = true;
            string text = null;
            string text2 = null;
            bool flag3 = false;
            bool flag4 = false;
            int[] array2 = null;
            bool[] array3 = null;
            int num2 = 0;
            object[] result = null;
            List<object> items = null;
            DataColumnCollection columns = this._dataTable.Columns;
            try
            {
                for (int i = 0; i < sortedSchemaRows.Length; i++)
                {
                    DbSchemaRow dbSchemaRow = sortedSchemaRows[i];
                    int unsortedIndex = dbSchemaRow.UnsortedIndex;
                    bool flag5 = false;
                    Type type = dbSchemaRow.DataType;
                    if (null == type)
                    {
                        type = this._dataReader.GetFieldType(i);
                    }
                    if (null == type)
                    {
                        throw new Exception("MissingDataReaderFieldType");
                    }
                    if (typeof(IDataReader).IsAssignableFrom(type))
                    {
                        if (array3 == null)
                        {
                            array3 = new bool[sortedSchemaRows.Length];
                        }
                        flag5 = (array3[unsortedIndex] = true);
                        type = typeof(int);
                    }
                    else
                    {
                        if (typeof(SqlXml).IsAssignableFrom(type))
                        {
                            if (this._xmlMap == null)
                            {
                                this._xmlMap = new int[sortedSchemaRows.Length];
                            }
                            this._xmlMap[i] = 1;
                        }
                        else
                        {
                            if (typeof(XmlReader).IsAssignableFrom(type))
                            {
                                type = typeof(string);
                                if (this._xmlMap == null)
                                {
                                    this._xmlMap = new int[sortedSchemaRows.Length];
                                }
                                this._xmlMap[i] = 2;
                            }
                        }
                    }
                    DataColumn dataColumn = null;
                    if (!dbSchemaRow.IsHidden)
                    {
                        dataColumn = this._tableMapping.GetDataColumn(this._fieldNames[i], type, this._dataTable, mappingAction, schemaAction);
                    }
                    string baseTableName = dbSchemaRow.BaseTableName;
                    if (dataColumn == null)
                    {
                        if (array2 == null)
                        {
                            array2 = this.CreateIndexMap(sortedSchemaRows.Length, unsortedIndex);
                        }
                        array2[unsortedIndex] = -1;
                        if (dbSchemaRow.IsKey && (flag3 || dbSchemaRow.BaseTableName == text))
                        {
                            flag = false;
                            array = null;
                        }
                    }
                    else
                    {
                        if (this._xmlMap != null && this._xmlMap[i] != 0)
                        {
                            if (typeof(SqlXml) == dataColumn.DataType)
                            {
                                this._xmlMap[i] = 1;
                            }
                            else
                            {
                                if (typeof(XmlDocument) == dataColumn.DataType)
                                {
                                    this._xmlMap[i] = 2;
                                }
                                else
                                {
                                    this._xmlMap[i] = 0;
                                    int num3 = 0;
                                    for (int j = 0; j < this._xmlMap.Length; j++)
                                    {
                                        num3 += this._xmlMap[j];
                                    }
                                    if (num3 == 0)
                                    {
                                        this._xmlMap = null;
                                    }
                                }
                            }
                        }
                        if (dbSchemaRow.IsKey && baseTableName != text)
                        {
                            if (text == null)
                            {
                                text = baseTableName;
                            }
                            else
                            {
                                flag3 = true;
                            }
                        }
                        if (flag5)
                        {
                            if (dataColumn.Table == null)
                            {
                                dataColumn.AllowDBNull = false;
                                dataColumn.AutoIncrement = true;
                                dataColumn.ReadOnly = true;
                            }
                            else
                            {
                                if (!dataColumn.AutoIncrement)
                                {
                                    throw new Exception("FillChapterAutoIncrement");
                                }
                            }
                        }
                        else
                        {
                            if (!flag4 && baseTableName != text2 && !string.IsNullOrEmpty(baseTableName))
                            {
                                if (text2 == null)
                                {
                                    text2 = baseTableName;
                                }
                                else
                                {
                                    flag4 = true;
                                }
                            }
                            if (dbSchemaRow.IsAutoIncrement && IsAutoIncrementType(type))
                            {
                                dataColumn.AutoIncrement = true;
                                if (!dbSchemaRow.AllowDBNull)
                                {
                                    dataColumn.AllowDBNull = false;
                                }
                            }
                            if (type == typeof(string))
                            {
                                dataColumn.MaxLength = ((dbSchemaRow.Size > 0) ? dbSchemaRow.Size : -1);
                            }
                            if (dbSchemaRow.IsReadOnly)
                            {
                                dataColumn.ReadOnly = true;
                            }
                            if (!dbSchemaRow.AllowDBNull && (!dbSchemaRow.IsReadOnly || dbSchemaRow.IsKey))
                            {
                                dataColumn.AllowDBNull = false;
                            }
                            if (dbSchemaRow.IsUnique && !dbSchemaRow.IsKey && !type.IsArray)
                            {
                                dataColumn.Unique = true;
                                if (!dbSchemaRow.AllowDBNull)
                                {
                                    dataColumn.AllowDBNull = false;
                                }
                            }
                        }
                        if (dataColumn.Table == null)
                        {
                            this.AddAdditionalProperties(dataColumn, dbSchemaRow.DataRow);
                            this.AddItemToAllowRollback(ref items, dataColumn);
                            columns.Add(dataColumn);
                        }
                        if (flag && dbSchemaRow.IsKey)
                        {
                            if (array == null)
                            {
                                array = new DataColumn[sortedSchemaRows.Length];
                            }
                            array[num++] = dataColumn;
                            if (flag2 && dataColumn.AllowDBNull)
                            {
                                flag2 = false;
                            }
                        }
                        if (array2 != null)
                        {
                            array2[unsortedIndex] = dataColumn.Ordinal;
                        }
                        else
                        {
                            if (unsortedIndex != dataColumn.Ordinal)
                            {
                                array2 = this.CreateIndexMap(sortedSchemaRows.Length, unsortedIndex);
                                array2[unsortedIndex] = dataColumn.Ordinal;
                            }
                        }
                        num2++;
                    }
                }
                bool flag6 = false;
                DataColumn dataColumn2 = null;
                if (chapterValue != null)
                {
                    Type type2 = chapterValue.GetType();
                    dataColumn2 = this._tableMapping.GetDataColumn(this._tableMapping.SourceTable, type2, this._dataTable, mappingAction, schemaAction);
                    if (dataColumn2 != null)
                    {
                        if (dataColumn2.Table == null)
                        {
                            dataColumn2.ReadOnly = true;
                            dataColumn2.AllowDBNull = false;
                            this.AddItemToAllowRollback(ref items, dataColumn2);
                            columns.Add(dataColumn2);
                            flag6 = (null != parentChapterColumn);
                        }
                        num2++;
                    }
                }
                if (0 < num2)
                {
                    if (this._dataSet != null && this._dataTable.DataSet == null)
                    {
                        this.AddItemToAllowRollback(ref items, this._dataTable);
                        this._dataSet.Tables.Add(this._dataTable);
                    }
                    if (flag && array != null)
                    {
                        if (num < array.Length)
                        {
                            array = this.ResizeColumnArray(array, num);
                        }
                        if (flag2)
                        {
                            this._dataTable.PrimaryKey = array;
                        }
                        else
                        {
                            UniqueConstraint uniqueConstraint = new UniqueConstraint("", array);
                            ConstraintCollection constraints = this._dataTable.Constraints;
                            int count = constraints.Count;
                            for (int k = 0; k < count; k++)
                            {
                                if (uniqueConstraint.Equals(constraints[k]))
                                {
                                    uniqueConstraint = null;
                                    break;
                                }
                            }
                            if (uniqueConstraint != null)
                            {
                                constraints.Add(uniqueConstraint);
                            }
                        }
                    }
                    if (!flag4 && !string.IsNullOrEmpty(text2) && string.IsNullOrEmpty(this._dataTable.TableName))
                    {
                        this._dataTable.TableName = text2;
                    }
                    if (gettingData)
                    {
                        this._indexMap = array2;
                        this._chapterMap = array3;
                        result = this.SetupMapping(sortedSchemaRows.Length, columns, dataColumn2, chapterValue);
                    }
                    else
                    {
                        this._mappedMode = -1;
                    }
                }
                else
                {
                    this._dataTable = null;
                }
                if (flag6)
                {
                    this.AddRelation(parentChapterColumn, dataColumn2);
                }
            }
            catch (Exception e)
            {

                this.RollbackAddedItems(items);
                throw e;
            }
            return result;
        }
        [EditorBrowsableAttribute(EditorBrowsableState.Advanced)]   // MDAC 69508
        static public DataColumn GetDataColumnBySchemaAction(string sourceColumn, string dataSetColumn, DataTable dataTable, Type dataType, MissingSchemaAction schemaAction)
        {
            if (null == dataTable)
            {
                throw ADP.ArgumentNull("dataTable");
            }
            if (ADP.IsEmpty(dataSetColumn))
            {
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceWarning)
                {
                    Debug.WriteLine("explicit filtering of SourceColumn \"" + sourceColumn + "\"");
                }
#endif
                return(null);
            }
            DataColumnCollection columns = dataTable.Columns;
            Debug.Assert(null != columns, "GetDataColumnBySchemaAction: unexpected null DataColumnCollection");

            int index = columns.IndexOf(dataSetColumn);
            if ((0 <= index) && (index < columns.Count))
            {
                DataColumn dataColumn = columns[index];
                Debug.Assert(null != dataColumn, "GetDataColumnBySchemaAction: unexpected null dataColumn");

                if (!ADP.IsEmpty(dataColumn.Expression))
                {
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceError)
                    {
                        Debug.WriteLine("schema mismatch on DataColumn \"" + dataSetColumn + "\" which is a computed column");
                    }
#endif
                    throw ADP.ColumnSchemaExpression(sourceColumn, dataSetColumn);
                }
                if ((null == dataType) || (dataType.IsArray == dataColumn.DataType.IsArray))
                {
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceInfo)
                    {
                        Debug.WriteLine("schema match on DataColumn \"" + dataSetColumn + "\"");
                    }
#endif
                    return(dataColumn);
                }
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceWarning)
                {
                    Debug.WriteLine("schema mismatch on DataColumn \"" + dataSetColumn + "\" " + dataType.Name + " != " + dataColumn.DataType.Name);
                }
#endif
                throw ADP.ColumnSchemaMismatch(sourceColumn, dataType, dataColumn);
            }

            return(CreateDataColumnBySchemaAction(sourceColumn, dataSetColumn, dataTable, dataType, schemaAction));
        }
        private object[] SetupSchemaWithKeyInfo(MissingMappingAction mappingAction, MissingSchemaAction schemaAction, bool gettingData, DataColumn parentChapterColumn, object chapterValue) {
            // must sort rows from schema table by ordinal because Jet is sorted by coumn name
            DbSchemaRow[] schemaRows = DbSchemaRow.GetSortedSchemaRows(_schemaTable, _dataReader.ReturnProviderSpecificTypes); // MDAC 60609
            Debug.Assert(null != schemaRows, "SchemaSetup - null DbSchemaRow[]");
            Debug.Assert(_dataReader.FieldCount <= schemaRows.Length, "unexpected fewer rows in Schema than FieldCount");

            if (0 == schemaRows.Length) {
                _dataTable = null;
                return (object[])null;
            }

            // Everett behavior, always add a primary key if a primary key didn't exist before
            // Whidbey behavior, same as Everett unless using LoadOption then add primary key only if no columns previously existed
            bool addPrimaryKeys = (((0 == _dataTable.PrimaryKey.Length) && ((4 <= (int)_loadOption) || (0 == _dataTable.Rows.Count)))
                                    || (0 == _dataTable.Columns.Count)); // MDAC 67033

            DataColumn[] keys = null;
            int keyCount = 0;
            bool isPrimary = true; // assume key info (if any) is about a primary key

            string keyBaseTable = null;
            string commonBaseTable = null;

            bool keyFromMultiTable = false;
            bool commonFromMultiTable = false;

            int[] columnIndexMap = null;
            bool[] chapterIndexMap = null;

            int mappingCount = 0;

            object[] dataValues = null;
            List<object> addedItems = null;
            DataColumnCollection columnCollection = _dataTable.Columns;
            try {
                for(int sortedIndex = 0; sortedIndex < schemaRows.Length; ++sortedIndex) {
                    DbSchemaRow schemaRow = schemaRows[sortedIndex];

                    int unsortedIndex = schemaRow.UnsortedIndex; // MDAC 67050

                    bool ischapter = false;
                    Type fieldType = schemaRow.DataType;
                    if (null == fieldType) {
                        fieldType = _dataReader.GetFieldType(sortedIndex);
                    }
                    if (null == fieldType) {
                        throw ADP.MissingDataReaderFieldType(sortedIndex);
                    }

                    // if IDataReader, hierarchy exists and we will use an Int32,AutoIncrementColumn in this table
                    if (typeof(IDataReader).IsAssignableFrom(fieldType)) {
                        if (null == chapterIndexMap) {
                            chapterIndexMap = new bool[schemaRows.Length];
                        }
                        chapterIndexMap[unsortedIndex] = ischapter = true;
                        fieldType = typeof(Int32);
                    }
                    else if (typeof(System.Data.SqlTypes.SqlXml).IsAssignableFrom(fieldType)) {
                        if (null == _xmlMap) {
                            _xmlMap = new int[schemaRows.Length];
                        }
                        _xmlMap[sortedIndex] = SqlXml;
                    }
                    else if (typeof(System.Xml.XmlReader).IsAssignableFrom(fieldType)) {
                        fieldType = typeof(String);
                        if (null == _xmlMap) {
                            _xmlMap = new int[schemaRows.Length];
                        }
                        _xmlMap[sortedIndex] = XmlDocument;
                    }

                    DataColumn dataColumn = null;
                    if (!schemaRow.IsHidden ) {
                        dataColumn = _tableMapping.GetDataColumn(_fieldNames[sortedIndex], fieldType, _dataTable, mappingAction, schemaAction);
                    }

                    string basetable = /*schemaRow.BaseServerName+schemaRow.BaseCatalogName+schemaRow.BaseSchemaName+*/ schemaRow.BaseTableName;
                    if (null == dataColumn) {
                        if (null == columnIndexMap) {
                            columnIndexMap = CreateIndexMap(schemaRows.Length, unsortedIndex);
                        }
                        columnIndexMap[unsortedIndex] = -1;

                        // if the column is not mapped and it is a key, then don't add any key information
                        if (schemaRow.IsKey) { // MDAC 90822
#if DEBUG
                            if (AdapterSwitches.DataSchema.TraceVerbose) {
                                Debug.WriteLine("SetupSchema: partial primary key detected");
                            }
#endif
                            // if the hidden key comes from a different table - don't throw away the primary key
                            // example SELECT [T2].[ID], [T2].[ProdID], [T2].[VendorName] FROM [Vendor] AS [T2], [Prod] AS [T1] WHERE (([T1].[ProdID] = [T2].[ProdID]))
                            if (keyFromMultiTable || (schemaRow.BaseTableName == keyBaseTable)) { // WebData 100376
                                addPrimaryKeys = false; // don't add any future keys now
                                keys = null; // get rid of any keys we've seen
                            }
                        }
                        continue; // null means ignore (mapped to nothing)
                    }
                    else if ((null != _xmlMap) && (0 != _xmlMap[sortedIndex])) {
                        if (typeof(System.Data.SqlTypes.SqlXml) == dataColumn.DataType) {
                            _xmlMap[sortedIndex] = SqlXml;
                        }
                        else if (typeof(System.Xml.XmlDocument) == dataColumn.DataType) {
                            _xmlMap[sortedIndex] = XmlDocument;
                        }
                        else {
                            _xmlMap[sortedIndex] = 0; // datacolumn is not a specific Xml dataType, i.e. string

                            int total = 0;
                            for(int x = 0; x < _xmlMap.Length; ++x) {
                                total += _xmlMap[x];
                            }
                            if (0 == total) { // not mapping to a specific Xml datatype, get rid of the map
                                _xmlMap = null;
                            }
                        }
                    }

                    if (schemaRow.IsKey) {
                        if (basetable != keyBaseTable) {
                            if (null == keyBaseTable) {
                                keyBaseTable = basetable;
                            }
                            else keyFromMultiTable = true;
                        }
                    }

                    if (ischapter) {
                        if (null == dataColumn.Table) {
                            dataColumn.AllowDBNull = false;
                            dataColumn.AutoIncrement = true;
                            dataColumn.ReadOnly = true;
                        }
                        else if (!dataColumn.AutoIncrement) {
                            throw ADP.FillChapterAutoIncrement();
                        }
                    }
                    else {// MDAC 67033
                        if (!commonFromMultiTable) {
                            if ((basetable != commonBaseTable) && (!ADP.IsEmpty(basetable))) {
                                if (null == commonBaseTable) {
                                    commonBaseTable = basetable;
                                }
                                else {
                                    commonFromMultiTable = true;
                                }
                            }
                        }
                        if (4 <= (int)_loadOption) {
                            if (schemaRow.IsAutoIncrement && DataColumn.IsAutoIncrementType(fieldType)) {
                                // 

                                dataColumn.AutoIncrement = true;

                                if (!schemaRow.AllowDBNull) { // MDAC 71060
                                    dataColumn.AllowDBNull = false;
                                }
                            }

                            // setup maxLength, only for string columns since this is all the DataSet supports
                            if (fieldType == typeof(string)) {
                                //@devnote:  schemaRow.Size is count of characters for string columns, count of bytes otherwise
                                dataColumn.MaxLength = schemaRow.Size>0?schemaRow.Size:-1;
                            }

                            if (schemaRow.IsReadOnly) {
                                dataColumn.ReadOnly = true;
                            }
                            if (!schemaRow.AllowDBNull && (!schemaRow.IsReadOnly || schemaRow.IsKey)) { // MDAC 71060, 72252
                                dataColumn.AllowDBNull = false;
                            }

                            if (schemaRow.IsUnique && !schemaRow.IsKey && !fieldType.IsArray) {
                                // note, arrays are not comparable so only mark non-arrays as unique, ie timestamp columns
                                // are unique, but not comparable
                                dataColumn.Unique = true;

                                if (!schemaRow.AllowDBNull) { // MDAC 71060
                                    dataColumn.AllowDBNull = false;
                                }
                            }
                        }
                        else if (null == dataColumn.Table) {
                            dataColumn.AutoIncrement = schemaRow.IsAutoIncrement;
                            dataColumn.AllowDBNull = schemaRow.AllowDBNull;
                            dataColumn.ReadOnly = schemaRow.IsReadOnly;
                            dataColumn.Unique = schemaRow.IsUnique;

                            if (fieldType == typeof(string) || (fieldType == typeof(SqlTypes.SqlString))) {
                                //@devnote:  schemaRow.Size is count of characters for string columns, count of bytes otherwise
                                dataColumn.MaxLength = schemaRow.Size;
                            }
                        }
                    }
                    if (null == dataColumn.Table) {
                        if (4 > (int)_loadOption) {
                            AddAdditionalProperties(dataColumn, schemaRow.DataRow);
                        }
                        AddItemToAllowRollback(ref addedItems, dataColumn);
                        columnCollection.Add(dataColumn);
                    }

                    // The server sends us one key per table according to these rules.
                    //
                    // 1. If the table has a primary key, the server sends us this key.
                    // 2. If the table has a primary key and a unique key, it sends us the primary key
                    // 3. if the table has no primary key but has a unique key, it sends us the unique key
                    //
                    // In case 3, we will promote a unique key to a primary key IFF all the columns that compose
                    // that key are not nullable since no columns in a primary key can be null.  If one or more
                    // of the keys is nullable, then we will add a unique constraint.
                    //
                    if (addPrimaryKeys && schemaRow.IsKey) { // MDAC 67033
                        if (keys == null) {
                            keys = new DataColumn[schemaRows.Length];
                        }
                        keys[keyCount++] = dataColumn;
#if DEBUG
                        if (AdapterSwitches.DataSchema.TraceVerbose) {
                            Debug.WriteLine("SetupSchema: building list of " + ((isPrimary) ? "PrimaryKey" : "UniqueConstraint"));
                        }
#endif
                        // see case 3 above, we do want dataColumn.AllowDBNull not schemaRow.AllowDBNull
                        // otherwise adding PrimaryKey will change AllowDBNull to false
                        if (isPrimary && dataColumn.AllowDBNull) { // MDAC 72241
#if DEBUG
                            if (AdapterSwitches.DataSchema.TraceVerbose) {
                                Debug.WriteLine("SetupSchema: changing PrimaryKey into UniqueContraint");
                            }
#endif
                            isPrimary = false;
                        }
                    }

                    if (null != columnIndexMap) {
                        columnIndexMap[unsortedIndex] = dataColumn.Ordinal;
                    }
                    else if (unsortedIndex != dataColumn.Ordinal) {
                        columnIndexMap = CreateIndexMap(schemaRows.Length, unsortedIndex);
                        columnIndexMap[unsortedIndex] = dataColumn.Ordinal;
                    }
                    mappingCount++;
                }

                bool addDataRelation = false;
                DataColumn chapterColumn = null;
                if (null != chapterValue) { // add the extra column in the child table
                    Type fieldType = chapterValue.GetType();
                    chapterColumn = _tableMapping.GetDataColumn(_tableMapping.SourceTable, fieldType, _dataTable, mappingAction, schemaAction);
                    if (null != chapterColumn) {

                        if (null == chapterColumn.Table) {

                            chapterColumn.ReadOnly = true; // MDAC 71878
                            chapterColumn.AllowDBNull = false;

                            AddItemToAllowRollback(ref addedItems, chapterColumn);
                            columnCollection.Add(chapterColumn);
                            addDataRelation = (null != parentChapterColumn);
                        }
                        mappingCount++;
                    }
                }

                if (0 < mappingCount) {
                    if ((null != _dataSet) && null == _dataTable.DataSet) {
                        AddItemToAllowRollback(ref addedItems, _dataTable);
                        _dataSet.Tables.Add(_dataTable);
                    }
                    // setup the key
                    if (addPrimaryKeys && (null != keys)) { // MDAC 67033
                        if (keyCount < keys.Length) {
                            keys = ResizeColumnArray(keys, keyCount);
                        }

                        // MDAC 66188
                        if (isPrimary) {
#if DEBUG
                            if (AdapterSwitches.DataSchema.TraceVerbose) {
                                Debug.WriteLine("SetupSchema: set_PrimaryKey");
                            }
#endif
                            _dataTable.PrimaryKey = keys;
                        }
                        else {
                            UniqueConstraint unique = new UniqueConstraint("", keys);
                            ConstraintCollection constraints = _dataTable.Constraints;
                            int constraintCount = constraints.Count;
                            for (int i = 0; i < constraintCount; ++i) {
                                if (unique.Equals(constraints[i])) {
#if DEBUG
                                    if (AdapterSwitches.DataSchema.TraceVerbose) {
                                        Debug.WriteLine("SetupSchema: duplicate Contraint detected");
                                    }
#endif
                                    unique = null;
                                    break;
                                }
                            }
                            if (null != unique) {
#if DEBUG
                                if (AdapterSwitches.DataSchema.TraceVerbose) {
                                    Debug.WriteLine("SetupSchema: adding new UniqueConstraint");
                                }
#endif
                                constraints.Add(unique);
                            }
                        }
                    }
                    if (!commonFromMultiTable && !ADP.IsEmpty(commonBaseTable) && ADP.IsEmpty(_dataTable.TableName)) {
                        _dataTable.TableName = commonBaseTable;
                    }
                    if (gettingData) {
                        _indexMap = columnIndexMap;
                        _chapterMap = chapterIndexMap;
                        dataValues = SetupMapping(schemaRows.Length, columnCollection, chapterColumn, chapterValue);
                    }
                    else {
                        // debug only, but for retail debug ability
                        _mappedMode = -1;
                    }
                }
                else {
                    _dataTable = null;
                }
                if (addDataRelation) {
                    AddRelation(parentChapterColumn, chapterColumn);
                }
            }
            catch (Exception e) {
                if (ADP.IsCatchableOrSecurityExceptionType(e)) {
                    RollbackAddedItems(addedItems);
                }
                throw;
            }
            return dataValues;
        }
        public DataTable GetDataTableBySchemaAction(DataSet dataSet, MissingSchemaAction schemaAction)
        {
            if (null == dataSet)
            {
                throw ADP.ArgumentNull(nameof(dataSet));
            }
            string dataSetTable = DataSetTable;

            if (string.IsNullOrEmpty(dataSetTable))
            {
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceWarning)
                {
                    Debug.WriteLine("explicit filtering of SourceTable \"" + SourceTable + "\"");
                }
#endif
                return(null);
            }
            DataTableCollection tables = dataSet.Tables;
            int index = tables.IndexOf(dataSetTable);
            if ((0 <= index) && (index < tables.Count))
            {
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceInfo)
                {
                    Debug.WriteLine("schema match on DataTable \"" + dataSetTable);
                }
#endif
                return(tables[index]);
            }
            switch (schemaAction)
            {
            case MissingSchemaAction.Add:
            case MissingSchemaAction.AddWithKey:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceInfo)
                {
                    Debug.WriteLine("schema add of DataTable \"" + dataSetTable + "\"");
                }
#endif
                return(new DataTable(dataSetTable));

            case MissingSchemaAction.Ignore:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceWarning)
                {
                    Debug.WriteLine("schema filter of DataTable \"" + dataSetTable + "\"");
                }
#endif
                return(null);

            case MissingSchemaAction.Error:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceError)
                {
                    Debug.WriteLine("schema error on DataTable \"" + dataSetTable + "\"");
                }
#endif
                throw ADP.MissingTableSchema(dataSetTable, SourceTable);
            }
            throw ADP.InvalidMissingSchemaAction(schemaAction);
        }
        static internal DataColumn CreateDataColumnBySchemaAction(string sourceColumn, string dataSetColumn, DataTable dataTable, Type dataType, MissingSchemaAction schemaAction)
        {
            Debug.Assert(dataTable != null, "Should not call with a null DataTable");
            if (ADP.IsEmpty(dataSetColumn))
            {
                return(null);
            }

            switch (schemaAction)
            {
            case MissingSchemaAction.Add:
            case MissingSchemaAction.AddWithKey:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceInfo)
                {
                    Debug.WriteLine("schema add of DataColumn \"" + dataSetColumn + "\" <" + Convert.ToString(dataType, CultureInfo.InvariantCulture) + ">");
                }
#endif
                return(new DataColumn(dataSetColumn, dataType));

            case MissingSchemaAction.Ignore:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceWarning)
                {
                    Debug.WriteLine("schema filter of DataColumn \"" + dataSetColumn + "\" <" + Convert.ToString(dataType, CultureInfo.InvariantCulture) + ">");
                }
#endif
                return(null);

            case MissingSchemaAction.Error:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceError)
                {
                    Debug.WriteLine("schema error on DataColumn \"" + dataSetColumn + "\" <" + Convert.ToString(dataType, CultureInfo.InvariantCulture) + ">");
                }
#endif
                throw ADP.ColumnSchemaMissing(dataSetColumn, dataTable.TableName, sourceColumn);
            }
            throw ADP.InvalidMissingSchemaAction(schemaAction);
        }
        internal static void Merge(DataSet targetSet, DataTable sourceTable, bool preserveChanges, MissingSchemaAction missingSchemaAction)
        {
            if (targetSet == null)
            {
                throw new ArgumentNullException("targetSet");
            }
            if (sourceTable == null)
            {
                throw new ArgumentNullException("sourceTable");
            }
            if (sourceTable.DataSet == targetSet)
            {
                return;
            }

            bool savedEnfoceConstraints = targetSet.EnforceConstraints;

            targetSet.EnforceConstraints = false;

            DataTable targetTable = null;

            if (!AdjustSchema(targetSet, sourceTable, missingSchemaAction, ref targetTable))
            {
                return;
            }
            if (targetTable != null)
            {
                fillData(targetTable, sourceTable, preserveChanges);
            }
            targetSet.EnforceConstraints = savedEnfoceConstraints;
        }
        public static DataColumn GetDataColumn(DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
        {
            if (columnMappings != null)
            {
                int index = columnMappings.IndexOf(sourceColumn);
                if (-1 != index)
                {
                    return columnMappings.items[index].GetDataColumnBySchemaAction(dataTable, dataType, schemaAction);
                }
            }
            if (ADP.IsEmpty(sourceColumn))
            {
                throw ADP.InvalidSourceColumn("sourceColumn");
            }
            switch (mappingAction)
            {
                case MissingMappingAction.Passthrough:
                    return DataColumnMapping.GetDataColumnBySchemaAction(sourceColumn, sourceColumn, dataTable, dataType, schemaAction);

                case MissingMappingAction.Ignore:
                    return null;

                case MissingMappingAction.Error:
                    throw ADP.MissingColumnMapping(sourceColumn);
            }
            throw ADP.InvalidMissingMappingAction(mappingAction);
        }
Exemple #32
0
 public DataColumn?GetDataColumn(string sourceColumn, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] Type?dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
 {
     return(DataColumnMappingCollection.GetDataColumn(_columnMappings, sourceColumn, dataType, dataTable, mappingAction, schemaAction));
 }
        private object[] SetupSchemaWithoutKeyInfo(MissingMappingAction mappingAction, MissingSchemaAction schemaAction, bool gettingData, DataColumn parentChapterColumn, object chapterValue)
        {
            int[]  numArray   = null;
            bool[] flagArray  = null;
            int    num3       = 0;
            int    fieldCount = this._dataReader.FieldCount;

            object[]      objArray = null;
            List <object> items    = null;

            try
            {
                DataColumnCollection columnCollection = this._dataTable.Columns;
                for (int i = 0; i < fieldCount; i++)
                {
                    bool flag      = false;
                    Type fieldType = this._dataReader.GetFieldType(i);
                    if (null == fieldType)
                    {
                        throw ADP.MissingDataReaderFieldType(i);
                    }
                    if (typeof(IDataReader).IsAssignableFrom(fieldType))
                    {
                        if (flagArray == null)
                        {
                            flagArray = new bool[fieldCount];
                        }
                        flagArray[i] = flag = true;
                        fieldType    = typeof(int);
                    }
                    else if (typeof(System.Data.SqlTypes.SqlXml).IsAssignableFrom(fieldType))
                    {
                        if (this._xmlMap == null)
                        {
                            this._xmlMap = new int[fieldCount];
                        }
                        this._xmlMap[i] = 1;
                    }
                    else if (typeof(XmlReader).IsAssignableFrom(fieldType))
                    {
                        fieldType = typeof(string);
                        if (this._xmlMap == null)
                        {
                            this._xmlMap = new int[fieldCount];
                        }
                        this._xmlMap[i] = 2;
                    }
                    DataColumn column = this._tableMapping.GetDataColumn(this._fieldNames[i], fieldType, this._dataTable, mappingAction, schemaAction);
                    if (column == null)
                    {
                        if (numArray == null)
                        {
                            numArray = this.CreateIndexMap(fieldCount, i);
                        }
                        numArray[i] = -1;
                    }
                    else
                    {
                        if ((this._xmlMap != null) && (this._xmlMap[i] != 0))
                        {
                            if (typeof(System.Data.SqlTypes.SqlXml) == column.DataType)
                            {
                                this._xmlMap[i] = 1;
                            }
                            else if (typeof(System.Xml.XmlDocument) == column.DataType)
                            {
                                this._xmlMap[i] = 2;
                            }
                            else
                            {
                                this._xmlMap[i] = 0;
                                int num5 = 0;
                                for (int j = 0; j < this._xmlMap.Length; j++)
                                {
                                    num5 += this._xmlMap[j];
                                }
                                if (num5 == 0)
                                {
                                    this._xmlMap = null;
                                }
                            }
                        }
                        if (column.Table == null)
                        {
                            if (flag)
                            {
                                column.AllowDBNull   = false;
                                column.AutoIncrement = true;
                                column.ReadOnly      = true;
                            }
                            this.AddItemToAllowRollback(ref items, column);
                            columnCollection.Add(column);
                        }
                        else if (flag && !column.AutoIncrement)
                        {
                            throw ADP.FillChapterAutoIncrement();
                        }
                        if (numArray != null)
                        {
                            numArray[i] = column.Ordinal;
                        }
                        else if (i != column.Ordinal)
                        {
                            numArray    = this.CreateIndexMap(fieldCount, i);
                            numArray[i] = column.Ordinal;
                        }
                        num3++;
                    }
                }
                bool       flag2   = false;
                DataColumn column2 = null;
                if (chapterValue != null)
                {
                    Type type = chapterValue.GetType();
                    column2 = this._tableMapping.GetDataColumn(this._tableMapping.SourceTable, type, this._dataTable, mappingAction, schemaAction);
                    if (column2 != null)
                    {
                        if (column2.Table == null)
                        {
                            this.AddItemToAllowRollback(ref items, column2);
                            columnCollection.Add(column2);
                            flag2 = null != parentChapterColumn;
                        }
                        num3++;
                    }
                }
                if (0 < num3)
                {
                    if ((this._dataSet != null) && (this._dataTable.DataSet == null))
                    {
                        this.AddItemToAllowRollback(ref items, this._dataTable);
                        this._dataSet.Tables.Add(this._dataTable);
                    }
                    if (gettingData)
                    {
                        if (columnCollection == null)
                        {
                            columnCollection = this._dataTable.Columns;
                        }
                        this._indexMap   = numArray;
                        this._chapterMap = flagArray;
                        objArray         = this.SetupMapping(fieldCount, columnCollection, column2, chapterValue);
                    }
                    else
                    {
                        this._mappedMode = -1;
                    }
                }
                else
                {
                    this._dataTable = null;
                }
                if (flag2)
                {
                    this.AddRelation(parentChapterColumn, column2);
                }
            }
            catch (Exception exception)
            {
                if (ADP.IsCatchableOrSecurityExceptionType(exception))
                {
                    this.RollbackAddedItems(items);
                }
                throw;
            }
            return(objArray);
        }
Exemple #34
0
 public static void FillDataSet(this DbConnection conn, DataSet dataSet, string tableName, string sql, MissingSchemaAction missingSchemaAction = MissingSchemaAction.Add)
 {
     using (var da = conn.CreateDataAdapter())
         using (var cmd = conn.CreateCommand())
         {
             cmd.CommandText        = sql;
             da.SelectCommand       = cmd;
             da.MissingSchemaAction = missingSchemaAction;
             da.Fill(dataSet, tableName);
         }
 }
Exemple #35
0
 public static DataColumn GetDataColumn(DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
 {
     throw new NotImplementedException();
 }
 public DataColumn? GetDataColumn(string sourceColumn, Type? dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
 {
     return DataColumnMappingCollection.GetDataColumn(_columnMappings, sourceColumn, dataType, dataTable, mappingAction, schemaAction);
 }
Exemple #37
0
        public static DataColumn GetDataColumn(DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
        {
            if (columnMappings != null)
            {
                int index = columnMappings.IndexOf(sourceColumn);
                if (-1 != index)
                {
                    return(columnMappings.items[index].GetDataColumnBySchemaAction(dataTable, dataType, schemaAction));
                }
            }
            if (ADP.IsEmpty(sourceColumn))
            {
                throw ADP.InvalidSourceColumn("sourceColumn");
            }
            switch (mappingAction)
            {
            case MissingMappingAction.Passthrough:
                return(DataColumnMapping.GetDataColumnBySchemaAction(sourceColumn, sourceColumn, dataTable, dataType, schemaAction));

            case MissingMappingAction.Ignore:
                return(null);

            case MissingMappingAction.Error:
                throw ADP.MissingColumnMapping(sourceColumn);
            }
            throw ADP.InvalidMissingMappingAction(mappingAction);
        }
Exemple #38
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SWCommandAttribute"/> class with the specified
        /// <see cref="CommandType"/>, <see cref="CommandText"/>
        /// and <see cref="ReturnIfNull"/> values
        /// </summary>
        /// <param name="commandType">Is a value of <see cref="SWCommandAttribute.CommandType"/> property</param>
        /// <param name="commandText">Is a value of <see cref="SWCommandAttribute.CommandText"/> property</param>
        /// <param name="returnIfNull">Is a value of <see cref="SWCommandAttribute.ReturnIfNull"/> property</param>
        /// <param name="missingSchemaAction">Is a value of <see cref="SWCommandAttribute.MissingSchemaAction"/> property</param>
        public SWCommandAttribute(SWCommandType commandType, string commandText, object returnIfNull, MissingSchemaAction missingSchemaAction)
        {
            m_commandType  = commandType;
            m_commandText  = commandText;
            m_returnIfNull = returnIfNull;
            if (m_returnIfNull is string)
            {
                if ((string)m_returnIfNull == NullReturnValueToken)
                {
                    m_returnIfNull = null;
                }
            }

            m_missingSchemaAction = missingSchemaAction;
        }
 private object[] SetupSchemaWithoutKeyInfo(MissingMappingAction mappingAction, MissingSchemaAction schemaAction, bool gettingData, DataColumn parentChapterColumn, object chapterValue)
 {
     int[] numArray = null;
     bool[] flagArray = null;
     int num3 = 0;
     int fieldCount = this._dataReader.FieldCount;
     object[] objArray = null;
     List<object> items = null;
     try
     {
         DataColumnCollection columnCollection = this._dataTable.Columns;
         for (int i = 0; i < fieldCount; i++)
         {
             bool flag = false;
             Type fieldType = this._dataReader.GetFieldType(i);
             if (null == fieldType)
             {
                 throw ADP.MissingDataReaderFieldType(i);
             }
             if (typeof(IDataReader).IsAssignableFrom(fieldType))
             {
                 if (flagArray == null)
                 {
                     flagArray = new bool[fieldCount];
                 }
                 flagArray[i] = flag = true;
                 fieldType = typeof(int);
             }
             else if (typeof(System.Data.SqlTypes.SqlXml).IsAssignableFrom(fieldType))
             {
                 if (this._xmlMap == null)
                 {
                     this._xmlMap = new int[fieldCount];
                 }
                 this._xmlMap[i] = 1;
             }
             else if (typeof(XmlReader).IsAssignableFrom(fieldType))
             {
                 fieldType = typeof(string);
                 if (this._xmlMap == null)
                 {
                     this._xmlMap = new int[fieldCount];
                 }
                 this._xmlMap[i] = 2;
             }
             DataColumn column = this._tableMapping.GetDataColumn(this._fieldNames[i], fieldType, this._dataTable, mappingAction, schemaAction);
             if (column == null)
             {
                 if (numArray == null)
                 {
                     numArray = this.CreateIndexMap(fieldCount, i);
                 }
                 numArray[i] = -1;
             }
             else
             {
                 if ((this._xmlMap != null) && (this._xmlMap[i] != 0))
                 {
                     if (typeof(System.Data.SqlTypes.SqlXml) == column.DataType)
                     {
                         this._xmlMap[i] = 1;
                     }
                     else if (typeof(System.Xml.XmlDocument) == column.DataType)
                     {
                         this._xmlMap[i] = 2;
                     }
                     else
                     {
                         this._xmlMap[i] = 0;
                         int num5 = 0;
                         for (int j = 0; j < this._xmlMap.Length; j++)
                         {
                             num5 += this._xmlMap[j];
                         }
                         if (num5 == 0)
                         {
                             this._xmlMap = null;
                         }
                     }
                 }
                 if (column.Table == null)
                 {
                     if (flag)
                     {
                         column.AllowDBNull = false;
                         column.AutoIncrement = true;
                         column.ReadOnly = true;
                     }
                     this.AddItemToAllowRollback(ref items, column);
                     columnCollection.Add(column);
                 }
                 else if (flag && !column.AutoIncrement)
                 {
                     throw ADP.FillChapterAutoIncrement();
                 }
                 if (numArray != null)
                 {
                     numArray[i] = column.Ordinal;
                 }
                 else if (i != column.Ordinal)
                 {
                     numArray = this.CreateIndexMap(fieldCount, i);
                     numArray[i] = column.Ordinal;
                 }
                 num3++;
             }
         }
         bool flag2 = false;
         DataColumn column2 = null;
         if (chapterValue != null)
         {
             Type type = chapterValue.GetType();
             column2 = this._tableMapping.GetDataColumn(this._tableMapping.SourceTable, type, this._dataTable, mappingAction, schemaAction);
             if (column2 != null)
             {
                 if (column2.Table == null)
                 {
                     this.AddItemToAllowRollback(ref items, column2);
                     columnCollection.Add(column2);
                     flag2 = null != parentChapterColumn;
                 }
                 num3++;
             }
         }
         if (0 < num3)
         {
             if ((this._dataSet != null) && (this._dataTable.DataSet == null))
             {
                 this.AddItemToAllowRollback(ref items, this._dataTable);
                 this._dataSet.Tables.Add(this._dataTable);
             }
             if (gettingData)
             {
                 if (columnCollection == null)
                 {
                     columnCollection = this._dataTable.Columns;
                 }
                 this._indexMap = numArray;
                 this._chapterMap = flagArray;
                 objArray = this.SetupMapping(fieldCount, columnCollection, column2, chapterValue);
             }
             else
             {
                 this._mappedMode = -1;
             }
         }
         else
         {
             this._dataTable = null;
         }
         if (flag2)
         {
             this.AddRelation(parentChapterColumn, column2);
         }
     }
     catch (Exception exception)
     {
         if (ADP.IsCatchableOrSecurityExceptionType(exception))
         {
             this.RollbackAddedItems(items);
         }
         throw;
     }
     return objArray;
 }
Exemple #40
0
 /// <summary>Exposes <c>static <see cref="DataColumn"/> <see cref="DataColumnMapping"/>.CreateDataColumnBySchemaAction(string sourceColumn, string dataSetColumn, DataTable dataTable, Type dataType, MissingSchemaAction schemaAction)</c>.</summary>
 public static DataColumn CreateDataColumnBySchemaAction_(string sourceColumn, string dataSetColumn, DataTable dataTable, Type dataType, MissingSchemaAction schemaAction)
 {
     return(_CreateDataColumnBySchemaAction.InvokeAllowNull <DataColumn>(@this: null, sourceColumn, dataSetColumn, dataTable, dataType, schemaAction));
 }
Exemple #41
0
        /// <summary>
        ///     Creates or Modifies the schema of the given DataTable based on the schema of
        ///     the reader and the arguments passed.
        /// </summary>
        internal static int[] BuildSchema(IDataReader reader, DataTable table,
                                          SchemaType schemaType,
                                          MissingSchemaAction missingSchAction,
                                          MissingMappingAction missingMapAction,
                                          DataTableMappingCollection dtMapping
                                          )
        {
            int readerIndex = 0;

            // FIXME : this fails if query has fewer columns than a table
            int[] mapping = new int[table.Columns.Count];             // mapping the reader indexes to the datatable indexes

            for (int i = 0; i < mapping.Length; i++)
            {
                mapping[i] = -1;
            }

            ArrayList primaryKey       = new ArrayList();
            ArrayList sourceColumns    = new ArrayList();
            bool      createPrimaryKey = true;

            DataTable schemaTable = reader.GetSchemaTable();

            DataColumn ColumnNameCol      = schemaTable.Columns["ColumnName"];
            DataColumn DataTypeCol        = schemaTable.Columns["DataType"];
            DataColumn IsAutoIncrementCol = schemaTable.Columns["IsAutoIncrement"];
            DataColumn AllowDBNullCol     = schemaTable.Columns["AllowDBNull"];
            DataColumn IsReadOnlyCol      = schemaTable.Columns["IsReadOnly"];
            DataColumn IsKeyCol           = schemaTable.Columns["IsKey"];
            DataColumn IsUniqueCol        = schemaTable.Columns["IsUnique"];
            DataColumn ColumnSizeCol      = schemaTable.Columns["ColumnSize"];

            foreach (DataRow schemaRow in schemaTable.Rows)
            {
                // generate a unique column name in the source table.
                string sourceColumnName;
                string realSourceColumnName;
                if (ColumnNameCol == null || schemaRow.IsNull(ColumnNameCol) ||
                    (string)schemaRow [ColumnNameCol] == String.Empty)
                {
                    sourceColumnName     = DefaultSourceColumnName;
                    realSourceColumnName = DefaultSourceColumnName + "1";
                }
                else
                {
                    sourceColumnName     = (string)schemaRow [ColumnNameCol];
                    realSourceColumnName = sourceColumnName;
                }

                for (int i = 1; sourceColumns.Contains(realSourceColumnName); i += 1)
                {
                    realSourceColumnName = String.Format("{0}{1}", sourceColumnName, i);
                }
                sourceColumns.Add(realSourceColumnName);

                // generate DataSetColumnName from DataTableMapping, if any
                DataTableMapping tableMapping = null;

                //FIXME : The sourcetable name shud get passed as a parameter..
                int    index    = dtMapping.IndexOfDataSetTable(table.TableName);
                string srcTable = (index != -1 ? dtMapping[index].SourceTable : table.TableName);
                tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(dtMapping, ADP.IsEmpty(srcTable) ? " " : srcTable, table.TableName, missingMapAction);
                if (tableMapping != null)
                {
                    table.TableName = tableMapping.DataSetTable;
                    // check to see if the column mapping exists
                    DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction(tableMapping.ColumnMappings, realSourceColumnName, missingMapAction);
                    if (columnMapping != null)
                    {
                        Type       columnType = schemaRow[DataTypeCol] as Type;
                        DataColumn col        = columnType != null?columnMapping.GetDataColumnBySchemaAction(
                            table,
                            columnType,
                            missingSchAction) : null;

                        if (col != null)
                        {
                            // if the column is not in the table - add it.
                            if (table.Columns.IndexOf(col) == -1)
                            {
                                if (missingSchAction == MissingSchemaAction.Add ||
                                    missingSchAction == MissingSchemaAction.AddWithKey)
                                {
                                    table.Columns.Add(col);
                                }

                                int[] tmp = new int[mapping.Length + 1];
                                Array.Copy(mapping, 0, tmp, 0, col.Ordinal);
                                Array.Copy(mapping, col.Ordinal, tmp, col.Ordinal + 1, mapping.Length - col.Ordinal);
                                mapping = tmp;
                            }

                            if (missingSchAction == MissingSchemaAction.AddWithKey)
                            {
                                object value       = (AllowDBNullCol != null) ? schemaRow[AllowDBNullCol] : null;
                                bool   allowDBNull = value is bool?(bool)value : true;

                                value = (IsKeyCol != null) ? schemaRow[IsKeyCol] : null;
                                bool isKey = value is bool?(bool)value : false;

                                value = (IsAutoIncrementCol != null) ? schemaRow[IsAutoIncrementCol] : null;
                                bool isAutoIncrement = value is bool?(bool)value : false;

                                value = (IsReadOnlyCol != null) ? schemaRow[IsReadOnlyCol] : null;
                                bool isReadOnly = value is bool?(bool)value : false;

                                value = (IsUniqueCol != null) ? schemaRow[IsUniqueCol] : null;
                                bool isUnique = value is bool?(bool)value : false;

                                col.AllowDBNull = allowDBNull;
                                // fill woth key info
                                if (isAutoIncrement && DataColumn.CanAutoIncrement(columnType))
                                {
                                    col.AutoIncrement = true;
                                    if (!allowDBNull)
                                    {
                                        col.AllowDBNull = false;
                                    }
                                }

                                if (columnType == DbTypes.TypeOfString)
                                {
                                    col.MaxLength = (ColumnSizeCol != null) ? (int)schemaRow[ColumnSizeCol] : 0;
                                }

                                if (isReadOnly)
                                {
                                    col.ReadOnly = true;
                                }

                                if (!allowDBNull && (!isReadOnly || isKey))
                                {
                                    col.AllowDBNull = false;
                                }
                                if (isUnique && !isKey && !columnType.IsArray)
                                {
                                    col.Unique = true;
                                    if (!allowDBNull)
                                    {
                                        col.AllowDBNull = false;
                                    }
                                }

                                // This might not be set by all DataProviders
                                bool isHidden = false;
                                if (schemaTable.Columns.Contains("IsHidden"))
                                {
                                    value    = schemaRow["IsHidden"];
                                    isHidden = ((value is bool) ? (bool)value : false);
                                }

                                if (isKey && !isHidden)
                                {
                                    primaryKey.Add(col);
                                    if (allowDBNull)
                                    {
                                        createPrimaryKey = false;
                                    }
                                }
                            }
                            // add the ordinal of the column as a key and the index of the column in the datareader as a value.
                            mapping[col.Ordinal] = readerIndex++;
                        }
                    }
                }
            }
            if (primaryKey.Count > 0)
            {
                DataColumn[] colKey = (DataColumn[])(primaryKey.ToArray(typeof(DataColumn)));
                if (createPrimaryKey)
                {
                    table.PrimaryKey = colKey;
                }
                else
                {
                    UniqueConstraint uConstraint = new UniqueConstraint(colKey);
                    for (int i = 0; i < table.Constraints.Count; i++)
                    {
                        if (table.Constraints[i].Equals(uConstraint))
                        {
                            uConstraint = null;
                            break;
                        }
                    }

                    if (uConstraint != null)
                    {
                        table.Constraints.Add(uConstraint);
                    }
                }
            }
            return(mapping);
        }
 /// <summary>
 ///     Merge the specified <see cref="T:System.Data.DataTable"/> with the current DataTable, indicating whether to preserve changes and 
 ///     how to handle missing schema in the current DataTable.
 /// </summary>
 /// <param name="table">
 ///     The <see cref="T:System.Data.DataTable"/> to be merged with the current <see cref="T:System.Data.DataTable"/>.
 /// </param>
 /// <param name="preserveChanges">
 ///     true, to preserve changes in the current <see cref="T:System.Data.DataTable"/>; otherwise false.
 /// </param>
 /// <param name="missingSchemaAction">
 ///     One of the <see cref="T:System.Data.MissingSchemaAction"/> values.
 /// </param>
 public void Merge(DataTable table,
                   bool preserveChanges,
                   MissingSchemaAction missingSchemaAction)
 {
     this.DataTableInstance.Merge(table, preserveChanges, missingSchemaAction);
 }
        static Object CreateMergerInvoker(DataTable target, bool preserveChanges, MissingSchemaAction action)
        {

            return null;
        }
Exemple #44
0
        public static DataColumn GetDataColumn(DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
        {
            if (null != columnMappings)
            {
                int index = columnMappings.IndexOf(sourceColumn);
                if (-1 != index)
                {
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceInfo)
                    {
                        Debug.WriteLine($"mapping match on SourceColumn \"{sourceColumn}\"");
                    }
#endif
                    return(columnMappings._items[index].GetDataColumnBySchemaAction(dataTable, dataType, schemaAction));
                }
            }
            if (string.IsNullOrEmpty(sourceColumn))
            {
                throw ADP.InvalidSourceColumn(nameof(sourceColumn));
            }
            switch (mappingAction)
            {
            case MissingMappingAction.Passthrough:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceInfo)
                {
                    Debug.WriteLine($"mapping passthrough of SourceColumn \"{sourceColumn}\"");
                }
#endif
                return(DataColumnMapping.GetDataColumnBySchemaAction(sourceColumn, sourceColumn, dataTable, dataType, schemaAction));

            case MissingMappingAction.Ignore:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceWarning)
                {
                    Debug.WriteLine($"mapping filter of SourceColumn \"{sourceColumn}\"");
                }
#endif
                return(null);

            case MissingMappingAction.Error:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceError)
                {
                    Debug.WriteLine($"mapping error on SourceColumn \"{sourceColumn}\"");
                }
#endif
                throw ADP.MissingColumnMapping(sourceColumn);
            }
            throw ADP.InvalidMissingMappingAction(mappingAction);
        }
        private static bool AdjustSchema(DataTable targetTable, DataTable sourceTable, MissingSchemaAction missingSchemaAction)
        {
            if (missingSchemaAction == MissingSchemaAction.Ignore)
            {
                return(true);
            }

            for (int i = 0; i < sourceTable.Columns.Count; i++)
            {
                DataColumn sourceColumn = sourceTable.Columns[i];
                // if a column from the source table doesn't exists in the target table
                // we act according to the missingschemaaction param.
                DataColumn targetColumn = targetTable.Columns [sourceColumn.ColumnName];
                if (targetColumn == null)
                {
                    if (missingSchemaAction == MissingSchemaAction.Error)
                    {
                        throw new DataException("Target table " + targetTable.TableName +
                                                " missing definition for column " + sourceColumn.ColumnName);
                    }

                    targetColumn = new DataColumn(sourceColumn.ColumnName, sourceColumn.DataType,
                                                  sourceColumn.Expression, sourceColumn.ColumnMapping);
                    targetTable.Columns.Add(targetColumn);
                }

                if (sourceColumn.AutoIncrement)
                {
                    targetColumn.AutoIncrement     = sourceColumn.AutoIncrement;
                    targetColumn.AutoIncrementSeed = sourceColumn.AutoIncrementSeed;
                    targetColumn.AutoIncrementStep = sourceColumn.AutoIncrementStep;
                }
            }

            if (!AdjustPrimaryKeys(targetTable, sourceTable))
            {
                return(false);
            }

            checkColumnTypes(targetTable, sourceTable);

            return(true);
        }
Exemple #46
0
 private object[] SetupSchemaWithoutKeyInfo(MissingMappingAction mappingAction, MissingSchemaAction schemaAction, bool gettingData, DataColumn parentChapterColumn, object chapterValue)
 {
     int[] array = null;
     bool[] array2 = null;
     int num = 0;
     int fieldCount = this._dataReader.FieldCount;
     object[] result = null;
     List<object> items = null;
     try
     {
         DataColumnCollection columns = this._dataTable.Columns;
         bool flag = this._dataTable.Columns.Count == 0 && (this._tableMapping.ColumnMappings == null || this._tableMapping.ColumnMappings.Count == 0) && mappingAction == MissingMappingAction.Passthrough;
         for (int i = 0; i < fieldCount; i++)
         {
             bool flag2 = false;
             Type type = this._dataReader.GetFieldType(i);
             if (null == type)
             {
                 throw new Exception("MissingDataReaderFieldType");
             }
             if (typeof(IDataReader).IsAssignableFrom(type))
             {
                 if (array2 == null)
                 {
                     array2 = new bool[fieldCount];
                 }
                 flag2 = (array2[i] = true);
                 type = typeof(int);
             }
             else
             {
                 if (typeof(SqlXml).IsAssignableFrom(type))
                 {
                     if (this._xmlMap == null)
                     {
                         this._xmlMap = new int[fieldCount];
                     }
                     this._xmlMap[i] = 1;
                 }
                 else
                 {
                     if (typeof(XmlReader).IsAssignableFrom(type))
                     {
                         type = typeof(string);
                         if (this._xmlMap == null)
                         {
                             this._xmlMap = new int[fieldCount];
                         }
                         this._xmlMap[i] = 2;
                     }
                 }
             }
             DataColumn dataColumn;
             if (flag)
             {
                 dataColumn = CreateDataColumnBySchemaAction(this._fieldNames[i], this._fieldNames[i], this._dataTable, type, schemaAction);
             }
             else
             {
                 dataColumn = this._tableMapping.GetDataColumn(this._fieldNames[i], type, this._dataTable, mappingAction, schemaAction);
             }
             if (dataColumn == null)
             {
                 if (array == null)
                 {
                     array = this.CreateIndexMap(fieldCount, i);
                 }
                 array[i] = -1;
             }
             else
             {
                 if (this._xmlMap != null && this._xmlMap[i] != 0)
                 {
                     if (typeof(SqlXml) == dataColumn.DataType)
                     {
                         this._xmlMap[i] = 1;
                     }
                     else
                     {
                         if (typeof(XmlDocument) == dataColumn.DataType)
                         {
                             this._xmlMap[i] = 2;
                         }
                         else
                         {
                             this._xmlMap[i] = 0;
                             int num2 = 0;
                             for (int j = 0; j < this._xmlMap.Length; j++)
                             {
                                 num2 += this._xmlMap[j];
                             }
                             if (num2 == 0)
                             {
                                 this._xmlMap = null;
                             }
                         }
                     }
                 }
                 if (dataColumn.Table == null)
                 {
                     if (flag2)
                     {
                         dataColumn.AllowDBNull = false;
                         dataColumn.AutoIncrement = true;
                         dataColumn.ReadOnly = true;
                     }
                     this.AddItemToAllowRollback(ref items, dataColumn);
                     columns.Add(dataColumn);
                 }
                 else
                 {
                     if (flag2 && !dataColumn.AutoIncrement)
                     {
                         throw new Exception("FillChapterAutoIncrement");
                     }
                 }
                 if (array != null)
                 {
                     array[i] = dataColumn.Ordinal;
                 }
                 else
                 {
                     if (i != dataColumn.Ordinal)
                     {
                         array = this.CreateIndexMap(fieldCount, i);
                         array[i] = dataColumn.Ordinal;
                     }
                 }
                 num++;
             }
         }
         bool flag3 = false;
         DataColumn dataColumn2 = null;
         if (chapterValue != null)
         {
             Type type2 = chapterValue.GetType();
             dataColumn2 = this._tableMapping.GetDataColumn(this._tableMapping.SourceTable, type2, this._dataTable, mappingAction, schemaAction);
             if (dataColumn2 != null)
             {
                 if (dataColumn2.Table == null)
                 {
                     this.AddItemToAllowRollback(ref items, dataColumn2);
                     columns.Add(dataColumn2);
                     flag3 = (null != parentChapterColumn);
                 }
                 num++;
             }
         }
         if (0 < num)
         {
             if (this._dataSet != null && this._dataTable.DataSet == null)
             {
                 this.AddItemToAllowRollback(ref items, this._dataTable);
                 this._dataSet.Tables.Add(this._dataTable);
             }
             if (gettingData)
             {
                 if (columns == null)
                 {
                     columns = this._dataTable.Columns;
                 }
                 this._indexMap = array;
                 this._chapterMap = array2;
                 result = this.SetupMapping(fieldCount, columns, dataColumn2, chapterValue);
             }
             else
             {
                 this._mappedMode = -1;
             }
         }
         else
         {
             this._dataTable = null;
         }
         if (flag3)
         {
             this.AddRelation(parentChapterColumn, dataColumn2);
         }
     }
     catch (Exception e)
     {
         this.RollbackAddedItems(items);
         throw e;
     }
     return result;
 }
 [EditorBrowsableAttribute(EditorBrowsableState.Advanced)]   // MDAC 69508
 public DataColumn GetDataColumnBySchemaAction(DataTable dataTable, Type dataType, MissingSchemaAction schemaAction)
 {
     return(GetDataColumnBySchemaAction(SourceColumn, DataSetColumn, dataTable, dataType, schemaAction));
 }
        private static bool AdjustSchemaRelations(DataSet targetSet, DataSet sourceSet, MissingSchemaAction missingSchemaAction)
        {
            if (missingSchemaAction == MissingSchemaAction.Ignore)
            {
                return(true);
            }

            foreach (DataTable sourceTable in sourceSet.Tables)
            {
                DataTable targetTable = targetSet.Tables[sourceTable.TableName];
                if (targetTable == null)
                {
                    continue;
                }

                foreach (Constraint constraint in sourceTable.Constraints)
                {
                    Constraint targetConstraint = null;

                    string constraintName = constraint.ConstraintName;
                    if (targetTable.Constraints.Contains(constraintName))
                    {
                        constraintName = "";
                    }

                    UniqueConstraint uc = constraint as UniqueConstraint;
                    // PrimaryKey is already taken care of while merging the table
                    // ForeignKey constraint takes care of Parent Unique Constraints
                    if (uc != null)
                    {
                        if (uc.IsPrimaryKey || uc.ChildConstraint != null)
                        {
                            continue;
                        }
                        DataColumn[] columns = ResolveColumns(targetTable, uc.Columns);
                        targetConstraint = new UniqueConstraint(constraintName, columns, false);
                    }

                    ForeignKeyConstraint fc = constraint as ForeignKeyConstraint;
                    if (fc != null)
                    {
                        DataColumn[] columns        = ResolveColumns(targetTable, fc.Columns);
                        DataColumn[] relatedColumns = ResolveColumns(targetSet.Tables [fc.RelatedTable.TableName],
                                                                     fc.RelatedColumns);
                        targetConstraint = new ForeignKeyConstraint(constraintName, relatedColumns, columns);
                    }

                    bool dupConstraintFound = false;
                    foreach (Constraint cons in targetTable.Constraints)
                    {
                        if (!targetConstraint.Equals(cons))
                        {
                            continue;
                        }
                        dupConstraintFound = true;
                        break;
                    }

                    // If equivalent-constraint already exists, then just do nothing
                    if (dupConstraintFound)
                    {
                        continue;
                    }

                    if (missingSchemaAction == MissingSchemaAction.Error)
                    {
                        throw new DataException("Target DataSet missing " + targetConstraint.GetType() +
                                                targetConstraint.ConstraintName);
                    }
                    else
                    {
                        targetTable.Constraints.Add(targetConstraint);
                    }
                }
            }

            foreach (DataRelation relation in sourceSet.Relations)
            {
                DataRelation targetRelation = targetSet.Relations [relation.RelationName];
                if (targetRelation == null)
                {
                    if (missingSchemaAction == MissingSchemaAction.Error)
                    {
                        throw new ArgumentException("Target DataSet mising definition for " +
                                                    relation.RelationName);
                    }

                    DataColumn[] parentColumns = ResolveColumns(targetSet.Tables [relation.ParentTable.TableName],
                                                                relation.ParentColumns);
                    DataColumn[] childColumns = ResolveColumns(targetSet.Tables [relation.ChildTable.TableName],
                                                               relation.ChildColumns);
                    targetRelation = targetSet.Relations.Add(relation.RelationName, parentColumns,
                                                             childColumns, relation.createConstraints);
                    targetRelation.Nested = relation.Nested;
                }
                else if (!CompareColumnArrays(relation.ParentColumns, targetRelation.ParentColumns) ||
                         !CompareColumnArrays(relation.ChildColumns, targetRelation.ChildColumns))
                {
                    RaiseMergeFailedEvent(null, "Relation " + relation.RelationName +
                                          " cannot be merged, because keys have mismatch columns.");
                }
            }

            return(true);
        }
        private object[] SetupSchemaWithoutKeyInfo(MissingMappingAction mappingAction, MissingSchemaAction schemaAction, bool gettingData, DataColumn parentChapterColumn, object chapterValue) {
            int[] columnIndexMap = null;
            bool[] chapterIndexMap = null;

            int mappingCount = 0;
            int count = _dataReader.FieldCount;

            object[] dataValues = null;
            List<object> addedItems = null;
            try {
                DataColumnCollection columnCollection = _dataTable.Columns;
                columnCollection.EnsureAdditionalCapacity(count + (chapterValue != null ? 1 : 0));
                // We can always just create column if there are no existing column or column mappings, and the mapping action is passthrough
                bool alwaysCreateColumns = ((_dataTable.Columns.Count == 0) && ((_tableMapping.ColumnMappings == null) || (_tableMapping.ColumnMappings.Count == 0)) && (mappingAction == MissingMappingAction.Passthrough));

                for (int i = 0; i < count; ++i) {

                    bool ischapter = false;
                    Type fieldType = _dataReader.GetFieldType(i);

                    if (null == fieldType) {
                        throw ADP.MissingDataReaderFieldType(i);
                    }

                    // if IDataReader, hierarchy exists and we will use an Int32,AutoIncrementColumn in this table
                    if (typeof(IDataReader).IsAssignableFrom(fieldType)) {
                        if (null == chapterIndexMap) {
                            chapterIndexMap = new bool[count];
                        }
                        chapterIndexMap[i] = ischapter = true;
                        fieldType = typeof(Int32);
                    }
                    else if (typeof(System.Data.SqlTypes.SqlXml).IsAssignableFrom(fieldType)) {
                        if (null == _xmlMap) { // map to DataColumn with DataType=typeof(SqlXml)
                            _xmlMap = new int[count];
                        }
                        _xmlMap[i] = SqlXml; // track its xml data
                    }
                    else if (typeof(System.Xml.XmlReader).IsAssignableFrom(fieldType)) {
                        fieldType = typeof(String); // map to DataColumn with DataType=typeof(string)
                        if (null == _xmlMap) {
                            _xmlMap = new int[count];
                        }
                        _xmlMap[i] = XmlDocument; // track its xml data
                    }

                    DataColumn dataColumn;
                    if (alwaysCreateColumns) {
                        dataColumn = DataColumnMapping.CreateDataColumnBySchemaAction(_fieldNames[i], _fieldNames[i], _dataTable, fieldType, schemaAction);
                    }
                    else {
                        dataColumn = _tableMapping.GetDataColumn(_fieldNames[i], fieldType, _dataTable, mappingAction, schemaAction);
                    }

                    if (null == dataColumn) {
                        if (null == columnIndexMap) {
                            columnIndexMap = CreateIndexMap(count, i);
                        }
                        columnIndexMap[i] = -1;
                        continue; // null means ignore (mapped to nothing)
                    }
                    else if ((null != _xmlMap) && (0 != _xmlMap[i])) {
                        if (typeof(System.Data.SqlTypes.SqlXml) == dataColumn.DataType) {
                            _xmlMap[i] = SqlXml;
                        }
                        else if (typeof(System.Xml.XmlDocument) == dataColumn.DataType) {
                            _xmlMap[i] = XmlDocument;
                        }
                        else {
                            _xmlMap[i] = 0; // datacolumn is not a specific Xml dataType, i.e. string

                            int total = 0;
                            for(int x = 0; x < _xmlMap.Length; ++x) {
                                total += _xmlMap[x];
                            }
                            if (0 == total) { // not mapping to a specific Xml datatype, get rid of the map
                                _xmlMap = null;
                            }
                        }
                    }

                    if (null == dataColumn.Table) {
                        if (ischapter) {
                            dataColumn.AllowDBNull = false;
                            dataColumn.AutoIncrement = true;
                            dataColumn.ReadOnly = true;
                        }
                        AddItemToAllowRollback(ref addedItems, dataColumn);
                        columnCollection.Add(dataColumn);
                    }
                    else if (ischapter && !dataColumn.AutoIncrement) {
                        throw ADP.FillChapterAutoIncrement();
                    }


                    if (null != columnIndexMap) {
                        columnIndexMap[i] = dataColumn.Ordinal;
                    }
                    else if (i != dataColumn.Ordinal) {
                        columnIndexMap = CreateIndexMap(count, i);
                        columnIndexMap[i] = dataColumn.Ordinal;
                    }
                    // else i == dataColumn.Ordinal and columnIndexMap can be optimized out

                    mappingCount++;
                }
                bool addDataRelation = false;
                DataColumn chapterColumn = null;
                if (null != chapterValue) { // add the extra column in the child table
                    Type fieldType = chapterValue.GetType();

                    chapterColumn = _tableMapping.GetDataColumn(_tableMapping.SourceTable, fieldType, _dataTable, mappingAction, schemaAction);
                    if (null != chapterColumn) {

                        if (null == chapterColumn.Table) {
                            AddItemToAllowRollback(ref addedItems, chapterColumn);
                            columnCollection.Add(chapterColumn);
                            addDataRelation = (null != parentChapterColumn);
                        }
                        mappingCount++;
                    }
                }

                if (0 < mappingCount) {
                    if ((null != _dataSet) && (null == _dataTable.DataSet)) {
                        // Allowed to throw exception if DataTable is from wrong DataSet
                        AddItemToAllowRollback(ref addedItems, _dataTable);
                        _dataSet.Tables.Add(_dataTable);
                    }
                    if (gettingData) {
                        if (null == columnCollection) {
                            columnCollection = _dataTable.Columns;
                        }
                        _indexMap = columnIndexMap;
                        _chapterMap = chapterIndexMap;
                        dataValues = SetupMapping(count, columnCollection, chapterColumn, chapterValue);
                    }
                    else {
                        // debug only, but for retail debug ability
                        _mappedMode = -1;
                    }
                }
                else {
                    _dataTable = null;
                }

                if (addDataRelation) {
                    AddRelation(parentChapterColumn, chapterColumn);
                }

            }
            catch (Exception e) {
                // 
                if (ADP.IsCatchableOrSecurityExceptionType(e)) {
                    RollbackAddedItems(addedItems);
                }
                throw;
            }
            return dataValues;
        }
Exemple #50
0
		/// <summary>
		///     Creates or Modifies the schema of the given DataTable based on the schema of
		///     the reader and the arguments passed.
		/// </summary>
		internal static int[] BuildSchema (IDataReader reader, DataTable table,
                                                   SchemaType schemaType,
                                                   MissingSchemaAction missingSchAction,
                                                   MissingMappingAction missingMapAction,
                                                   DataTableMappingCollection dtMapping
                                                   )
		{
			int readerIndex = 0;
			// FIXME : this fails if query has fewer columns than a table
			int[] mapping = new int[table.Columns.Count]; // mapping the reader indexes to the datatable indexes
			
			for(int i=0; i < mapping.Length; i++) {
				mapping[i] = -1;
			}
			
			ArrayList primaryKey = new ArrayList ();
			ArrayList sourceColumns = new ArrayList ();
			bool createPrimaryKey = true;
			
			DataTable schemaTable = reader.GetSchemaTable ();

			DataColumn ColumnNameCol =  schemaTable.Columns["ColumnName"];
			DataColumn DataTypeCol = schemaTable.Columns["DataType"];
			DataColumn IsAutoIncrementCol = schemaTable.Columns["IsAutoIncrement"];
			DataColumn AllowDBNullCol = schemaTable.Columns["AllowDBNull"];
			DataColumn IsReadOnlyCol = schemaTable.Columns["IsReadOnly"];
			DataColumn IsKeyCol = schemaTable.Columns["IsKey"];
			DataColumn IsUniqueCol = schemaTable.Columns["IsUnique"];
			DataColumn ColumnSizeCol = schemaTable.Columns["ColumnSize"];

			foreach (DataRow schemaRow in schemaTable.Rows) {
				// generate a unique column name in the source table.
				string sourceColumnName;
				string realSourceColumnName ;
				if (ColumnNameCol == null || schemaRow.IsNull(ColumnNameCol) ||
				    (string)schemaRow [ColumnNameCol] == String.Empty) {
					sourceColumnName = DefaultSourceColumnName;
					realSourceColumnName = DefaultSourceColumnName + "1";
				} else {
					sourceColumnName = (string) schemaRow [ColumnNameCol];
					realSourceColumnName = sourceColumnName;
				}

				for (int i = 1; sourceColumns.Contains (realSourceColumnName); i += 1)
					realSourceColumnName = String.Format ("{0}{1}", sourceColumnName, i);
				sourceColumns.Add(realSourceColumnName);

				// generate DataSetColumnName from DataTableMapping, if any
				DataTableMapping tableMapping = null;

				//FIXME : The sourcetable name shud get passed as a parameter.. 
				int index = dtMapping.IndexOfDataSetTable (table.TableName);
				string srcTable = (index != -1 ? dtMapping[index].SourceTable : table.TableName);
				tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (dtMapping, srcTable, table.TableName, missingMapAction); 
				if (tableMapping != null) {
					table.TableName = tableMapping.DataSetTable;
					// check to see if the column mapping exists
					DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction(tableMapping.ColumnMappings, realSourceColumnName, missingMapAction);
					if (columnMapping != null) {
						Type columnType = schemaRow[DataTypeCol] as Type;
						DataColumn col = columnType != null ? columnMapping.GetDataColumnBySchemaAction(
						                                                                                table ,
						                                                                                columnType,
						                                                                                missingSchAction) : null;

						if (col != null) {
							// if the column is not in the table - add it.
							if (table.Columns.IndexOf(col) == -1) {
								if (missingSchAction == MissingSchemaAction.Add 
								    || missingSchAction == MissingSchemaAction.AddWithKey)
									table.Columns.Add(col);

								int[] tmp = new int[mapping.Length + 1];
								Array.Copy(mapping,0,tmp,0,col.Ordinal);
								Array.Copy(mapping,col.Ordinal,tmp,col.Ordinal + 1,mapping.Length - col.Ordinal);
								mapping = tmp;
							}

							if (missingSchAction == MissingSchemaAction.AddWithKey) {
								object value = (AllowDBNullCol != null) ? schemaRow[AllowDBNullCol] : null;
								bool allowDBNull = value is bool ? (bool)value : true;

								value = (IsKeyCol != null) ? schemaRow[IsKeyCol] : null;
								bool isKey = value is bool ? (bool)value : false;

								value = (IsAutoIncrementCol != null) ? schemaRow[IsAutoIncrementCol] : null;
								bool isAutoIncrement = value is bool ? (bool)value : false;

								value = (IsReadOnlyCol != null) ? schemaRow[IsReadOnlyCol] : null;
								bool isReadOnly = value is bool ? (bool)value : false;

								value = (IsUniqueCol != null) ? schemaRow[IsUniqueCol] : null;
								bool isUnique = value is bool ? (bool)value : false;
								
								col.AllowDBNull = allowDBNull;
								// fill woth key info
								if (isAutoIncrement && DataColumn.CanAutoIncrement(columnType)) {
									col.AutoIncrement = true;
									if (!allowDBNull)
										col.AllowDBNull = false;
								}

								if (columnType == DbTypes.TypeOfString) {
									col.MaxLength = (ColumnSizeCol != null) ? (int)schemaRow[ColumnSizeCol] : 0;
								}

								if (isReadOnly)
									col.ReadOnly = true;
									
								if (!allowDBNull && (!isReadOnly || isKey))
									col.AllowDBNull = false;
								if (isUnique && !isKey && !columnType.IsArray) {
									col.Unique = true;
									if (!allowDBNull)
										col.AllowDBNull = false;
								}
								
								// This might not be set by all DataProviders
								bool isHidden = false;
								if (schemaTable.Columns.Contains ("IsHidden")) {
									value = schemaRow["IsHidden"];
									isHidden = ((value is bool) ? (bool)value : false);
								}

								if (isKey && !isHidden) {
									primaryKey.Add (col);
									if (allowDBNull)
										createPrimaryKey = false;
								}
							}
							// add the ordinal of the column as a key and the index of the column in the datareader as a value.
							mapping[col.Ordinal] = readerIndex++;
						}
					}
				}
			}
			if (primaryKey.Count > 0) {
				DataColumn[] colKey = (DataColumn[])(primaryKey.ToArray(typeof (DataColumn)));
				if (createPrimaryKey)
					table.PrimaryKey = colKey;
				else {
					UniqueConstraint uConstraint = new UniqueConstraint(colKey);
					for (int i = 0; i < table.Constraints.Count; i++) {
						if (table.Constraints[i].Equals(uConstraint)) {
							uConstraint = null;
							break;
						}
					}

					if (uConstraint != null)
						table.Constraints.Add(uConstraint);
				}
			}
			return mapping;
		}
		// adjust the dataset schema according to the missingschemaaction param
		// (relations).
		// return false if adjusting fails.
		private static bool AdjustSchema(DataSet targetSet, DataSet sourceSet, MissingSchemaAction missingSchemaAction)
		{
			if (missingSchemaAction == MissingSchemaAction.Add || missingSchemaAction == MissingSchemaAction.AddWithKey) {
				foreach (DataRelation relation in sourceSet.Relations) {
					// TODO : add more precise condition (columns)
					if (!targetSet.Relations.Contains(relation.RelationName)) {
						DataTable targetTable = targetSet.Tables[relation.ParentColumns[0].Table.TableName];
						DataColumn[] parentColumns = ResolveColumns(sourceSet,targetTable,relation.ParentColumns);
						targetTable = targetSet.Tables[relation.ChildColumns[0].Table.TableName];
						DataColumn[] childColumns = ResolveColumns(sourceSet,targetTable,relation.ChildColumns);
						if (parentColumns != null && childColumns != null) {
							DataRelation newRelation = new DataRelation(relation.RelationName,parentColumns,childColumns);
							newRelation.Nested = relation.Nested; 
							targetSet.Relations.Add(newRelation);
						}
					}
					else {
						// TODO : should we throw an exeption ?
					}
				}			

				foreach(DataTable sourceTable in sourceSet.Tables) {				
					DataTable targetTable = targetSet.Tables[sourceTable.TableName];

					if (targetTable != null) {
						foreach(Constraint constraint in sourceTable.Constraints) {

							if (constraint is UniqueConstraint) {
								UniqueConstraint uc = (UniqueConstraint)constraint;
								// FIXME : add more precise condition (columns)
								if ( !targetTable.Constraints.Contains(uc.ConstraintName) ) {		
									DataColumn[] columns = ResolveColumns(sourceSet,targetTable,uc.Columns);
									if (columns != null) {
										UniqueConstraint newConstraint = new UniqueConstraint(uc.ConstraintName,columns,uc.IsPrimaryKey);
										targetTable.Constraints.Add(newConstraint);
									}
								}
								else {
									// FIXME : should we throw an exception ?
								}
							}
							else {
								ForeignKeyConstraint fc = (ForeignKeyConstraint)constraint;
								// FIXME : add more precise condition (columns)
								if (!targetTable.Constraints.Contains(fc.ConstraintName)) {
									DataColumn[] columns = ResolveColumns(sourceSet,targetTable,fc.Columns);
									DataTable relatedTable = targetSet.Tables[fc.RelatedTable.TableName];
									DataColumn[] relatedColumns = ResolveColumns(sourceSet,relatedTable,fc.RelatedColumns);
									if (columns != null && relatedColumns != null) {
										ForeignKeyConstraint newConstraint = new ForeignKeyConstraint(fc.ConstraintName,relatedColumns,columns);
										targetTable.Constraints.Add(newConstraint);
									}
								}
								else {
									// FIXME : should we throw an exception ?
								}
							}
						}
					}
				}
			}

			return true;
		}
Exemple #52
0
        internal static DataColumn CreateDataColumnBySchemaAction(string sourceColumn, string dataSetColumn, DataTable dataTable, Type dataType, MissingSchemaAction schemaAction)
        {
            Debug.Assert(dataTable != null, "Should not call with a null DataTable");
            if (string.IsNullOrEmpty(dataSetColumn))
            {
                return(null);
            }

            switch (schemaAction)
            {
            case MissingSchemaAction.Add:
            case MissingSchemaAction.AddWithKey:
                return(new DataColumn(dataSetColumn, dataType));

            case MissingSchemaAction.Ignore:
                return(null);

            case MissingSchemaAction.Error:
                throw ADP.ColumnSchemaMissing(dataSetColumn, dataTable.TableName, sourceColumn);
            }
            throw ADP.InvalidMissingSchemaAction(schemaAction);
        }
        internal static void Merge(DataSet targetSet, DataSet sourceSet, bool preserveChanges, MissingSchemaAction missingSchemaAction)
        {
            if (targetSet == null)
            {
                throw new ArgumentNullException("targetSet");
            }
            if (sourceSet == null)
            {
                throw new ArgumentNullException("sourceSet");
            }
            if (sourceSet == targetSet)
            {
                return;
            }

            bool prevEC = targetSet.EnforceConstraints;

            targetSet.EnforceConstraints = false;

            foreach (DataTable t in sourceSet.Tables)
            {
                MergeManager.Merge(targetSet, t, preserveChanges, missingSchemaAction);
            }

            AdjustSchemaRelations(targetSet, sourceSet, missingSchemaAction);
            targetSet.EnforceConstraints = prevEC;
        }
Exemple #54
0
        public static DataColumn GetDataColumnBySchemaAction(string sourceColumn, string dataSetColumn, DataTable dataTable, Type dataType, MissingSchemaAction schemaAction)
        {
            if (null == dataTable)
            {
                throw ADP.ArgumentNull(nameof(dataTable));
            }
            if (string.IsNullOrEmpty(dataSetColumn))
            {
                return(null);
            }
            DataColumnCollection columns = dataTable.Columns;

            Debug.Assert(null != columns, "GetDataColumnBySchemaAction: unexpected null DataColumnCollection");

            int index = columns.IndexOf(dataSetColumn);

            if ((0 <= index) && (index < columns.Count))
            {
                DataColumn dataColumn = columns[index];
                Debug.Assert(null != dataColumn, "GetDataColumnBySchemaAction: unexpected null dataColumn");

                if (!string.IsNullOrEmpty(dataColumn.Expression))
                {
                    throw ADP.ColumnSchemaExpression(sourceColumn, dataSetColumn);
                }

                if ((null == dataType) || (dataType.IsArray == dataColumn.DataType.IsArray))
                {
                    return(dataColumn);
                }

                throw ADP.ColumnSchemaMismatch(sourceColumn, dataType, dataColumn);
            }

            return(CreateDataColumnBySchemaAction(sourceColumn, dataSetColumn, dataTable, dataType, schemaAction));
        }
 private object[] SetupSchemaWithKeyInfo(MissingMappingAction mappingAction, MissingSchemaAction schemaAction, bool gettingData, DataColumn parentChapterColumn, object chapterValue)
 {
     DbSchemaRow[] sortedSchemaRows = DbSchemaRow.GetSortedSchemaRows(this._schemaTable, this._dataReader.ReturnProviderSpecificTypes);
     if (sortedSchemaRows.Length == 0)
     {
         this._dataTable = null;
         return null;
     }
     bool flag3 = ((this._dataTable.PrimaryKey.Length == 0) && ((((LoadOption) 4) <= this._loadOption) || (this._dataTable.Rows.Count == 0))) || (0 == this._dataTable.Columns.Count);
     DataColumn[] rgcol = null;
     int len = 0;
     bool flag2 = true;
     string str3 = null;
     string str2 = null;
     bool flag6 = false;
     bool flag = false;
     int[] numArray = null;
     bool[] flagArray = null;
     int num3 = 0;
     object[] objArray = null;
     List<object> items = null;
     DataColumnCollection columnCollection = this._dataTable.Columns;
     try
     {
         for (int i = 0; i < sortedSchemaRows.Length; i++)
         {
             DbSchemaRow row = sortedSchemaRows[i];
             int unsortedIndex = row.UnsortedIndex;
             bool flag5 = false;
             Type dataType = row.DataType;
             if (null == dataType)
             {
                 dataType = this._dataReader.GetFieldType(i);
             }
             if (null == dataType)
             {
                 throw ADP.MissingDataReaderFieldType(i);
             }
             if (typeof(IDataReader).IsAssignableFrom(dataType))
             {
                 if (flagArray == null)
                 {
                     flagArray = new bool[sortedSchemaRows.Length];
                 }
                 flagArray[unsortedIndex] = flag5 = true;
                 dataType = typeof(int);
             }
             else if (typeof(System.Data.SqlTypes.SqlXml).IsAssignableFrom(dataType))
             {
                 if (this._xmlMap == null)
                 {
                     this._xmlMap = new int[sortedSchemaRows.Length];
                 }
                 this._xmlMap[i] = 1;
             }
             else if (typeof(XmlReader).IsAssignableFrom(dataType))
             {
                 dataType = typeof(string);
                 if (this._xmlMap == null)
                 {
                     this._xmlMap = new int[sortedSchemaRows.Length];
                 }
                 this._xmlMap[i] = 2;
             }
             DataColumn targetColumn = null;
             if (!row.IsHidden)
             {
                 targetColumn = this._tableMapping.GetDataColumn(this._fieldNames[i], dataType, this._dataTable, mappingAction, schemaAction);
             }
             string baseTableName = row.BaseTableName;
             if (targetColumn == null)
             {
                 if (numArray == null)
                 {
                     numArray = this.CreateIndexMap(sortedSchemaRows.Length, unsortedIndex);
                 }
                 numArray[unsortedIndex] = -1;
                 if (row.IsKey && (flag6 || (row.BaseTableName == str3)))
                 {
                     flag3 = false;
                     rgcol = null;
                 }
             }
             else
             {
                 if ((this._xmlMap != null) && (this._xmlMap[i] != 0))
                 {
                     if (typeof(System.Data.SqlTypes.SqlXml) == targetColumn.DataType)
                     {
                         this._xmlMap[i] = 1;
                     }
                     else if (typeof(System.Xml.XmlDocument) == targetColumn.DataType)
                     {
                         this._xmlMap[i] = 2;
                     }
                     else
                     {
                         this._xmlMap[i] = 0;
                         int num7 = 0;
                         for (int j = 0; j < this._xmlMap.Length; j++)
                         {
                             num7 += this._xmlMap[j];
                         }
                         if (num7 == 0)
                         {
                             this._xmlMap = null;
                         }
                     }
                 }
                 if (row.IsKey && (baseTableName != str3))
                 {
                     if (str3 == null)
                     {
                         str3 = baseTableName;
                     }
                     else
                     {
                         flag6 = true;
                     }
                 }
                 if (flag5)
                 {
                     if (targetColumn.Table != null)
                     {
                         if (!targetColumn.AutoIncrement)
                         {
                             throw ADP.FillChapterAutoIncrement();
                         }
                     }
                     else
                     {
                         targetColumn.AllowDBNull = false;
                         targetColumn.AutoIncrement = true;
                         targetColumn.ReadOnly = true;
                     }
                 }
                 else
                 {
                     if ((!flag && (baseTableName != str2)) && !ADP.IsEmpty(baseTableName))
                     {
                         if (str2 == null)
                         {
                             str2 = baseTableName;
                         }
                         else
                         {
                             flag = true;
                         }
                     }
                     if (((LoadOption) 4) <= this._loadOption)
                     {
                         if (row.IsAutoIncrement && DataColumn.IsAutoIncrementType(dataType))
                         {
                             targetColumn.AutoIncrement = true;
                             if (!row.AllowDBNull)
                             {
                                 targetColumn.AllowDBNull = false;
                             }
                         }
                         if (dataType == typeof(string))
                         {
                             targetColumn.MaxLength = (row.Size > 0) ? row.Size : -1;
                         }
                         if (row.IsReadOnly)
                         {
                             targetColumn.ReadOnly = true;
                         }
                         if (!row.AllowDBNull && (!row.IsReadOnly || row.IsKey))
                         {
                             targetColumn.AllowDBNull = false;
                         }
                         if ((row.IsUnique && !row.IsKey) && !dataType.IsArray)
                         {
                             targetColumn.Unique = true;
                             if (!row.AllowDBNull)
                             {
                                 targetColumn.AllowDBNull = false;
                             }
                         }
                     }
                     else if (targetColumn.Table == null)
                     {
                         targetColumn.AutoIncrement = row.IsAutoIncrement;
                         targetColumn.AllowDBNull = row.AllowDBNull;
                         targetColumn.ReadOnly = row.IsReadOnly;
                         targetColumn.Unique = row.IsUnique;
                         if ((dataType == typeof(string)) || (dataType == typeof(SqlString)))
                         {
                             targetColumn.MaxLength = row.Size;
                         }
                     }
                 }
                 if (targetColumn.Table == null)
                 {
                     if (((LoadOption) 4) > this._loadOption)
                     {
                         this.AddAdditionalProperties(targetColumn, row.DataRow);
                     }
                     this.AddItemToAllowRollback(ref items, targetColumn);
                     columnCollection.Add(targetColumn);
                 }
                 if (flag3 && row.IsKey)
                 {
                     if (rgcol == null)
                     {
                         rgcol = new DataColumn[sortedSchemaRows.Length];
                     }
                     rgcol[len++] = targetColumn;
                     if (flag2 && targetColumn.AllowDBNull)
                     {
                         flag2 = false;
                     }
                 }
                 if (numArray != null)
                 {
                     numArray[unsortedIndex] = targetColumn.Ordinal;
                 }
                 else if (unsortedIndex != targetColumn.Ordinal)
                 {
                     numArray = this.CreateIndexMap(sortedSchemaRows.Length, unsortedIndex);
                     numArray[unsortedIndex] = targetColumn.Ordinal;
                 }
                 num3++;
             }
         }
         bool flag4 = false;
         DataColumn column2 = null;
         if (chapterValue != null)
         {
             Type type = chapterValue.GetType();
             column2 = this._tableMapping.GetDataColumn(this._tableMapping.SourceTable, type, this._dataTable, mappingAction, schemaAction);
             if (column2 != null)
             {
                 if (column2.Table == null)
                 {
                     column2.ReadOnly = true;
                     column2.AllowDBNull = false;
                     this.AddItemToAllowRollback(ref items, column2);
                     columnCollection.Add(column2);
                     flag4 = null != parentChapterColumn;
                 }
                 num3++;
             }
         }
         if (0 < num3)
         {
             if ((this._dataSet != null) && (this._dataTable.DataSet == null))
             {
                 this.AddItemToAllowRollback(ref items, this._dataTable);
                 this._dataSet.Tables.Add(this._dataTable);
             }
             if (flag3 && (rgcol != null))
             {
                 if (len < rgcol.Length)
                 {
                     rgcol = this.ResizeColumnArray(rgcol, len);
                 }
                 if (flag2)
                 {
                     this._dataTable.PrimaryKey = rgcol;
                 }
                 else
                 {
                     UniqueConstraint constraint = new UniqueConstraint("", rgcol);
                     ConstraintCollection constraints = this._dataTable.Constraints;
                     int count = constraints.Count;
                     for (int k = 0; k < count; k++)
                     {
                         if (constraint.Equals(constraints[k]))
                         {
                             constraint = null;
                             break;
                         }
                     }
                     if (constraint != null)
                     {
                         constraints.Add(constraint);
                     }
                 }
             }
             if ((!flag && !ADP.IsEmpty(str2)) && ADP.IsEmpty(this._dataTable.TableName))
             {
                 this._dataTable.TableName = str2;
             }
             if (gettingData)
             {
                 this._indexMap = numArray;
                 this._chapterMap = flagArray;
                 objArray = this.SetupMapping(sortedSchemaRows.Length, columnCollection, column2, chapterValue);
             }
             else
             {
                 this._mappedMode = -1;
             }
         }
         else
         {
             this._dataTable = null;
         }
         if (flag4)
         {
             this.AddRelation(parentChapterColumn, column2);
         }
     }
     catch (Exception exception)
     {
         if (ADP.IsCatchableOrSecurityExceptionType(exception))
         {
             this.RollbackAddedItems(items);
         }
         throw;
     }
     return objArray;
 }
        private object[] SetupSchemaWithKeyInfo(MissingMappingAction mappingAction, MissingSchemaAction schemaAction, bool gettingData, DataColumn parentChapterColumn, object chapterValue)
        {
            DbSchemaRow[] sortedSchemaRows = DbSchemaRow.GetSortedSchemaRows(this._schemaTable, this._dataReader.ReturnProviderSpecificTypes);
            if (sortedSchemaRows.Length == 0)
            {
                this._dataTable = null;
                return(null);
            }
            bool flag3 = ((this._dataTable.PrimaryKey.Length == 0) && ((((LoadOption)4) <= this._loadOption) || (this._dataTable.Rows.Count == 0))) || (0 == this._dataTable.Columns.Count);

            DataColumn[] rgcol = null;
            int          len   = 0;
            bool         flag2 = true;
            string       str3  = null;
            string       str2  = null;
            bool         flag6 = false;
            bool         flag  = false;

            int[]  numArray  = null;
            bool[] flagArray = null;
            int    num3      = 0;

            object[]             objArray         = null;
            List <object>        items            = null;
            DataColumnCollection columnCollection = this._dataTable.Columns;

            try
            {
                for (int i = 0; i < sortedSchemaRows.Length; i++)
                {
                    DbSchemaRow row           = sortedSchemaRows[i];
                    int         unsortedIndex = row.UnsortedIndex;
                    bool        flag5         = false;
                    Type        dataType      = row.DataType;
                    if (null == dataType)
                    {
                        dataType = this._dataReader.GetFieldType(i);
                    }
                    if (null == dataType)
                    {
                        throw ADP.MissingDataReaderFieldType(i);
                    }
                    if (typeof(IDataReader).IsAssignableFrom(dataType))
                    {
                        if (flagArray == null)
                        {
                            flagArray = new bool[sortedSchemaRows.Length];
                        }
                        flagArray[unsortedIndex] = flag5 = true;
                        dataType = typeof(int);
                    }
                    else if (typeof(System.Data.SqlTypes.SqlXml).IsAssignableFrom(dataType))
                    {
                        if (this._xmlMap == null)
                        {
                            this._xmlMap = new int[sortedSchemaRows.Length];
                        }
                        this._xmlMap[i] = 1;
                    }
                    else if (typeof(XmlReader).IsAssignableFrom(dataType))
                    {
                        dataType = typeof(string);
                        if (this._xmlMap == null)
                        {
                            this._xmlMap = new int[sortedSchemaRows.Length];
                        }
                        this._xmlMap[i] = 2;
                    }
                    DataColumn targetColumn = null;
                    if (!row.IsHidden)
                    {
                        targetColumn = this._tableMapping.GetDataColumn(this._fieldNames[i], dataType, this._dataTable, mappingAction, schemaAction);
                    }
                    string baseTableName = row.BaseTableName;
                    if (targetColumn == null)
                    {
                        if (numArray == null)
                        {
                            numArray = this.CreateIndexMap(sortedSchemaRows.Length, unsortedIndex);
                        }
                        numArray[unsortedIndex] = -1;
                        if (row.IsKey && (flag6 || (row.BaseTableName == str3)))
                        {
                            flag3 = false;
                            rgcol = null;
                        }
                    }
                    else
                    {
                        if ((this._xmlMap != null) && (this._xmlMap[i] != 0))
                        {
                            if (typeof(System.Data.SqlTypes.SqlXml) == targetColumn.DataType)
                            {
                                this._xmlMap[i] = 1;
                            }
                            else if (typeof(System.Xml.XmlDocument) == targetColumn.DataType)
                            {
                                this._xmlMap[i] = 2;
                            }
                            else
                            {
                                this._xmlMap[i] = 0;
                                int num7 = 0;
                                for (int j = 0; j < this._xmlMap.Length; j++)
                                {
                                    num7 += this._xmlMap[j];
                                }
                                if (num7 == 0)
                                {
                                    this._xmlMap = null;
                                }
                            }
                        }
                        if (row.IsKey && (baseTableName != str3))
                        {
                            if (str3 == null)
                            {
                                str3 = baseTableName;
                            }
                            else
                            {
                                flag6 = true;
                            }
                        }
                        if (flag5)
                        {
                            if (targetColumn.Table != null)
                            {
                                if (!targetColumn.AutoIncrement)
                                {
                                    throw ADP.FillChapterAutoIncrement();
                                }
                            }
                            else
                            {
                                targetColumn.AllowDBNull   = false;
                                targetColumn.AutoIncrement = true;
                                targetColumn.ReadOnly      = true;
                            }
                        }
                        else
                        {
                            if ((!flag && (baseTableName != str2)) && !ADP.IsEmpty(baseTableName))
                            {
                                if (str2 == null)
                                {
                                    str2 = baseTableName;
                                }
                                else
                                {
                                    flag = true;
                                }
                            }
                            if (((LoadOption)4) <= this._loadOption)
                            {
                                if (row.IsAutoIncrement && DataColumn.IsAutoIncrementType(dataType))
                                {
                                    targetColumn.AutoIncrement = true;
                                    if (!row.AllowDBNull)
                                    {
                                        targetColumn.AllowDBNull = false;
                                    }
                                }
                                if (dataType == typeof(string))
                                {
                                    targetColumn.MaxLength = (row.Size > 0) ? row.Size : -1;
                                }
                                if (row.IsReadOnly)
                                {
                                    targetColumn.ReadOnly = true;
                                }
                                if (!row.AllowDBNull && (!row.IsReadOnly || row.IsKey))
                                {
                                    targetColumn.AllowDBNull = false;
                                }
                                if ((row.IsUnique && !row.IsKey) && !dataType.IsArray)
                                {
                                    targetColumn.Unique = true;
                                    if (!row.AllowDBNull)
                                    {
                                        targetColumn.AllowDBNull = false;
                                    }
                                }
                            }
                            else if (targetColumn.Table == null)
                            {
                                targetColumn.AutoIncrement = row.IsAutoIncrement;
                                targetColumn.AllowDBNull   = row.AllowDBNull;
                                targetColumn.ReadOnly      = row.IsReadOnly;
                                targetColumn.Unique        = row.IsUnique;
                                if ((dataType == typeof(string)) || (dataType == typeof(SqlString)))
                                {
                                    targetColumn.MaxLength = row.Size;
                                }
                            }
                        }
                        if (targetColumn.Table == null)
                        {
                            if (((LoadOption)4) > this._loadOption)
                            {
                                this.AddAdditionalProperties(targetColumn, row.DataRow);
                            }
                            this.AddItemToAllowRollback(ref items, targetColumn);
                            columnCollection.Add(targetColumn);
                        }
                        if (flag3 && row.IsKey)
                        {
                            if (rgcol == null)
                            {
                                rgcol = new DataColumn[sortedSchemaRows.Length];
                            }
                            rgcol[len++] = targetColumn;
                            if (flag2 && targetColumn.AllowDBNull)
                            {
                                flag2 = false;
                            }
                        }
                        if (numArray != null)
                        {
                            numArray[unsortedIndex] = targetColumn.Ordinal;
                        }
                        else if (unsortedIndex != targetColumn.Ordinal)
                        {
                            numArray = this.CreateIndexMap(sortedSchemaRows.Length, unsortedIndex);
                            numArray[unsortedIndex] = targetColumn.Ordinal;
                        }
                        num3++;
                    }
                }
                bool       flag4   = false;
                DataColumn column2 = null;
                if (chapterValue != null)
                {
                    Type type = chapterValue.GetType();
                    column2 = this._tableMapping.GetDataColumn(this._tableMapping.SourceTable, type, this._dataTable, mappingAction, schemaAction);
                    if (column2 != null)
                    {
                        if (column2.Table == null)
                        {
                            column2.ReadOnly    = true;
                            column2.AllowDBNull = false;
                            this.AddItemToAllowRollback(ref items, column2);
                            columnCollection.Add(column2);
                            flag4 = null != parentChapterColumn;
                        }
                        num3++;
                    }
                }
                if (0 < num3)
                {
                    if ((this._dataSet != null) && (this._dataTable.DataSet == null))
                    {
                        this.AddItemToAllowRollback(ref items, this._dataTable);
                        this._dataSet.Tables.Add(this._dataTable);
                    }
                    if (flag3 && (rgcol != null))
                    {
                        if (len < rgcol.Length)
                        {
                            rgcol = this.ResizeColumnArray(rgcol, len);
                        }
                        if (flag2)
                        {
                            this._dataTable.PrimaryKey = rgcol;
                        }
                        else
                        {
                            UniqueConstraint     constraint  = new UniqueConstraint("", rgcol);
                            ConstraintCollection constraints = this._dataTable.Constraints;
                            int count = constraints.Count;
                            for (int k = 0; k < count; k++)
                            {
                                if (constraint.Equals(constraints[k]))
                                {
                                    constraint = null;
                                    break;
                                }
                            }
                            if (constraint != null)
                            {
                                constraints.Add(constraint);
                            }
                        }
                    }
                    if ((!flag && !ADP.IsEmpty(str2)) && ADP.IsEmpty(this._dataTable.TableName))
                    {
                        this._dataTable.TableName = str2;
                    }
                    if (gettingData)
                    {
                        this._indexMap   = numArray;
                        this._chapterMap = flagArray;
                        objArray         = this.SetupMapping(sortedSchemaRows.Length, columnCollection, column2, chapterValue);
                    }
                    else
                    {
                        this._mappedMode = -1;
                    }
                }
                else
                {
                    this._dataTable = null;
                }
                if (flag4)
                {
                    this.AddRelation(parentChapterColumn, column2);
                }
            }
            catch (Exception exception)
            {
                if (ADP.IsCatchableOrSecurityExceptionType(exception))
                {
                    this.RollbackAddedItems(items);
                }
                throw;
            }
            return(objArray);
        }
Exemple #57
0
 // System.Data.Common.DataColumnMapping
 internal static DataColumn CreateDataColumnBySchemaAction(string sourceColumn, string dataSetColumn, DataTable dataTable, Type dataType, MissingSchemaAction schemaAction)
 {
     if (string.IsNullOrEmpty(dataSetColumn))
     {
         return null;
     }
     switch (schemaAction)
     {
         case MissingSchemaAction.Add:
         case MissingSchemaAction.AddWithKey:
             return new DataColumn(dataSetColumn, dataType);
         case MissingSchemaAction.Ignore:
             return null;
         case MissingSchemaAction.Error:
         default:
             throw new EntryPointNotFoundException();
     }
 }
		public static DataColumn GetDataColumn (DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
		{
			throw new NotImplementedException ();
		}
        public void Merge(DataTable table, bool preserveChanges, MissingSchemaAction missingSchemaAction)
        {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.Merge|API> %d#, table=%d, preserveChanges=%d{bool}, missingSchemaAction=%d{ds.MissingSchemaAction}\n", ObjectID, (table != null) ? table.ObjectID : 0, preserveChanges, (int)missingSchemaAction);
            try{
                if (table == null)
                    throw ExceptionBuilder.ArgumentNull("table");

                switch(missingSchemaAction) { // @perfnote: Enum.IsDefined
                case MissingSchemaAction.Add:
                case MissingSchemaAction.Ignore:
                case MissingSchemaAction.Error:
                case MissingSchemaAction.AddWithKey:
                    Merger merger = new Merger(this, preserveChanges, missingSchemaAction);
                    merger.MergeTable(table);
                    break;
                default:
                    throw Common.ADP.InvalidMissingSchemaAction(missingSchemaAction);
                }
            }
            finally{
                Bid.ScopeLeave(ref hscp);
            }
        }
Exemple #60
0
		private static bool IsLegalSchemaAction (MissingSchemaAction missingSchemaAction)
		{
			if (missingSchemaAction == MissingSchemaAction.Add || missingSchemaAction == MissingSchemaAction.AddWithKey
				|| missingSchemaAction == MissingSchemaAction.Error || missingSchemaAction == MissingSchemaAction.Ignore)
				return true;
			return false;
		}