none
how to read .dbf file in Visual C#.Net?

    Question

  • I got sample of reading excel. can work. but cannot find the sample of reading .dbf (Dbase IV) file. how to read .dbf?
    Monday, August 22, 2005 9:32 AM

Answers

  • This works fine for me (for SELECT/UPDATE).  Tho a bit slow.  You can use OleDb or Odbc.  The connection string is different for each method - (from www.connectionstrings.com) (both worked ok for me):
    Odbc: 
    "Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=c:\mydbpath;"
    OleDb:
    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\mydbpath;Extended Properties=dBASE IV;User ID=Admin;Password="

    Change c:\mydbpath to the folder where the DBFs are stored.
    In SQL the DBF files can be with or without the .DBF extension (try both as I cannot remember which worked)

    The only problem I had was with creating a new DBF. I wanted to export my Datatable to a new DBF... It seemed that INSERT INTO doesn't create a new DBF... it has to exist.  So I came up with a long winded way and in the end the bit that wrote to the DBF took 2 minutes to write 4000 records... 

    So because I was working with Access I instead automated Access via the Microsoft.Office.Interop.Access PIA and ran a module subroutine that used Access' built-in method:
    DoCmd.TransferDatabase acExport, "DBase IV", dbfPath, acTable, queryName, dbfName & ".dbf"

    That took 15 seconds to export 4000 records...

    Wednesday, August 31, 2005 7:09 PM

All replies

  • anyone can help?

    thanks
    Tuesday, August 23, 2005 7:23 AM
  • Though I have not tried it, you can use OleDb Provider of ADO.NET to connect to DBase databases. The connection string can be as follows

    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\db\db.dbf;Extended Properties=dBASE IV;User ID=Admin;Password="

    Once you have an OleDB connection established you can use the normal ADO.NET methods to read/write to the database.

    Regards,
    Saurabh Nandu
    www.MasterCSharp.com
    www.AksTech.com

    Tuesday, August 23, 2005 9:39 AM
  • This works fine for me (for SELECT/UPDATE).  Tho a bit slow.  You can use OleDb or Odbc.  The connection string is different for each method - (from www.connectionstrings.com) (both worked ok for me):
    Odbc: 
    "Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=c:\mydbpath;"
    OleDb:
    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\mydbpath;Extended Properties=dBASE IV;User ID=Admin;Password="

    Change c:\mydbpath to the folder where the DBFs are stored.
    In SQL the DBF files can be with or without the .DBF extension (try both as I cannot remember which worked)

    The only problem I had was with creating a new DBF. I wanted to export my Datatable to a new DBF... It seemed that INSERT INTO doesn't create a new DBF... it has to exist.  So I came up with a long winded way and in the end the bit that wrote to the DBF took 2 minutes to write 4000 records... 

    So because I was working with Access I instead automated Access via the Microsoft.Office.Interop.Access PIA and ran a module subroutine that used Access' built-in method:
    DoCmd.TransferDatabase acExport, "DBase IV", dbfPath, acTable, queryName, dbfName & ".dbf"

    That took 15 seconds to export 4000 records...

    Wednesday, August 31, 2005 7:09 PM
  • Since this last email I have also discovered a faster way to create DBFs with less coding required.  And it does not require Access to be installed whereas using the Access Interop does.

    This only works if you have access to another datasource like Access, SQL etc.

    Set up the connection object to your datasource:

    Dim con As New OleDb.OleDbConnection("<connectionstring>")

    The trick is in the SQL Command.

    Dim cmd As New OleDb.OleDbCommand("SELECT * INTO [dBase IV;DATABASE=c:\filepath;].[file.dbf] FROM table1", con)

    This is only a modification of the following SQL SELECT statement - the INTO gives the SQL statement a destination :

    SELECT * FROM table1   (from Access or other datasource)

    modified with -   INTO [dBase IV;DATABASE=c:\filepath;].[file.dbf]

    This method is the fastest way i've found to create a new DBF file from another database format.

    So if you want to save a Datatable to DBF.  It might sound crazy - but you could upload the DataTable to a temporary Access database or other datasource.  And then pipe the data from that datasource into a dbf using the above method.  And this would probably be much faster than simply constructing OleDb connection and command objects directly to the DBF, which in my experience can take over 2 minutes for 4000+ records...

    You can also use this to pipe data into any other datasource... I used this to move a table from one Access database to another as I was having a problem that meant I had to delete the original table and then insert a new one...

    SELECT * INTO [MS Access;DATABASE=C:\mdbpath\test.mdb;].[tablename] FROM table1
    (could be written:
    SELECT * INTO tablename IN 'C:\mdbpath\test.mdb' FROM table1)

    This method may not be supported by all databases.  I've only tested this with an Access database.

    Monday, March 27, 2006 9:58 AM
  • Hi!

     

    I also have a .DBF file that I want to read and query.

     

    I tryed the Odbc connection string. The program compiled ok, but when running it returned the error: "keyword not suported: 'driver' ".

     

    I'm new at C# so I don't know if there's anything that I must do first in order to be able to read the file.

     

    Thanks in advance.

     

     

    RB

    Wednesday, July 18, 2007 10:30 AM
  • Hi Oliver,

     

    Do you know if your DBFs are created with FoxPro? If you search for my posts you'll find VB examples of working with FoxPro DBFs that you can easily adapt to C#. You can use the CREATE TABLE statement to create DBFs.

    Wednesday, July 18, 2007 2:26 PM
  • I can't find examples to read data from .DBF files can you please specify a path where your examples are

    Wednesday, October 31, 2007 4:07 PM
  • Cindy,

     

      I'm sort of new to the dbf world. I used them years ago with foxpro for windows but haven't kept up. I'm wondering about the format of dbf files and how that affects my ability to use them from inside .net. The one's I'm working with are of several different dbf levels, all used in an old delphi 5 application.

     

       I've tried using oledb but the results have been inconsistent. Sometimes I can read and write and sometimes I can't. There's doesn't seem to be much of a pattern to it. The earlier posters in this thread seem to imply there is a way to do this consistently but I'm not sure if that would work for dbfs created in foxpro or if there is no difference between the dbfs I'm using and the foxpro dbfs.

     

      I'd like to be able to access all of these dbfs for read/write but I'm not sure what the issues are. Any advice would be appreciated.

     

    Thanks,

    fig000

     

    Friday, May 30, 2008 2:14 PM
  • Maybe this can help. It works fine with dbf files.

    https://www.microsoft.com/en-us/download/details.aspx?id=14839

    Wednesday, April 25, 2018 7:21 PM