Get data in a .dbf file using c#
Asked Answered
P

3

17

How can I get the data in a .dbf file using c#??

What I want to do is to read the data in each row (same column) to further process them.

Thanks.

Poss answered 6/7, 2012 at 6:22 Comment(2)
Tbh, I am no idea. Now, I can just count the number of row in the .dbf filePoss
Have a look at an ODBC connectionJoell
B
23

You may create a connection string to dbf file, then using OleDb, you can populate a dataset, something like:

string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=directoryPath;Extended Properties=dBASE IV;User ID=Admin;Password=;";
using (OleDbConnection con = new OleDbConnection(constr))
{
    var sql = "select * from " + fileName;
    OleDbCommand cmd = new OleDbCommand(sql, con);
    con.Open();
    DataSet ds = new DataSet(); ;
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    da.Fill(ds);
}

Later you can use the ds.Tables[0] for further processing.

You may also check this article Load a DBF into a DataTable

Belongings answered 6/7, 2012 at 6:29 Comment(4)
Thanks a lot. I can do what I wanted now!Poss
You should use directoryPath instead of yourfilepath to avoid misleading... or use specific example, such as: c:\folderCommotion
is it possible to get specific row data? like i will pass row index and i should get that specific row dataHaggerty
It is good solution but I found out that oledb of version 4 cannot read files with name longer than 8 characters, it says that object could not be found...Hyaloid
V
6

I found out the accepted answer didn't work for me, as the .dbf files I'm working with are nested in a hierarchy of directories that makes the paths rather long, which, sadly, cause the OleDbCommand object to throw.

I found a neat little library that only needs a file path to work. Here's a little sample adapted from the examples on its GitHub page:

var file = "C:\\Path\\To\\File.dbf";
using (var dbfDataReader = new DbfDataReader(file))
{
    while (dbfDataReader.Read())
    {
        var foo = Convert.ToString(dbfDataReader["FOO"]);
        var bar = Convert.ToInt32(dbfDataReader["BAR"]);
    }
}
Vaivode answered 24/7, 2019 at 21:23 Comment(2)
Hi, I had no luck with this API. Did u run it from 32 or 64 bit OS?Prefer
This library did work for me out of the box. github.com/henck/dBASE.NETPrefer
G
3

For 64 bit systems I used the Microsoft ACE OLEDB 12.0 data provider, for that provider to work you have to install Microsoft's Access Database Engine 2010

So it looks a lot like the accepted answer but with the provider changed:

string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\folder;Extended Properties=dBASE IV;User ID=Admin;";

using (OleDbConnection con = new OleDbConnection(constr))
{
    var sql = "select * from " + fileName;
    OleDbCommand cmd = new OleDbCommand(sql, con);
    con.Open();
    DataSet ds = new DataSet();
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    da.Fill(ds);
}
Gow answered 7/2, 2023 at 21:20 Comment(1)
download link is not available,new link: microsoft.com/en-us/download/details.aspx?id=54920Trevatrevah

© 2022 - 2024 — McMap. All rights reserved.