Esempio n. 1
0
        /// <summary>
        /// 设置DataTable到数据库中表的映射
        /// </summary>
        /// <param name="mappings">The mappings.</param>
        /// <param name="table">The table.</param>
        private void SetMappings(SqlBulkCopyColumnMappingCollection mappings, RdbTable table)
        {
            foreach (var column in table.Columns)
            {
                mappings.Add(column.Name, column.Name);
            }

            //暂留:通过查询的真实列名来实现 Mapping。
            //var sql = string.Format("SELECT TOP 0 * FROM [{0}]", _table.Name);
            //var dbNames = new List<string>();
            //using (var reader = _meta.DBA.QueryDataReader(sql))
            //{
            //    for (int i = 0, c = reader.FieldCount; i < c; i++)
            //    {
            //        var name = reader.GetName(i);
            //        dbNames.Add(name);
            //    }
            //}
            //foreach (var column in _table.Columns)
            //{
            //    var correspondingDbName = dbNames.First(c => string.Compare(c, column.Name, true) == 0);
            //    mappings.Add(column.Name, correspondingDbName);
            //}
        }
Esempio n. 2
0
 /// <summary>
 /// 设置DataTable到数据库中表的映射
 /// </summary>
 /// <param name="mappings"></param>
 private void SetMappings(SqlBulkCopyColumnMappingCollection mappings)
 {
     var cmd = this._db.CreateCommand();
     cmd.CommandText = string.Format("select top 1 * from [{0}]", this._table.Name);
     var dbNames = new List<string>();
     using (var reader = cmd.ExecuteReader())
     {
         for (int i = 0, c = reader.FieldCount; i < c; i++)
         {
             var name = reader.GetName(i);
             dbNames.Add(name);
         }
     }
     foreach (var column in this._table.Columns)
     {
         var correspondingDbName = dbNames.First(c => string.Compare(c, column.Name, true) == 0);
         mappings.Add(column.Name, correspondingDbName);
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Maps columns by name.  Required by SqlBulkCopy if schema changes or columns are out of order.
        /// </summary>
        private void MapColumns(SqlBulkCopyColumnMappingCollection columnMappings, IEnumerable<string> tableColumnNames,
            DataColumnCollection importFileColumns)
        {
            var importColumnNames = importFileColumns.Cast<DataColumn>().Select(c => c.ColumnName);
            var columnsInCommon = tableColumnNames.Intersect(importColumnNames);
            foreach (var column in columnsInCommon)
            {
                columnMappings.Add(column, column);
            }

        }