Ejemplo n.º 1
0
        public override bool BulkInsert(DbTable schema, MungedDataReader reader)
        {
            using (var cn = _getConnection()) {
                using (SqlBulkCopy copy = new SqlBulkCopy(cn)) {
                    //copy.ColumnMappings = new SqlBulkCopyColumnMappingCollection();

                    for (var i = 0; i < reader.ColumnNames.Length; i++)
                    {
                        var column             = reader.ColumnNames[i];
                        var sourceOrdinal      = i;
                        var destinationOrdinal = schema.Columns.FindIndex(x => x.Name == column);

                        if (destinationOrdinal == -1)
                        {
                            var msg = string.Format("Unable to resolve column mapping, column: {0} was not found in destination table {1}",
                                                    column,
                                                    _table
                                                    );
                            throw new Exception(msg);
                        }
                        copy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(i, destinationOrdinal));
                    }

                    copy.DestinationTableName = string.Format("[{0}].[{1}]", _schema, _table);

                    copy.BatchSize       = 1000;
                    copy.BulkCopyTimeout = 9999999;

                    copy.WriteToServer(reader);
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        public bool Execute()
        {
            using (var stream = File.OpenText(_filename)) {
                using (var reader = new MungedDataReader(stream)) {
                    var currentSchema = new DbTable(_table, reader);
                    var oldSchema     = GetDbSchema();

                    // Check / Update the schema in the DB
                    if (oldSchema == null)
                    {
                        CreateTable(currentSchema);
                    }
                    else
                    {
                        ModifySchema(oldSchema, currentSchema);
                    }

                    var newSchema = GetDbSchema();

                    BulkInsert(newSchema, reader);
                }
            }
            return(true);
        }
Ejemplo n.º 3
0
 public abstract bool BulkInsert(DbTable schema, MungedDataReader reader);