2

I try to connect to DBF database using C# (I try 3 types of connection)

string connectionString = @"Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=c:\employees.dbf;";
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\employees.dbf;Extended Properties=dBASE IV;";
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\employees.dbf;Extended Properties=dBASE IV;User ID=Admin;Password=;";

using (OdbcConnection connection = new OdbcConnection(connectionString))
{
    connection.Open();
}

and I got error

error1:

ERROR [HY024] [Microsoft][ODBC dBase Driver] '(unknown)' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.
ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr failed
ERROR [HY024] [Microsoft][ODBC dBase Driver] '(unknown)' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.

or error2:

ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

what can be the problem ?

thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gali
  • 14,511
  • 28
  • 80
  • 105

4 Answers4

7
Dim Conn As New OLEDBConnection  
Conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\folder;Extended Properties=dBASE IV;User ID=Admin;Password=;"

To select from the database tables you must do the following (for instance):

"SELECT * FROM tblCustomers.DBF"

(Note the .DBF after the table name)

Steve Woods
  • 588
  • 4
  • 8
  • 3
    got this error: ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified – Gali Aug 29 '11 at 09:31
  • 4
    if you are using a 64bit server you need to use "Provider=Microsoft.Jet.OLEDB.12.0". I know this is an old thread, but just if someone stumble upon this..... –  Oct 27 '14 at 14:08
  • Thanks Crisim II Numenoreano – Steve Woods Nov 13 '14 at 21:28
4

Two things:

First, try the following connection string:

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\;Extended Properties=dBASE IV;";

Note you're not specifying the file name in the connection string (that will be part of your SELECT or other statements), just the path where the file(s) are.

Once you've opened the connection, that's where you use the filename. For example:

OleDbCommand cmd = new OleDbCommand("SELECT * FROM Employees");

Note that you don't add the ".dbf" extension (it's assumed - as a matter of fact, the file has to have the .dbf extension or it won't be recognized and read, at least in my experience).

EDIT

I've had the "privilege" of working with DBF files (hundreds to thousands at a time) more than I'd like. If you're still having problems leave a comment and I'll take a look tomorrow when I'm at work - the above is mostly off the top of my head with a little googling.

Tim
  • 28,212
  • 8
  • 63
  • 76
  • thanks for the help, can you send a little C# sample for this ? – Gali Aug 29 '11 at 10:16
  • @Gali - what kind of sample? I.e., a sample of how to do what (beyond what's above, I mean). – Tim Aug 29 '11 at 18:53
  • hello sir, i am getting different types of error while reading dbf files. Please check http://stackoverflow.com/questions/33687000/the-microsoft-jet-database-engine-could-not-find-object – Muneem Habib Nov 13 '15 at 11:39
0

If you are connecting to a DBF file, the filename of the DBF needs to be 8 characters or less depending on the driver you are using. I was using the Jet 4.0 and just ran into this issue and renaming the DBF from 12 to 7 characters (not including extension) worked fine.

QSmith
  • 1
-1

try this :

System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection ("Driver={Microsoft Visual FoxPro Driver};
SourceType = DBF;
SourceDB = " + System.IO.Path.GetFullPath(strFileName).Replace(System.IO.Path.GetFileName(strFileName), "") + ";Exclusive=No");
Smittey
  • 2,475
  • 10
  • 28
  • 35