public override void Load_DBFToDb() { try { using (NDbfReader.Table dbfTable = NDbfReader.Table.Open(File.Open(FileMode.Open))) using (DBFDataReader dbfReader = new DBFDataReader(dbfTable, Encoding.GetEncoding(866))) { while (dbfReader.Read()) { foreach (var c in dbfTable.Columns) { switch (Command.Parameters["@" + c.Name].DbType) { case System.Data.DbType.String: if (c.Type == typeof(byte[])) { Command.Parameters["@" + c.Name].Value = dbfReader.GetValue(c).ToString(); } else { Command.Parameters["@" + c.Name].Value = dbfReader.GetString(c); } break; default: Command.Parameters["@" + c.Name].Value = dbfReader.GetValue(c); break; } } Command.ExecuteNonQuery(); } } File.Delete(); } catch (Exception e) { Logger.Logger.Error(e.Message); } }
public override void Load_DBFToDb() { using (SqlTransaction TRA = Connection.BeginTransaction(("Bulk" + TableName))) { SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(Connection, SqlBulkCopyOptions.Default, TRA) { BulkCopyTimeout = 60000, DestinationTableName = DestinationTableName }; try { var errors = new List <object>(); using (NDbfReader.Table dbfTable = NDbfReader.Table.Open(File.Open(FileMode.Open))) { DBFDataReader dbfReader = new DBFDataReader(dbfTable, Encoding.GetEncoding(866)); foreach (var c in dbfTable.Columns) { sqlBulkCopy.ColumnMappings.Add(c.Name, c.Name); } sqlBulkCopy.WriteToServer(dbfReader); TRA.Commit(); } if (errors.Count == 0) { File.Delete(); } } catch (Exception e) { Logger.Logger.Error(e.Message); } finally { sqlBulkCopy.Close(); } } }