DbfDataReader — это небольшая быстрая библиотека .Net Core для чтения файлов баз данных dBase, xBase, Clipper и FoxPro
Пример, как получить общую информацию о файле:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
var dbfPath = "path/file.dbf"; using (var dbfTable = new DbfTable(dbfPath)) { var header = dbfTable.Header; var versionDescription = header.VersionDescription; var hasMemo = dbfTable.Memo != null; var recordCount = header.RecordCount; foreach (var dbfColumn in dbfTable.Columns) { var name = dbfColumn.Name; var columnType = dbfColumn.ColumnType; var length = dbfColumn.Length; var decimalCount = dbfColumn.DecimalCount; } } |
и перебрать все строки:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
var skipDeleted = true; var dbfPath = "path/file.dbf"; using (var dbfTable = new DbfTable(dbfPath)) { var dbfRecord = new DbfRecord(dbfTable); while (dbfTable.Read(dbfRecord)) { if (skipDeleted && dbfRecord.IsDeleted) { continue; } foreach (var dbfValue in dbfRecord.Values) { var stringValue = dbfValue.ToString(); var obj = dbfValue.GetValue(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
var options = new DbfDataReaderOptions { SkipDeletedRecords = true // Encoding = EncodingProvider.GetEncoding(1252); }; var dbfPath = "path/file.dbf"; using (var dbfDataReader = new DbfDataReader(dbfPath, options)) { while (dbfDataReader.Read()) { var valueCol1 = dbfDataReader.GetString(0); var valueCol2 = dbfDataReader.GetDecimal(1); var valueCol3 = dbfDataReader.GetDateTime(2); var valueCol4 = dbfDataReader.GetInt32(3); } } |
это также означает, что вы можете массово скопировать в MS SqlServer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
var options = new DbfDataReaderOptions { SkipDeletedRecords = true // Encoding = EncodingProvider.GetEncoding(1252); }; var dbfPath = "path/file.dbf"; using (var dbfDataReader = new DbfDataReader(dbfPath, options)) { using (var bulkCopy = new SqlBulkCopy(connection)) { bulkCopy.DestinationTableName = "DestinationTableName"; try { bulkCopy.WriteToServer(dbfDataReader); } catch (Exception ex) { Console.WriteLine($"Error importing: dbf file: '{dbfPath}', exception: {ex.Message}"); } } } |
Использован
- DbfDataReader
https://github.com/yellowfeather/DbfDataReader
- DbfBulkCopy
Приложение командной строки для массового копирования из файлов DBF в MS SqlServer
https://github.com/yellowfeather/DbfBulkCopy
- dbf
Утилита командной строки для отображения информации и содержимого DBF
https://github.com/yellowfeather/dbf