none
Reading DBF files RRS feed

  • Question

  •  

    Hi all!

    I'm reading a dbf file using a OleDBConnection and show it in a DataGrid on my GUI. This works fine.

    The problem are special german charactes (Umlaut) which are not displayed correctly (strange signs are shown). The dbf file has set ANSI to true

     

    Here is my short code:

    Code Block

    System.Data.OleDb.OleDbConnection oConn = new System.Data.OleDb.OleDbConnection();

    oConn.ConnectionString =

    "Provider=Microsoft.Jet.OLEDB.4.0" +

    ";Data Source=" + Path.GetDirectoryName(this.tbxFile.Text) +

    ";Extended Properties=dBASE IV";

     

    oConn.Open();

     

    System.Data.OleDb.OleDbCommand oCmd = oConn.CreateCommand();

    oCmd.CommandText = "SELECT * FROM [" + Path.GetFileName(this.tbxFile.Text) + "]";

     

    DataTable dataTable = new DataTable();

    dataTable.Load(oCmd.ExecuteReader());

     

    this.dataGridView1.DataSource = dataTable;

     

     

    Are there any options to set in the connection string

     

    Thanks for any help!

     

    Jörg

    Monday, December 3, 2007 10:58 AM

Answers

  • I found a solution. The dbf I work with useses the codepage 850. I go throu each row and convert the string values in bytes using an encoder with the codepage 850. Then I encode the bytes back to string using the default encoder.

     

    Code Block

    private void DecodeTable(DataTable pDt)

    {

    string colValue;

    DataColumnCollection cols = pDt.Columns;

    foreach (DataRow row in pDt.Rows)

    {

    foreach (DataColumn col in cols

    {

    if (col.DataType == typeof(string) && row[col.ColumnName] != DBNull.Value)

    {

    colValue = row[col.ColumnName].ToString();

    byte[] bytes = Encoding.GetEncoding(850).GetBytes(colValue);

    colValue = System.Text.Encoding.Default.GetString(bytes);

    row[col.ColumnName] = colValue;

    }

    }

    }

    pDt.AcceptChanges();

    }

     

     

    It's not the fastest way, but it works
    Monday, December 10, 2007 3:11 PM

All replies

  • I found a solution. The dbf I work with useses the codepage 850. I go throu each row and convert the string values in bytes using an encoder with the codepage 850. Then I encode the bytes back to string using the default encoder.

     

    Code Block

    private void DecodeTable(DataTable pDt)

    {

    string colValue;

    DataColumnCollection cols = pDt.Columns;

    foreach (DataRow row in pDt.Rows)

    {

    foreach (DataColumn col in cols

    {

    if (col.DataType == typeof(string) && row[col.ColumnName] != DBNull.Value)

    {

    colValue = row[col.ColumnName].ToString();

    byte[] bytes = Encoding.GetEncoding(850).GetBytes(colValue);

    colValue = System.Text.Encoding.Default.GetString(bytes);

    row[col.ColumnName] = colValue;

    }

    }

    }

    pDt.AcceptChanges();

    }

     

     

    It's not the fastest way, but it works
    Monday, December 10, 2007 3:11 PM
  • Why not handle this at the cell rendering level in the grid? That way you dont have to worry about it again (ie in another table), as its really only a display issue.

     

    Just a thought.

    Neil

    Monday, December 10, 2007 3:17 PM
  • Thanks for your suggestion Neil.

    This was just a sample grid to see what I was I reading. In my application I import data from dbf to sql

    Monday, December 10, 2007 3:30 PM