OLEDB connection string

If you want to use OLEDB to read FoxPro data from C# then you'll need a connection string, something ghastly such as:

strConnect = "Provider=VFPOLEDB.1;
  Data Source=D:\\Data\\advanced.dbc;
  Collating Sequence=MACHINE";

The problem is that most of the C# data handling examples in books and on the web assume that you will be using SQL Server and they only give information about connection strings for SQL Server. If you want to use FoxPro or Access tables then you are out of luck. There is however a quick and easy way to trick Windows into generating the string for you. All that you have to do is create a temporary udl (Universal Data Link) file.

[Icon for udl file] Start by creating an empty text file. The easiest way is to right-click in Explorer and select New then Text Document. This will create a file named 'New Text Document.txt' so press F2 and rename it to something like 'temp.udl'. The extension is important but the name doesn't matter because this is just going to be a temporary file. The icon will change as Windows recognises what it thinks is a Universal Data Link:

Setting the properties of the Data Link

If you double-click on this icon then Windows will open a Data Link Properties window so that you can edit the udl file. Note that the file must be empty. If you use FoxPro's MODIFY FILE command to create the file then it won't be empty and the properties window will complain that the udl file is corrupt.

[Editting a udl file]

Open the Provider tab in the properties window and select the OLE DB provider that you want - VFP in this case. Now move to the Connection tab and pick the database that you want to use. Click the Test Connection button to make sure that all is working properly and then press OK to save the data and close the properties window.

[Selecting a database for a udl file]

Getting the OLE DB connection string

We're now very close to getting our connection string. Open the temp.udl file in a text editor and you'll see text looking something like this:

[oledb]
; Everything after this line is an OLE DB initstring
Provider=VFPOLEDB.1;
  Data Source=D:\Data\advanced.dbc;
  Collating Sequence=MACHINE

Those last three lines are our connection string. In the file they're on a single line but I've had to wrap them here. You can paste them straight into the code that's creating the OLEDB connection to the database. The only change that you have to make is to replace the single backslashes in the path with double:

  Data Source=D:\\Data\\advanced.dbc;

One final step - delete the temporary udl file.

See Also

More details at this MSDN page.