none
read .dbf file RRS feed

  • Question

  • I have a *.dbf file (personen.dbf).

    Im trying to read something from it (select name from personen where id=15)

    Can somebody give me a simple example how to read this?
    Monday, March 8, 2010 5:27 PM

Answers

All replies

  • You can do this via ODBC.  Here's a post on reading DBF files with C#.  It shows complete code for reading from a DBF file using an OdbcConnection.

    Reed Copsey, Jr. - http://reedcopsey.com
    Monday, March 8, 2010 5:44 PM
    Moderator
  • OK. I solve the problem:

    string

     

    fileName = "personen.dbf";

     

    string filePath = "d:\\mydbpath";

     

    string query = "SELECT * FROM " + fileName + ".dbf where lfnummer<'355'";

    richTextBox1.Text =

    "Branje...\n";

     

    OdbcConnection conn =

     

    new OdbcConnection("Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=" + filePath + ";");

     

    OdbcCommand cmd = new OdbcCommand(query, conn);

     

    Cursor.Current = Cursors.WaitCursor;

    conn.Open();

     

    DataTable dt = new DataTable();

    dt.Load(cmd.ExecuteReader());

     

    conn.Close();

    dataGridView1.DataSource = dt;

     

    Cursor.Current = Cursors.Default;



    Now I have another problem.
    This dbf file contains 150000 records, and it takes a long time to read everything.
    Is there a faster way to read ?

    Monday, March 8, 2010 6:27 PM
  • Unfortunately, DBF files are not (usually) very good in terms of performance.

    Is using a DBF file a fixed requirement for your application?  If not, changing to another data storage option could potentially make life a lot faster.

    If it is, make sure you have your DBF file indexed properly.  If you index the lfnummer column of your dbf file, you should get better performance.

    Otherwise, changing to a more mature database (such as SQL Server CE) would make this type of operation very fast.


    Reed Copsey, Jr. - http://reedcopsey.com
    Monday, March 8, 2010 6:55 PM
    Moderator
  • Yes I am writting a new program and I'm using SQL database, but I have to import archives
    from earlier versions of program - those versions were using dbf.

    Ok, it takes about a minute to read 150000 records (that ok), but in the middle of reading
    program stop responding and then after a minute everything is ok.

    How do I solve this problem so that my program wont stop responding while reading?
    Monday, March 8, 2010 8:24 PM
  • The program stops responding because you're doing this work on the UI thread.

    If you want to prevent that, you'll need to parse your DBF file on a background thread.  That will leave your UI responsive.



    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by wartmignef Tuesday, March 9, 2010 7:56 AM
    Monday, March 8, 2010 8:34 PM
    Moderator
  • Ok.

    How do I do this?
    Tuesday, March 9, 2010 7:11 AM
  • The easiest way would be to use a BackgroundWorker.  Just add one in your form, and do the "work" you're trying to do in the DoWork event handler.

    This will cause it to run on a background thread.

    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by wartmignef Wednesday, March 10, 2010 2:59 PM
    Tuesday, March 9, 2010 6:29 PM
    Moderator