DbfDataReader is a small fast .Net Core library for reading dBase, xBase, Clipper and FoxPro database files
Usage, to get summary info:
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;
}
}
and to iterate over the rows:
var dbfPath = "path/file.dbf";
using (var dbfTable = new DbfTable(dbfPath))
{
var dbfRecord = new DbfRecord(dbfTable);
while (dbfTable.Read(dbfRecord))
{
if (options.SkipDeleted && dbfRecord.IsDeleted)
{
continue;
}
foreach (var dbfValue in dbfRecord.Values)
{
var stringValue = dbfValue.ToString();
var obj = dbfValue.GetValue();
}
}
}
There is also an implementation of DbDataReader:
var dbfPath = "path/file.dbf";
using (var dbfDataReader = new DbfDataReader.DbfDataReader(dbfPath))
{
while (dbfDataReader.Read())
{
var valueCol1 = dbfDataReader.GetString(0);
var valueCol2 = dbfDataReader.GetDecimal(1);
var valueCol3 = dbfDataReader.GetDateTime(2);
var valueCol4 = dbfDataReader.GetInt32(3);
}
}
which also means you can bulk copy to MS SqlServer:
var dbfPath = "path/file.dbf";
using (var dbfDataReader = new DbfDataReader.DbfDataReader(dbfPath))
{
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}");
}
}
}
Used by
-
DbfBulkCopy
Command line application to bulk copy from DBF files to MS SqlServer
-
dbf
Command line utility to display DBF info and contents