Get data in a .dbf file using C #

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

What I want to do is read the data in each row (same column) to continue processing it.

Thanks.

+11
c # dbf
source share
2 answers

You can create a connection string to the dbf file , then using OleDb you can fill in the data set, for example:

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 ds.Tables[0] for further processing.

You can also check this article Load DBF in DataTable

+18
source share

I found that the accepted answer does not work for me, because the .dbf files I work with are nested in a directory hierarchy that makes the paths quite long, which unfortunately calls the OleDbCommand object.

I found a neat little library that needs only the file path to work. Here is a small example adapted from the examples on his 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"]); } } 
0
source share

All Articles