简体   繁体   中英

how to connect to dbf database

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

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)

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.

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM