예제 #1
0
        /// <summary>
        /// Check that every column specified in the mappings collection exists
        /// in the wrapped reader.
        /// </summary>
        void ValidateBulkCopySourceColumnMappings(SqlBulkCopyColumnMappingCollection mappings)
        {
            foreach (SqlBulkCopyColumnMapping mapping in mappings)
            {
                var sc = new SqlName(mapping.SourceColumn);

                if (!String.IsNullOrEmpty(sc.Name))
                {
                    if (wrappedReader.GetOrdinal(sc.Name) == -1)
                    {
                        string bestFit = wrappedReader.GetColumns().SingleOrDefault(c => c.Equals(sc.Name, StringComparison.OrdinalIgnoreCase));

                        if (bestFit == null)
                        {
                            throw new SchemaException("Source column " + mapping.SourceColumn + " does not exist in source.");
                        }
                        else
                        {
                            throw new SchemaException
                                  (
                                      "Source column " + mapping.SourceColumn + " does not exist in source." +
                                      " Column name mappings are case specific and best found match is " + bestFit + "."
                                  );
                        }
                    }
                }
                else
                {
                    if (mapping.SourceOrdinal < 0 || mapping.SourceOrdinal >= wrappedReader.FieldCount)
                    {
                        throw new SchemaException("No column exists at index " + mapping.SourceOrdinal + " in source.");
                    }
                }
            }
        }
예제 #2
0
        void ValidateBulkCopyDestinationColumnMappings
        (
            SqlBulkCopyColumnMappingCollection mappings,
            Dictionary <string, int> columnLookup,
            DataRow[] columns
        )
        {
            foreach (SqlBulkCopyColumnMapping mapping in mappings)
            {
                var destColumn = new SqlName(mapping.DestinationColumn);

                if (!String.IsNullOrEmpty(destColumn.Name))
                {
                    if (!columnLookup.ContainsKey(destColumn.Name))
                    {
                        // If we can't find an exact match by case, try for a case-insensitive match.
                        string bestFit = columnLookup.Keys.SingleOrDefault(c => c.Equals(destColumn.Name, StringComparison.OrdinalIgnoreCase));

                        if (bestFit == null)
                        {
                            throw new SchemaException
                                  (
                                      "Destination column " + mapping.DestinationColumn +
                                      " does not exist in destination table " + targetTableName +
                                      " in database " + targetDatabaseName +
                                      " on server " + targetServerName + "."
                                  );
                        }
                        else
                        {
                            throw new SchemaException
                                  (
                                      "Destination column " + mapping.DestinationColumn +
                                      " does not exist in destination table " + targetTableName +
                                      " in database " + targetDatabaseName +
                                      " on server " + targetServerName +
                                      ". Column name mappings are case specific and best found match is " + bestFit + "."
                                  );
                        }
                    }
                }
                else
                {
                    if (mapping.DestinationOrdinal < 0 || mapping.DestinationOrdinal >= columns.Length)
                    {
                        throw new SchemaException
                              (
                                  "No column exists at index " + mapping.DestinationOrdinal +
                                  " in destination table " + targetTableName +
                                  " in database " + targetDatabaseName +
                                  " on server " + targetServerName + "."
                              );
                    }
                }
            }
        }
예제 #3
0
        void CreateLookupFromColumnMappings
        (
            SqlBulkCopyColumnMappingCollection mappings,
            DataTable schemaTableOfDestinationTable
        )
        {
            // create lookup dest column definition by source index
            foreach (SqlBulkCopyColumnMapping mapping in mappings)
            {
                int sourceIndex  = -1;
                var sourceColumn = new SqlName(mapping.SourceColumn);
                if (!String.IsNullOrEmpty(sourceColumn.Name))
                {
                    sourceIndex = wrappedReader.GetOrdinal(sourceColumn.Name);
                }
                else
                {
                    sourceIndex = mapping.SourceOrdinal;
                }

                DataRow destColumnDef = null;
                var     destColumn    = new SqlName(mapping.DestinationColumn);
                if (!String.IsNullOrEmpty(destColumn.Name))
                {
                    destColumnDef = schemaTableOfDestinationTable.Rows.Cast <DataRow>().
                                    Single(c => (string)c["ColumnName"] == destColumn.Name);

                    //foreach (DataRow column in schemaTableOfDestinationTable.Rows)
                    //{
                    //    if ((string)column["ColumnName"] == destColumn.Name)
                    //    {
                    //        destColumnDef = column;
                    //        break;
                    //    }
                    //}
                }
                else
                {
                    destColumnDef = schemaTableOfDestinationTable.Rows.Cast <DataRow>().
                                    Single(c => (int)c["ColumnOrdinal"] == mapping.DestinationOrdinal);

                    //foreach (DataRow column in schemaTableOfDestinationTable.Rows)
                    //{
                    //    if ((int)column["ColumnOrdinal"] == mapping.DestinationOrdinal)
                    //    {
                    //        destColumnDef = column;
                    //        break;
                    //    }
                    //}
                }

                lookup[sourceIndex] = destColumnDef;
            }
        }