Palani Kumar
Palani Kumar

Reputation: 350

C# - dbf External table is not in the expected format

I am struggling here. I am trying to fetch data from dbf file. for that using the below connection string and code.

DataTable YourResultSet = new DataTable();
        const string path = "D:\\Sample\\Database\\client.dbf";
        string conStr = String.Format("Provider = Microsoft.Jet.Oledb.4.0; Data Source = {0}; Extended Properties = \"dBase IV\"", Path.GetDirectoryName(path));
        var connection = new OleDbConnection(conStr);
        connection.Open();

        var command = new OleDbCommand(string.Format("select id from {0}", Path.GetFileName(path)), connection);

        using (var reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                var str = (string)reader["id"];
            }
        }

        connection.Close();

Few dbf file can be read and most of the dbf file cannot fetch. While execute the code ann error has been occured that "External table is not in the expected format."

Even though i am using different of connextion string like vfopledb, vfoledb1, jet, ace etc..

my machine is 64bit and i am using vs2013. Please help me.

don't put negative because i have tried all stack over flow answer regarding this same issue.

Upvotes: 2

Views: 2862

Answers (1)

Cetin Basoz
Cetin Basoz

Reputation: 23797

Use VFPOLEDB driver. Jet or ACE driver wouldn't recognize all dbf tables. VFPOLEDB driver is 32 bits, that means you should be targeting x86 in your C# project (properties/build target platform).

void Main()
{
    DataTable YourResultSet = new DataTable();
    string path = @"D:\Sample\Database\client.dbf";

    using (OleDbConnection connection =
    new OleDbConnection("Provider=VFPOLEDB;Data Source=" + Path.GetDirectoryName(path)))
    using (OleDbCommand command = new OleDbCommand(
    string.Format("select id from {0}", Path.GetFileNameWithoutExtension(path)), connection))
    {
    connection.Open();
    YourResultSet.Load(command.ExecuteReader());
    connection.Close();
    }

    Form f = new Form();
    DataGridView dgv = new DataGridView { Dock = DockStyle.Fill, DataSource=YourResultSet};
    f.Controls.Add(dgv);
    f.Show();
}

Upvotes: 4

Related Questions