0
votes

I have a legacy Visual FoxPro database from which I need to get the data from. I have DBC, DCT, DCX & FPT files in the database folder.

I installed the Visual FoxPro provider & driver and wrote a short C# program to access the data. I also made sure the program is running in 32bit mode, since I understood that the VFP driver is 32bit. However I'm getting the following error:

Additional information: Connectivity error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

I made sure that the driver name ("Microsoft Visual FoxPro Driver") as appears in the Windows ODBC 32bit administration tool is specified in the connection string, but it doesn't seem to help.

The code:

OleDbConnection yourConnectionHandler = new OleDbConnection(
 @"Provider=VFPOLEDB.1;DRIVER=Microsoft Visual FoxPro Driver;Data Source=mydatabase.dbc;Mode=Read;Collating Sequence=machine");

// Open the connection, and if open successfully, you can try to query it
yourConnectionHandler.Open();

if (yourConnectionHandler.State == ConnectionState.Open)
{
    string mySQL = "select * from tablename";

    var command = yourConnectionHandler.CreateCommand();
    command.CommandText = mySQL;
    var res = command.ExecuteReader(); // throws the error

    yourConnectionHandler.Close();
}

I've been stuck on this for a long time, and have tried everything I could find on the net. Any idea?

1
Possibly useful. Perhaps try the .dbf alternativeMickyD
...also odbc tag isn't oledbMickyD
@MickyD - the existing table file is in DBC/DCT format, so I don't have a DBF format.Metheny
The DBC is the database container, the DBF's live in it, they should be in the same folder as the DBCHank
Your connection string needs to point to a specific DBF within the DBC.Steve

1 Answers

0
votes

You are saying provider and trying to use OleDbConnection (which is right) but defining an ODBC driver within the connection string. In other words, your connection string is wrong.

using (OleDbConnection cn = new OleDbConnection(
 @"Provider=VFPOLEDB;Data Source=c:\MyPath\mydatabase.dbc;Mode=Read;Collating Sequence=machine"))
using (OleDbCommand cmd = new OleDbCommand("select * from tablename", cn))
{
    cn.Open();
    var reader = cmd.ExecuteReader(); 
    // do something with the reader. ie:
    // someDataTable.Load(reader);
    cn.Close();
}

It works wonderfully well. In connectionstring you don't need to use the database name, you can just use the tables' path as the data source.