2

I'm using the code below to import a dbf file.

But I am getting an error:

'External table is not in expected format'

if (ofdDBF.ShowDialog()==DialogResult.OK) 
{ 
   string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ofdDBF.FileName.Substring(0, ofdDBF.FileName.LastIndexOf("\\")) + ";Extended Properties=dBASE IV;";     
   OleDbConnection conn = new OleDbConnection(connStr); 
   conn.Open();

   string cmd_string = "select * from " + ofdDBF.SafeFileName.Substring(0, ofdDBF.SafeFileName.IndexOf(".")); 
   MessageBox.Show(cmd_string); 
   OleDbDataAdapter da = new OleDbDataAdapter(cmd_string, conn); 
   DataSet ds = new DataSet(); 
   da.Fill(ds); 
   dgvImport.DataSource = ds.Tables[0]; 
} 

Any help?

Ralph Willgoss
  • 11,750
  • 4
  • 64
  • 67
  • 3
    Take a look http://www.codeproject.com/Questions/288367/External-table-is-not-in-the-expected-format and http://stackoverflow.com/questions/1139390/excel-external-table-is-not-in-the-expected-format – Soner Gönül Mar 10 '13 at 10:19

2 Answers2

5

Same problem (on 64Bit Systems), solution

Download: http://download.microsoft.com/download/b/f/b/bfbfa4b8-7f91-4649-8dab-9a6476360365/VFPOLEDBSetup.msi

string connString = @"Provider=vfpoledb;Data Source=C:\Directory;Collating Sequence=machine;";
        using (OleDbConnection con = new OleDbConnection(connString))
        {
            con.Open();

                OleDbCommand command = new OleDbCommand("Select * from Table.DBF", con);
                OleDbDataReader reader = command.ExecuteReader();
                ...
        }
user2391637
  • 51
  • 1
  • 2
0

When dealing with data sources for dBase/FoxPro, the source typically refers to EITHER a Database name, or just the path.

Once that is opened, you can just do

select * from YourTable

(no .dbf file name suffix required in the query. It will resolve itself by looking in the path or the connected database).

DRapp
  • 47,638
  • 12
  • 72
  • 142