Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi
I am using the following source code to import a dbf (DBASE) file into C# dataset and it is giving the error 'External table is not in expected format'. What have I missed? What should I do?

C#
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]; 
 
            } 


Pls Help
Regards
Nirmala Saravanan
Posted
Updated 10-Mar-13 3:56am
v3

1 solution

I have two "solutions" in one for you.

1. Is it possible that the DBF file is not dBase IV? The Mircosoft Documentation[^] lists dBase III and dBase 5.0 as other possible values.


2. I read DBF files successfully about 10 years ago using ODBC with the following ConnectionString for the DBASE files. Multiple DBF files are read via one Connection object. The DBF full path filename is specified in each SELECT statement not in the ConnectionString. I included references to Microsoft documentation for ODBC for dBase files at the end of this solution.

Below is some sample VB.NET code that should get you started.

Imports Microsoft.Data.Odbc
...
...	
Dim cn As OdbcConnection = New OdbcConnection()
cn.ConnectionString = "DRIVER={Microsoft dBase Driver (*.dbf)};Deleted=1"
cn.Open()
Dim obCommand As OdbcCommand
Dim strSQL As String = "SELECT ID,text_type,text_line,text_data FROM " & strPath & strSource_DBF_Filename
obCommand = New OdbcCommand(strSQL, cn)

Note: ID,text_type,text_line,text_data are columns names in my DBF file.

Tested: Visual Basic .NET 2003

Microsoft Documentation for ODBC DBASE Driver:
Microsoft ODBC Desktop Database Drivers[^]
dBASE Driver Programming Considerations[^]
dBASE Data Types[^]
Setting Options Programmatically for the dBASE Driver[^]
 
Share this answer
 
v8

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900