protected DataAdapter () { acceptChangesDuringFill = true; continueUpdateOnError = false; missingMappingAction = MissingMappingAction.Passthrough; missingSchemaAction = MissingSchemaAction.Add; tableMappings = new DataTableMappingCollection (); }
public void AddException1() { Assert.Throws<InvalidCastException>(() => { DataTableMappingCollection c = new DataTableMappingCollection(); _tableMapCollection.Add(c); }); }
public void GetReady() { tabs=new DataTableMapping[5]; tabs[0]=new DataTableMapping("sourceCustomers", "dataSetCustomers"); tabs[1]=new DataTableMapping("sourceEmployees", "dataSetEmployees"); tabs[2]=new DataTableMapping("sourceBooks", "dataSetBooks"); tabs[3]=new DataTableMapping("sourceStore", "dataSetStore"); tabs[4]=new DataTableMapping("sourceInventory", "dataSetInventory"); tableMapCollection=new DataTableMappingCollection(); }
protected override void Dispose(bool disposing) { if (disposing) { // release mananged objects _tableMappings = null; } // release unmanaged objects base.Dispose(disposing); // notify base classes }
protected DataAdapter () { acceptChangesDuringFill = true; continueUpdateOnError = false; missingMappingAction = MissingMappingAction.Passthrough; missingSchemaAction = MissingSchemaAction.Add; tableMappings = new DataTableMappingCollection (); acceptChangesDuringUpdate = true; fillLoadOption = LoadOption.OverwriteChanges; returnProviderSpecificTypes = false; }
[EditorBrowsableAttribute(EditorBrowsableState.Advanced)] // MDAC 69508 static public DataTableMapping GetTableMappingBySchemaAction(DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction) { if (null != tableMappings) { int index = tableMappings.IndexOf(sourceTable); if (-1 != index) { #if DEBUG if (AdapterSwitches.DataSchema.TraceWarning) { Debug.WriteLine("mapping match on SourceTable \"" + sourceTable + "\""); } #endif return((DataTableMapping)tableMappings.items[index]); } } if (ADP.IsEmpty(sourceTable)) { throw ADP.InvalidSourceTable("sourceTable"); } switch (mappingAction) { case MissingMappingAction.Passthrough: #if DEBUG if (AdapterSwitches.DataSchema.TraceInfo) { Debug.WriteLine("mapping passthrough of SourceTable \"" + sourceTable + "\" -> \"" + dataSetTable + "\""); } #endif return(new DataTableMapping(sourceTable, dataSetTable)); case MissingMappingAction.Ignore: #if DEBUG if (AdapterSwitches.DataSchema.TraceWarning) { Debug.WriteLine("mapping filter of SourceTable \"" + sourceTable + "\""); } #endif return(null); case MissingMappingAction.Error: #if DEBUG if (AdapterSwitches.DataSchema.TraceError) { Debug.WriteLine("mapping error on SourceTable \"" + sourceTable + "\""); } #endif throw ADP.MissingTableMapping(sourceTable); default: throw ADP.InvalidMappingAction((int)mappingAction); } }
public int Update(DataSet dataSet, string srcTable) { MissingMappingAction mappingAction = MissingMappingAction; if (mappingAction == MissingMappingAction.Ignore) { mappingAction = MissingMappingAction.Error; } DataTableMapping tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(TableMappings, srcTable, srcTable, mappingAction); DataTable dataTable = dataSet.Tables [tableMapping.DataSetTable]; if (dataTable == null) { throw new ArgumentException(String.Format("Missing table {0}", srcTable)); } /** Copied from another Update function **/ if (tableMapping != null) { foreach (DataColumn col in dataTable.Columns) { if (tableMapping.ColumnMappings.IndexOf(col.ColumnName) >= 0) { continue; } DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction(tableMapping.ColumnMappings, col.ColumnName, MissingMappingAction); if (columnMapping == null) { columnMapping = new DataColumnMapping(col.ColumnName, col.ColumnName); } tableMapping.ColumnMappings.Add(columnMapping); } } else { ArrayList cmc = new ArrayList(); foreach (DataColumn col in dataTable.Columns) { cmc.Add(new DataColumnMapping(col.ColumnName, col.ColumnName)); } tableMapping = new DataTableMapping( dataTable.TableName, dataTable.TableName, cmc.ToArray(typeof(DataColumnMapping)) as DataColumnMapping []); } /**end insert from another update**/ return(Update(dataTable, tableMapping)); }
protected DataAdapter() { acceptChangesDuringFill = true; continueUpdateOnError = false; missingMappingAction = MissingMappingAction.Passthrough; missingSchemaAction = MissingSchemaAction.Add; tableMappings = new DataTableMappingCollection(); #if NET_2_0 acceptChangesDuringUpdate = true; fillLoadOption = LoadOption.OverwriteChanges; returnProviderSpecificTypes = false; #endif }
public int Update(DataTable dataTable) { /* * int index = TableMappings.IndexOfDataSetTable (dataTable.TableName); * if (index < 0) * throw new ArgumentException (); * return Update (dataTable, TableMappings [index]); */ DataTableMapping tableMapping = TableMappings.GetByDataSetTable(dataTable.TableName); if (tableMapping == null) { tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction( TableMappings, dataTable.TableName, dataTable.TableName, MissingMappingAction); if (tableMapping != null) { foreach (DataColumn col in dataTable.Columns) { if (tableMapping.ColumnMappings.IndexOf(col.ColumnName) >= 0) { continue; } DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction(tableMapping.ColumnMappings, col.ColumnName, MissingMappingAction); if (columnMapping == null) { columnMapping = new DataColumnMapping(col.ColumnName, col.ColumnName); } tableMapping.ColumnMappings.Add(columnMapping); } } else { ArrayList cmc = new ArrayList(); foreach (DataColumn col in dataTable.Columns) { cmc.Add(new DataColumnMapping(col.ColumnName, col.ColumnName)); } tableMapping = new DataTableMapping( dataTable.TableName, dataTable.TableName, cmc.ToArray(typeof(DataColumnMapping)) as DataColumnMapping []); } } return(Update(dataTable, tableMapping)); }
public void SourceTableParentValidation() { // Sets dataTableMappingCollection as the dataTableMapping Parent. DataTableMappingCollection dataTableMappingCollection = new DataTableMappingCollection(); dataTableMappingCollection.Add(_dataTableMapping); // Adds a data table with another source table name. DataTableMapping secondDataTableMapping = new DataTableMapping("AnotherCustomSourceTable", ""); dataTableMappingCollection.Add(secondDataTableMapping); // Attempts to change the second data table source table with a repeated value. Assert.Throws <ArgumentException>(() => secondDataTableMapping.SourceTable = "MyCustomSourceTable"); }
/// <summary> /// 创建默认表列映射 /// </summary> /// <param name="ds"></param> /// <returns></returns> public static System.Data.Common.DataTableMappingCollection CreateDefaultMapping(System.Data.DataSet ds) { DataTableMappingCollection rtn = new DataTableMappingCollection(); foreach (System.Data.DataTable dt in ds.Tables) { DataTableMapping dtm = rtn.Add(dt.TableName, dt.TableName); foreach (DataColumn dc in dt.Columns) { dtm.ColumnMappings.Add(dc.ColumnName, dc.ColumnName); } } return rtn; }
/// <include file='doc\DataAdapter.uex' path='docs/doc[@for="DataAdapter.DataAdapter1"]/*' /> protected DataAdapter(DataAdapter adapter) : base() // MDAC 81448 { AcceptChangesDuringFill = adapter.AcceptChangesDuringFill; ContinueUpdateOnError = adapter.ContinueUpdateOnError; MissingMappingAction = adapter.MissingMappingAction; MissingSchemaAction = adapter.MissingSchemaAction; if ((null != adapter.tableMappings) && (0 < adapter.TableMappings.Count)) { DataTableMappingCollection parameters = this.TableMappings; foreach (ICloneable parameter in adapter.TableMappings) { parameters.Add(parameter.Clone()); } } }
public static DataTableMapping GetTableMappingBySchemaAction(DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction) { if (tableMappings.Contains(sourceTable)) { return(tableMappings[sourceTable]); } if (mappingAction == MissingMappingAction.Error) { throw new InvalidOperationException(String.Format("Missing source table mapping: '{0}'", sourceTable)); } if (mappingAction == MissingMappingAction.Ignore) { return(null); } return(new DataTableMapping(sourceTable, dataSetTable)); }
internal string SetupSchema(SchemaType schemaType, string sourceTableName) { DataTableMapping tableMapping = null; if (schemaType == SchemaType.Mapped) { tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(TableMappings, sourceTableName, sourceTableName, MissingMappingAction); if (tableMapping != null) { return(tableMapping.DataSetTable); } return(null); } else { return(sourceTableName); } }
[System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] // MDAC 82936 virtual protected DataAdapter CloneInternals() { DataAdapter clone = (DataAdapter)Activator.CreateInstance(GetType()); clone.AcceptChangesDuringFill = AcceptChangesDuringFill; clone.ContinueUpdateOnError = ContinueUpdateOnError; clone.MissingMappingAction = MissingMappingAction; clone.MissingSchemaAction = MissingSchemaAction; if ((null != this.tableMappings) && (0 < TableMappings.Count)) { DataTableMappingCollection parameters = clone.TableMappings; foreach (ICloneable parameter in TableMappings) { parameters.Add(parameter.Clone()); } } return(clone); }
private void CloneFrom(DataAdapter from) { this._acceptChangesDuringUpdate = from._acceptChangesDuringUpdate; this._acceptChangesDuringUpdateAfterInsert = from._acceptChangesDuringUpdateAfterInsert; this._continueUpdateOnError = from._continueUpdateOnError; this._returnProviderSpecificTypes = from._returnProviderSpecificTypes; this._acceptChangesDuringFill = from._acceptChangesDuringFill; this._fillLoadOption = from._fillLoadOption; this._missingMappingAction = from._missingMappingAction; this._missingSchemaAction = from._missingSchemaAction; if ((from._tableMappings != null) && (0 < from.TableMappings.Count)) { DataTableMappingCollection tableMappings = this.TableMappings; foreach (object obj2 in from.TableMappings) { tableMappings.Add((obj2 is ICloneable) ? ((ICloneable)obj2).Clone() : obj2); } } }
private void CloneFrom(DataAdapter from) { _acceptChangesDuringUpdate = from._acceptChangesDuringUpdate; _acceptChangesDuringUpdateAfterInsert = from._acceptChangesDuringUpdateAfterInsert; _continueUpdateOnError = from._continueUpdateOnError; _returnProviderSpecificTypes = from._returnProviderSpecificTypes; _acceptChangesDuringFill = from._acceptChangesDuringFill; _fillLoadOption = from._fillLoadOption; _missingMappingAction = from._missingMappingAction; _missingSchemaAction = from._missingSchemaAction; if ((null != from._tableMappings) && (0 < from.TableMappings.Count)) { DataTableMappingCollection parameters = TableMappings; foreach (object parameter in from.TableMappings) { parameters.Add((parameter is ICloneable) ? ((ICloneable)parameter).Clone() : parameter); } } }
public int Update(DataSet dataSet, string srcTable) { MissingMappingAction mappingAction = MissingMappingAction; if (mappingAction == MissingMappingAction.Ignore) { mappingAction = MissingMappingAction.Error; } DataTableMapping tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(TableMappings, srcTable, srcTable, mappingAction); DataTable dataTable = dataSet.Tables [tableMapping.DataSetTable]; if (dataTable == null) { throw new ArgumentException(String.Format("Missing table {0}", srcTable)); } return(Update(dataTable, tableMapping)); }
internal DataTableMapping GetTableMappingBySchemaAction(string sourceTableName, string dataSetTableName, MissingMappingAction mappingAction) { return(DataTableMappingCollection.GetTableMappingBySchemaAction(_tableMappings, sourceTableName, dataSetTableName, mappingAction)); }
/// <summary> /// Build a DataTable for the form's datagrid from the /// existing TableMappings. /// </summary> /// <param name="dt">2-column DataTable to hold the names.</param> /// <param name="mapList">TableMapping collection usually /// from the DataAdapter.</param> /// <returns></returns> private bool BuildMapFromExistingMap( System.Data.DataTable dt, DataTableMappingCollection mapList) { DataTableMapping map = mapList[0]; string sourceTable = map.SourceTable; string datasetTable= map.DataSetTable; comboBoxSourceTable.Text = sourceTable; textBoxDatasetTable.Text = datasetTable; // copy the column mappings to the 2-column DataTable // that will be the DataSource for the datagrid foreach (DataColumnMapping colMap in map.ColumnMappings) { System.Data.DataRow datarow = dt.NewRow(); datarow[ TableMappings.SOURCE_COLUMNS ] = colMap.SourceColumn; datarow[ TableMappings.DATASET_COLUMNS ] = colMap.DataSetColumn; dt.Rows.Add( datarow ); } return true; }
public static DataTableMapping GetTableMappingBySchemaAction (DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction) { if (tableMappings.Contains (sourceTable)) return tableMappings[sourceTable]; if (mappingAction == MissingMappingAction.Error) throw new InvalidOperationException (); if (mappingAction == MissingMappingAction.Ignore) return null; return new DataTableMapping (sourceTable, dataSetTable); }
internal void SetupSchema(SchemaType schemaType, string sourceTableName, bool gettingData, DataColumn parentChapterColumn, object parentChapterValue) { #if DEBUG Debug.Assert(null != this.dataSet || null != this.dataTable, "SetupSchema - null dataSet"); Debug.Assert(SchemaType.Mapped == schemaType || SchemaType.Source == schemaType, "SetupSchema - invalid schemaType"); #endif MissingMappingAction mappingAction; MissingSchemaAction schemaAction; if (SchemaType.Mapped == schemaType) { mappingAction = this.adapter.MissingMappingAction; schemaAction = this.adapter.MissingSchemaAction; if (!ADP.IsEmpty(sourceTableName)) // MDAC 66034 { tableMapping = this.adapter.GetTableMappingBySchemaAction(sourceTableName, sourceTableName, mappingAction); } else if (null != this.dataTable) { int index = this.adapter.IndexOfDataSetTable(this.dataTable.TableName); if (-1 != index) { tableMapping = this.adapter.TableMappings[index]; } else { switch (mappingAction) { case MissingMappingAction.Passthrough: tableMapping = new DataTableMapping(this.dataTable.TableName, this.dataTable.TableName); break; case MissingMappingAction.Ignore: tableMapping = null; break; case MissingMappingAction.Error: throw ADP.MissingTableMappingDestination(this.dataTable.TableName); default: throw ADP.InvalidMappingAction((int)mappingAction); } } } } else if (SchemaType.Source == schemaType) { mappingAction = System.Data.MissingMappingAction.Passthrough; schemaAction = Data.MissingSchemaAction.Add; if (!ADP.IsEmpty(sourceTableName)) // MDAC 66034 { tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(null, sourceTableName, sourceTableName, mappingAction); } else if (null != this.dataTable) { int index = this.adapter.IndexOfDataSetTable(this.dataTable.TableName); // MDAC 66034 if (-1 != index) { tableMapping = this.adapter.TableMappings[index]; } else { tableMapping = new DataTableMapping(this.dataTable.TableName, this.dataTable.TableName); } } } else { throw ADP.InvalidSchemaType((int)schemaType); } if (null == tableMapping) { return; } if (null == this.dataTable) { this.dataTable = tableMapping.GetDataTableBySchemaAction(this.dataSet, schemaAction); if (null == this.dataTable) { return; // null means ignore (mapped to nothing) } } if (null == this.schemaTable) { SetupSchemaWithoutKeyInfo(mappingAction, schemaAction, gettingData, parentChapterColumn, parentChapterValue); } else { SetupSchemaWithKeyInfo(mappingAction, schemaAction, gettingData, parentChapterColumn, parentChapterValue); } }
public virtual int DataTableFillWithParams(DataTable dataTable, CommandType commandType, string sql, IDbParameters parameters, ITableMapping tableMapping, IDataAdapterSetter dataAdapterSetter) { ValidateFillWithParameterArguments(dataTable, sql, parameters, tableMapping); ITableMappingCollection mappingCollection = new DataTableMappingCollection(); mappingCollection.Add((object)tableMapping); return (int)Execute(new DataAdapterFillCallback(dataTable, commandType, sql, mappingCollection, dataAdapterSetter, null, parameters)); }
public virtual int DataTableFill(DataTable dataTable, CommandType commandType, string sql, ITableMapping tableMapping) { ValidateFillArguments(dataTable, sql, tableMapping); ITableMappingCollection mappingCollection = new DataTableMappingCollection(); mappingCollection.Add((object)tableMapping); return (int)Execute(new DataAdapterFillCallback(dataTable, commandType, sql, mappingCollection, null, null, null)); }
/// <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); }
protected virtual DataTableMappingCollection CreateTableMappings () { tableMappings = new DataTableMappingCollection (); return tableMappings; }
public int Update(DataRow [] dataRows) { if (dataRows == null) { throw new ArgumentNullException("dataRows"); } if (dataRows.Length == 0) { return(0); } if (dataRows [0] == null) { throw new ArgumentException("dataRows[0]."); } DataTable table = dataRows [0].Table; if (table == null) { throw new ArgumentException("table is null reference."); } // all rows must be in the same table for (int i = 0; i < dataRows.Length; i++) { if (dataRows [i] == null) { throw new ArgumentException("dataRows[" + i + "]."); } if (dataRows [i].Table != table) { throw new ArgumentException( " DataRow[" + i + "] is from a different DataTable than DataRow[0]."); } } // get table mapping for this rows DataTableMapping tableMapping = TableMappings.GetByDataSetTable(table.TableName); if (tableMapping == null) { tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction( TableMappings, table.TableName, table.TableName, MissingMappingAction); if (tableMapping != null) { foreach (DataColumn col in table.Columns) { if (tableMapping.ColumnMappings.IndexOf(col.ColumnName) >= 0) { continue; } DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction(tableMapping.ColumnMappings, col.ColumnName, MissingMappingAction); if (columnMapping == null) { columnMapping = new DataColumnMapping(col.ColumnName, col.ColumnName); } tableMapping.ColumnMappings.Add(columnMapping); } } else { ArrayList cmc = new ArrayList(); foreach (DataColumn col in table.Columns) { cmc.Add(new DataColumnMapping(col.ColumnName, col.ColumnName)); } tableMapping = new DataTableMapping( table.TableName, table.TableName, cmc.ToArray(typeof(DataColumnMapping)) as DataColumnMapping []); } } DataRow[] copy = table.NewRowArray(dataRows.Length); Array.Copy(dataRows, 0, copy, 0, dataRows.Length); return(Update(copy, tableMapping)); }
/// <summary>Releases the unmanaged resources used by the <see cref="T:System.Data.Common.DataAdapter" /> and optionally releases the managed resources.</summary> /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources. </param> protected override void Dispose(bool disposing) { if (disposing) { this._tableMappings = null; } base.Dispose(disposing); }
public virtual int DataTableUpdateWithCommandBuilder(DataTable dataTable, CommandType commandType, string selectSql, IDbParameters parameters, ITableMapping tableMapping, IDataAdapterSetter dataAdapterSetter) { ValidateUpdateWithCommandBuilderArguments(dataTable, tableMapping, selectSql); ITableMappingCollection mappingCollection = new DataTableMappingCollection(); mappingCollection.Add(tableMapping); return (int)Execute(new DataAdapterUpdateWithCommandBuilderCallback(dataTable, DbProvider.CreateCommandBuilder(), mappingCollection, commandType, selectSql, parameters, dataAdapterSetter)); }
public void AddException1() { DataTableMappingCollection c=new DataTableMappingCollection(); tableMapCollection.Add((Object)c); }
public virtual int DataTableUpdate(DataTable dataTable, ITableMapping tableMapping, CommandType insertCommandtype, string insertSql, IDbParameters insertParameters, CommandType updateCommandtype, string updateSql, IDbParameters updateParameters, CommandType deleteCommandtype, string deleteSql, IDbParameters deleteParameters, IDataAdapterSetter dataAdapterSetter) { ValidateUpdateArguments(dataTable, tableMapping); ITableMappingCollection mappingCollection = new DataTableMappingCollection(); mappingCollection.Add((object)tableMapping); return DataTableUpdate(dataTable, mappingCollection, insertCommandtype, insertSql, insertParameters, updateCommandtype, updateSql, updateParameters, deleteCommandtype, deleteSql, deleteParameters, dataAdapterSetter); }
/// <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; }
protected virtual ITableMappingCollection DoCreateMappingCollection(string[] dataSetTableNames) { DataTableMappingCollection mappingCollection; if (dataSetTableNames == null) { dataSetTableNames = new string[] { "Table" }; } foreach (string tableName in dataSetTableNames) { if (StringUtils.IsNullOrEmpty(tableName)) { throw new ArgumentException("TableName for DataTable mapping can not be null or empty"); } } mappingCollection = new DataTableMappingCollection(); int counter = 0; bool isFirstTable = true; foreach (string dataSetTableName in dataSetTableNames) { string sourceTableName; if (isFirstTable) { sourceTableName = "Table"; isFirstTable = false; } else { sourceTableName = "Table" + ++counter; } mappingCollection.Add(sourceTableName, dataSetTableName); } return mappingCollection; }
override protected void Dispose(bool disposing) { // V1.0.3300, MDAC 65459 if (disposing) { // release mananged objects _tableMappings = null; } // release unmanaged objects base.Dispose(disposing); // notify base classes }
/// <summary> /// [Microsoft.Office.Interop.Excel]建立表列结构(保留不使用的页(Worksheet).(创建/覆盖文件) /// </summary> /// <param name="FileName">文件路径</param> /// <param name="dtmc">取其 Source 属性值建立空文件</param> public static void BuildSheets(string FileName, DataTableMappingCollection dtmc) { Application app = new Application(); try { Workbook wb; if (File.Exists(FileName)) { wb = app.Workbooks.Open(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing , true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); } else { wb = app.Workbooks.Add(Type.Missing); } foreach (DataTableMapping dtm in dtmc) { try { Worksheet ws; try { ws = wb.Worksheets[dtm.SourceTable] as Worksheet; // 清空表 ws.UsedRange.ClearContents(); } catch { ws = wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing) as Worksheet; // 表名 ws.Name = dtm.SourceTable; } ((_Worksheet)ws).Activate(); // 列名 for (int i = 1; i <= dtm.ColumnMappings.Count; i++) { DataColumnMapping dcm = dtm.ColumnMappings[i - 1]; ws.Cells[1, i] = dcm.SourceColumn; } } catch (System.Exception ex) { Apq.GlobalObject.ApqLog.Warn(string.Format("构建文件结构[{0}]失败:", dtm.SourceTable), ex); } } // 保存 if (File.Exists(FileName)) { wb.Save(); } else { wb.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing , XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing, Type.Missing, true); } // 关闭 wb.Close(XlSaveAction.xlDoNotSaveChanges, FileName, false); wb = null; } finally { try { Int64 appID = 0; IntPtr hwnd = new IntPtr(app.Hwnd); int appThreadID = Apq.DllImports.User32.GetWindowThreadProcessId(hwnd, ref appID); // 退出 Excel app.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); if (appID > 0) { try { Process pr = Process.GetProcessById(System.Convert.ToInt32(appID)); pr.Kill(); } catch { } } app = null; GC.Collect(); } catch (System.Exception ex) { Apq.GlobalObject.ApqLog.Warn("关闭Excel失败", ex); } } }
public void DataSetFillNoParams() { String sql = "select USER_ID, USER_NAME from USER_TABLE"; DataSet dataSet = new DataSet(); adoOperations.DataSetFill(dataSet, CommandType.Text, sql); Assert.AreEqual(1, dataSet.Tables.Count); Assert.AreEqual(18, dataSet.Tables["Table"].Rows.Count); dataSet = new DataSet(); adoOperations.DataSetFill(dataSet, CommandType.Text, sql, new string[] {"TestObjects"}); Assert.AreEqual(1, dataSet.Tables.Count); Assert.AreEqual(18, dataSet.Tables["TestObjects"].Rows.Count); dataSet = new DataSet(); DataTableMappingCollection mappingCollection = new DataTableMappingCollection(); DataTableMapping testObjectsMapping = mappingCollection.Add("Table", "TestObjects"); testObjectsMapping.ColumnMappings.Add("USER_ID", "UserID"); testObjectsMapping.ColumnMappings.Add("USER_NAME", "UserName"); adoOperations.DataSetFill(dataSet, CommandType.Text, sql, mappingCollection); Assert.AreEqual(1, dataSet.Tables.Count); Assert.AreEqual(18, dataSet.Tables["TestObjects"].Rows.Count); foreach (DataRow testObjectRow in dataSet.Tables["TestObjects"].Rows) { Assert.IsNotNull(testObjectRow["UserID"]); Assert.IsNotNull(testObjectRow["UserName"]); } }
[ EditorBrowsableAttribute(EditorBrowsableState.Advanced) ] // MDAC 69508 static public DataTableMapping GetTableMappingBySchemaAction(DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction) { if (null != tableMappings) { int index = tableMappings.IndexOf(sourceTable); if (-1 != index) { #if DEBUG if (AdapterSwitches.DataSchema.TraceWarning) { Debug.WriteLine("mapping match on SourceTable \"" + sourceTable + "\""); } #endif return tableMappings.items[index]; } } if (ADP.IsEmpty(sourceTable)) { throw ADP.InvalidSourceTable("sourceTable"); } switch (mappingAction) { case MissingMappingAction.Passthrough: #if DEBUG if (AdapterSwitches.DataSchema.TraceInfo) { Debug.WriteLine("mapping passthrough of SourceTable \"" + sourceTable + "\" -> \"" + dataSetTable + "\""); } #endif return new DataTableMapping(sourceTable, dataSetTable); case MissingMappingAction.Ignore: #if DEBUG if (AdapterSwitches.DataSchema.TraceWarning) { Debug.WriteLine("mapping filter of SourceTable \"" + sourceTable + "\""); } #endif return null; case MissingMappingAction.Error: #if DEBUG if (AdapterSwitches.DataSchema.TraceError) { Debug.WriteLine("mapping error on SourceTable \"" + sourceTable + "\""); } #endif throw ADP.MissingTableMapping(sourceTable); default: throw ADP.InvalidMissingMappingAction(mappingAction); } }
public void FillDataSetNoParams() { PopulateTestObjectsTable(); string sql = ValidateTestObjects(4); DataSet dataSet; dataSet = new DataSet(); adoOperations.DataSetFill(dataSet, CommandType.Text, sql, new string[] {"TestObjects"}); Assert.AreEqual(1, dataSet.Tables.Count); Assert.AreEqual(4, dataSet.Tables["TestObjects"].Rows.Count); dataSet = new DataSet(); DataTableMappingCollection mappingCollection = new DataTableMappingCollection(); DataTableMapping testObjectsMapping = mappingCollection.Add("Table", "TestObjects"); testObjectsMapping.ColumnMappings.Add("TestObjectNo", "UserID"); testObjectsMapping.ColumnMappings.Add("Name", "UserName"); adoOperations.DataSetFill(dataSet, CommandType.Text, sql, mappingCollection); Assert.AreEqual(1, dataSet.Tables.Count); Assert.AreEqual(4, dataSet.Tables["TestObjects"].Rows.Count); foreach (DataRow testObjectRow in dataSet.Tables["TestObjects"].Rows) { Assert.IsNotNull(testObjectRow["UserID"]); Assert.IsNotNull(testObjectRow["Age"]); Assert.IsNotNull(testObjectRow["UserName"]); } }