How to Read and Write a .dbf file in C# ?


How to Read and Write a .dbf file or how to make connection iwth .dbf file in C# ?

There are two way to make connection with .dbf file :

1. With ODBC Connection.


string filepath = "c:\\nn\\";

OdbcConnection CC = new OdbcConnection("Driver={Microsoft dBase Driver (*.dbf)};SourceType=DBF;SourceDB=" + filepath + ";Exclusive=No; Collate=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;");
CC.Open();

OdbcCommand cmd = new OdbcCommand("Select * From " + filepath + "smsout.dbf" , CC);
OdbcDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
DataTable dt = new DataTable();
dt.Load(dr);
}
CC.Close();


2. With OLEDB Connection.

OleDbConnection ccc = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath.Substring(0, filepath.LastIndexOf("\\")) + ";Extended Properties=dBASE IV;");
ccc.Open();
OleDbCommand cmd1 = new OleDbCommand("Select * From " + filepath + "smsout.dbf", ccc);
OleDbDataReader dr1 = cmd1.ExecuteReader();
if (dr1.HasRows)
{
DataTable dt1 = new DataTable();
dt1.Load(dr1);
}
ccc.Close();


Comments

Guest Author: CLS01 Feb 2012

Use System.IO.Path.GetDirectoryName(filepath)
instead of
filepath.Substring(0, filepath.LastIndexOf("\\"))

Guest Author: Nirmala10 Mar 2013

I used the above coding but I am getting error 'External table is not in expected format'

Guest Author: Indrajeyan25 Feb 2014

The above code works fine..
But while the character of the file name exceeds more than 8 it throws exception.i searched related to it and got a result for this.(i.e)In Microsoft Access, when you import a dBase, FoxPro, or Paradox file that does not follow the MS-DOS 8.3 file name format.
Is there any alternative to read the file that has more than 8 chatacter...?

Thanks,
Indra



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: