Skip to content
This repository has been archived by the owner on Dec 9, 2020. It is now read-only.

DbfDataReader is a small fast .Net Core library for reading dBase, xBase, Clipper and FoxPro database files

License

Notifications You must be signed in to change notification settings

burdickryan/DbfDataReader

 
 

Repository files navigation

DbfDataReader

Build status NuGet MyGet Build Status

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

About

DbfDataReader is a small fast .Net Core library for reading dBase, xBase, Clipper and FoxPro database files

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 96.2%
  • PowerShell 3.8%