public DataColumnMappingCollectionTest()
 {
     _cols                = new DataColumnMapping[5];
     _cols[0]             = new DataColumnMapping("sourceName", "dataSetName");
     _cols[1]             = new DataColumnMapping("sourceID", "dataSetID");
     _cols[2]             = new DataColumnMapping("sourceAddress", "dataSetAddress");
     _cols[3]             = new DataColumnMapping("sourcePhone", "dataSetPhone");
     _cols[4]             = new DataColumnMapping("sourcePIN", "dataSetPIN");
     _columnMapCollection = new DataColumnMappingCollection();
 }
        public void GetDataColumn_DataColumnMappingCollection_String_Type_DataTable_MissingMappingAction_MissingSchemaAction()
        {
            DataColumnMappingCollection dataColumnMappingCollection = new DataColumnMappingCollection();

            dataColumnMappingCollection.Add(new DataColumnMapping("sourcePIN", "dataSetPIN"));

            DataColumn dataColumn = DataColumnMappingCollection.GetDataColumn(dataColumnMappingCollection, "sourcePIN", null, new DataTable(), MissingMappingAction.Ignore, MissingSchemaAction.Ignore);

            Assert.Null(dataColumn);
        }
 public void GetReady()
 {
     cols                = new DataColumnMapping[5];
     cols[0]             = new DataColumnMapping("sourceName", "dataSetName");
     cols[1]             = new DataColumnMapping("sourceID", "dataSetID");
     cols[2]             = new DataColumnMapping("sourceAddress", "dataSetAddress");
     cols[3]             = new DataColumnMapping("sourcePhone", "dataSetPhone");
     cols[4]             = new DataColumnMapping("sourcePIN", "dataSetPIN");
     columnMapCollection = new DataColumnMappingCollection();
 }
        public void Insert_Int_Object_Success()
        {
            DataColumnMappingCollection dataColumnMappingCollection = new DataColumnMappingCollection();

            Assert.Equal(0, dataColumnMappingCollection.Count);

            dataColumnMappingCollection.Insert(0, (object)new DataColumnMapping("sourcePIN", "dataSetPIN"));

            Assert.Equal(1, dataColumnMappingCollection.Count);
            Assert.Equal("dataSetPIN", dataColumnMappingCollection["sourcePIN"].DataSetColumn);
        }
        public void Indexer_Int_SetAndGetOK()
        {
            DataColumnMappingCollection dataColumnMappingCollection = new DataColumnMappingCollection();

            dataColumnMappingCollection.Add(new DataColumnMapping("sourcePIN", "dataSetPIN"));

            dataColumnMappingCollection[0] = new DataColumnMapping("sourcePIN", "dataSetPINSet");
            DataColumnMapping dataColumnMapping = dataColumnMappingCollection[0];

            Assert.Equal("dataSetPINSet", dataColumnMapping.DataSetColumn);
        }
        public void This_String_SetAndGetOK()
        {
            DataColumnMappingCollection dataColumnMappingCollection = new DataColumnMappingCollection();

            dataColumnMappingCollection.Add(new DataColumnMapping("sourcePIN", "dataSetPIN"));

            dataColumnMappingCollection["sourcePIN"] = new DataColumnMapping("sourcePIN", "dataSetPINSet");
            DataColumnMapping dataColumnMapping = dataColumnMappingCollection["sourcePIN"];

            Assert.Equal("dataSetPINSet", dataColumnMapping.DataSetColumn);
        }
Exemple #7
0
 // <Snippet1>
 public void FindDataColumnMapping()
 {
     // ...
     // create mappings and mapping
     // ...
     if (mappings.Contains("Description"))
     {
         mapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction
                       (mappings, "Description", MissingMappingAction.Ignore);
     }
 }
        public void GetEnumerator_Success()
        {
            DataColumnMappingCollection dataColumnMappingCollection = new DataColumnMappingCollection();

            dataColumnMappingCollection.Add(new DataColumnMapping("a", "b"));

            IEnumerator enumerator = dataColumnMappingCollection.GetEnumerator();

            Assert.True(enumerator.MoveNext());
            Assert.Equal("b", ((DataColumnMapping)enumerator.Current).DataSetColumn);
            Assert.False(enumerator.MoveNext());
        }
        public void Remove_Object_Success()
        {
            DataColumnMapping           dataColumnMapping           = new DataColumnMapping("sourcePIN", "dataSetPIN");
            DataColumnMappingCollection dataColumnMappingCollection = new DataColumnMappingCollection();

            dataColumnMappingCollection.Add(dataColumnMapping);
            Assert.Equal(1, dataColumnMappingCollection.Count);

            dataColumnMappingCollection.Remove((object)dataColumnMapping);

            Assert.Equal(0, dataColumnMappingCollection.Count);
        }
        public void Remove_DataColumnMapping_Success()
        {
            DataColumnMapping           dataColumnMapping           = new DataColumnMapping("source", "dataSet");
            DataColumnMappingCollection dataColumnMappingCollection = new DataColumnMappingCollection
            {
                dataColumnMapping
            };

            Assert.Equal(1, dataColumnMappingCollection.Count);

            dataColumnMappingCollection.Remove(dataColumnMapping);

            Assert.Equal(0, dataColumnMappingCollection.Count);
        }
        public void CopyTo_Array_Int_Success()
        {
            Array array = new DataColumnMapping[1];
            DataColumnMappingCollection dataColumnMappingCollection = new DataColumnMappingCollection();

            dataColumnMappingCollection.Add(new DataColumnMapping("sourcePIN", "dataSetPIN"));
            IEnumerator enumerator = array.GetEnumerator();

            enumerator.MoveNext();
            Assert.Null(enumerator.Current);

            dataColumnMappingCollection.CopyTo(array, 0);

            enumerator = array.GetEnumerator();
            enumerator.MoveNext();
            Assert.Equal("dataSetPIN", ((DataColumnMapping)enumerator.Current).DataSetColumn);
        }
Exemple #12
0
    // <Snippet1>
    public void CreateColumnMappings()
    {
        DataColumnMappingCollection mappings =
            new DataColumnMappingCollection();

        mappings.Add("Category Name", "DataCategory");
        mappings.Add("Description", "DataDescription");
        mappings.Add("Picture", "DataPicture");
        string message = "ColumnMappings:\n";

        for (int i = 0; i < mappings.Count; i++)
        {
            message += i.ToString() + " "
                       + mappings[i].ToString() + "\n";
        }
        Console.WriteLine(message);
    }
        private void OnRowUpdating(Object sender, NpgsqlRowUpdatingEventArgs value)
        {
            switch (value.StatementType)
            {
            case StatementType.Insert:
                value.Command = GetInsertCommand(value.Row, false);
                break;

            case StatementType.Update:
                value.Command = GetUpdateCommand(value.Row, false);
                break;

            case StatementType.Delete:
                value.Command = GetDeleteCommand(value.Row, false);
                break;
            }

            DataColumnMappingCollection columnMappings = value.TableMapping.ColumnMappings;

            foreach (IDataParameter parameter in value.Command.Parameters)
            {
                string dsColumnName = parameter.SourceColumn;
                if (columnMappings.Contains(parameter.SourceColumn))
                {
                    DataColumnMapping mapping = columnMappings[parameter.SourceColumn];
                    if (mapping != null)
                    {
                        dsColumnName = mapping.DataSetColumn;
                    }
                }

                DataRowVersion rowVersion = DataRowVersion.Default;
                if (value.StatementType == StatementType.Update)
                {
                    rowVersion = parameter.SourceVersion;
                }
                if (value.StatementType == StatementType.Delete)
                {
                    rowVersion = DataRowVersion.Original;
                }
                parameter.Value = value.Row [dsColumnName, rowVersion];
            }
        }
Exemple #14
0
        static void Exmpl10()
        {
            string strSQL = "SELECT intEquipmentID, intGarageRoom, strSerialNo FROM newEquipment";

            SqlConnection  conn = new SqlConnection(connectionString);
            SqlDataAdapter da   = new SqlDataAdapter(strSQL, conn);

            //Указывает TM
            da.TableMappings.Add("Table", "newEquipment");

            DataColumnMappingCollection cm = da.TableMappings[0].ColumnMappings;

            cm.Add("EquipmentID", "intEquipmentID");
            cm.Add("GarageRoom", "intGarageRoom");
            cm.Add("SerialNo", "strSerialNo");

            DataTable tbl = new DataTable("newEquipment");

            da.Fill(tbl);

            //Можно также воспользоваться методом AddRange объекта DataTableMappingCoUection
            //или DataColumnfrlappingCollection и добавить в набор группу элементов за один вызов

            //DataColumnMappingCollection cm2 = da.TableMappings[0].ColumnMappings;
            //cm2.AddRange(new DataColumnMapping[]
            //{
            //    new DataColumnMapping("intEquipmentID", "intEquipmentID"),
            //    new DataColumnMapping("intGarageRoom", "intGarageRoom"),
            //    new DataColumnMapping("strSerialNo", "strSerialNo")
            //});

            /*
             * А что, если выполняемый объектом DataAdapter запрос содержит информацию об одной из таблиц объекта DataSet, отсутствующую в наборе TableMappings?
             * По умолчанию DataAdapter предполагает, что вам требуется получить эту информацию и записать ее в таблицу
             */
            SqlDataAdapter da2 = new SqlDataAdapter(strSQL, conn);
            DataSet        ds  = new DataSet();

            da2.Fill(ds);

            Console.WriteLine(ds.Tables["Table"].Rows[0]["intEquipmentID"].ToString());
        }
Exemple #15
0
        public void GetColumnMappingBySchemaAction()
        {
            _columnMapCollection.AddRange(_cols);
            bool eq;
            DataColumnMapping col1;

            col1 = DataColumnMappingCollection.GetColumnMappingBySchemaAction(_columnMapCollection, "sourceName", MissingMappingAction.Passthrough);
            eq   = (col1.DataSetColumn.Equals("dataSetName") && col1.SourceColumn.Equals("sourceName"));
            Assert.Equal(true, eq);
            col1 = DataColumnMappingCollection.GetColumnMappingBySchemaAction(_columnMapCollection, "sourceID", MissingMappingAction.Passthrough);
            eq   = (col1.DataSetColumn.Equals("dataSetID") && col1.SourceColumn.Equals("sourceID"));
            Assert.Equal(true, eq);

            col1 = DataColumnMappingCollection.GetColumnMappingBySchemaAction(_columnMapCollection, "sourceData", MissingMappingAction.Passthrough);
            eq   = (col1.DataSetColumn.Equals("sourceData") && col1.SourceColumn.Equals("sourceData"));
            Assert.Equal(true, eq);
            eq = _columnMapCollection.Contains(col1);
            Assert.Equal(false, eq);
            col1 = DataColumnMappingCollection.GetColumnMappingBySchemaAction(_columnMapCollection, "sourceData", MissingMappingAction.Ignore);
            Assert.Equal(null, col1);
        }
Exemple #16
0
        protected override int Fill(DataTable[] dataTables, IDataReader dataReader, int startRecord, int maxRecords)
        {
            ExTraceGlobals.IntegrationTracer.Information <int, string>((long)this.GetHashCode(), "-->MonadDataAdapter.Fill({0} (first of {1}))", dataTables.Length, dataTables[0].TableName);
            ExTraceGlobals.IntegrationTracer.Information <string>((long)this.GetHashCode(), "\tSelectCommand={0}", this.SelectCommand.CommandText);
            MonadDataReader monadDataReader = dataReader as MonadDataReader;

            if (dataTables == null || dataTables[0] == null)
            {
                throw new ArgumentNullException("dataTables");
            }
            DataTable dataTable = dataTables[0];

            if (this.enforceDataSetSchema)
            {
                DataColumnMappingCollection mappings = null;
                if (base.TableMappings.Contains(dataTable.TableName))
                {
                    mappings = base.TableMappings[dataTable.TableName].ColumnMappings;
                }
                monadDataReader.EnforceSchema(dataTable.Columns, mappings);
            }
            if (monadDataReader.PositionInfo != null)
            {
                dataTable.ExtendedProperties["Position"]   = monadDataReader.PositionInfo.PageOffset;
                dataTable.ExtendedProperties["TotalCount"] = monadDataReader.PositionInfo.TotalCount;
            }
            int num = base.Fill(dataTables, dataReader, startRecord, maxRecords);

            if (monadDataReader.PositionInfo != null)
            {
                dataTable.ExtendedProperties["BookmarkPrevious"] = monadDataReader.FirstResult;
                dataTable.ExtendedProperties["BookmarkNext"]     = monadDataReader.LastResult;
            }
            ExTraceGlobals.IntegrationTracer.Information <int>((long)this.GetHashCode(), "<--MonadDataAdapter.Fill(), {0}", num);
            return(num);
        }
 public void GetDataColumn_DataColumnMappingCollection_String_Type_DataTable_MissingMappingAction_MissingSchemaAction_MissingMappingActionErrorThrowsException()
 {
     Assert.Throws <InvalidOperationException>(() => DataColumnMappingCollection.GetDataColumn((DataColumnMappingCollection)null, "not null", typeof(string), new DataTable(), MissingMappingAction.Error, new MissingSchemaAction()));
 }
        public void AddException1()
        {
            DataColumnMappingCollection c = new DataColumnMappingCollection();

            columnMapCollection.Add((Object)c);
        }
Exemple #19
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 && 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);
        }
        public void Remove_Object_PassingNullThrowsException()
        {
            DataColumnMappingCollection dataColumnMappingCollection = new DataColumnMappingCollection();

            AssertExtensions.Throws <ArgumentNullException>("value", () => dataColumnMappingCollection.Remove((object)null));
        }
        public void GetByDataSetColumn_String_InvalidArguments()
        {
            DataColumnMappingCollection dataColumnMappingCollection = new DataColumnMappingCollection();

            Assert.Throws <IndexOutOfRangeException>(() => dataColumnMappingCollection.GetByDataSetColumn((string)null));
        }
        public void IndexOf_String_IsNull()
        {
            DataColumnMappingCollection dataColumnMappingCollection = new DataColumnMappingCollection();

            Assert.Equal(-1, dataColumnMappingCollection.IndexOf((string)null));
        }
        public void IndexOf_Object_IsNull()
        {
            DataColumnMappingCollection dataColumnMappingCollection = new DataColumnMappingCollection();

            Assert.Equal(-1, dataColumnMappingCollection.IndexOf((object)null));
        }
        public void Insert_Int_DataColumnMapping_InvalidArguments()
        {
            DataColumnMappingCollection dataColumnMappingCollection = new DataColumnMappingCollection();

            Assert.Throws <ArgumentNullException>(() => dataColumnMappingCollection.Insert(123, (DataColumnMapping)null));
        }
        public void Remove_DataColumnMapping_InvalidArguments()
        {
            DataColumnMappingCollection dataColumnMappingCollection = new DataColumnMappingCollection();

            Assert.Throws <ArgumentNullException>(() => dataColumnMappingCollection.Remove((DataColumnMapping)null));
        }
 public void GetColumnMappingBySchemaAction_DataColumnMappingCollection_String_MissingMappingAction_MissingMappingActionNotFoundThrowsException()
 {
     AssertExtensions.Throws <ArgumentOutOfRangeException>("MissingMappingAction", () => DataColumnMappingCollection.GetColumnMappingBySchemaAction((DataColumnMappingCollection)null, "not null", new MissingMappingAction()));
 }
 public void GetColumnMappingBySchemaAction_DataColumnMappingCollection_String_MissingMappingAction_InvalidArguments()
 {
     AssertExtensions.Throws <ArgumentException>("sourceColumn", () => DataColumnMappingCollection.GetColumnMappingBySchemaAction((DataColumnMappingCollection)null, null, new MissingMappingAction()));
 }
 public void GetDataColumn_DataColumnMappingCollection_String_Type_DataTable_MissingMappingAction_MissingSchemaAction_MissingMappingActionNotFoundThrowsException()
 {
     AssertExtensions.Throws <ArgumentOutOfRangeException>("MissingMappingAction", () => DataColumnMappingCollection.GetDataColumn((DataColumnMappingCollection)null, "not null", typeof(string), new DataTable(), new MissingMappingAction(), new MissingSchemaAction()));
 }
 public void GetDataColumn_DataColumnMappingCollection_String_Type_DataTable_MissingMappingAction_MissingSchemaAction_InvalidArguments()
 {
     AssertExtensions.Throws <ArgumentException>("sourceColumn", () => DataColumnMappingCollection.GetDataColumn((DataColumnMappingCollection)null, null, typeof(string), new DataTable(), new MissingMappingAction(), new MissingSchemaAction()));
 }
 public void GetDataColumn_DataColumnMappingCollection_String_Type_DataTable_MissingMappingAction_MissingSchemaAction_MissingMappingActionIgnoreReturnsNull()
 {
     Assert.Null(DataColumnMappingCollection.GetDataColumn((DataColumnMappingCollection)null, "not null", typeof(string), new DataTable(), MissingMappingAction.Ignore, new MissingSchemaAction()));
 }