Database development and training for Birmingham and the midlands
Specialists in Microsoft Access and Visual Foxpro database training and development
I am happy to hear from existing clients but I am taking no new calls.
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:
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.
Start by creating an empty text file. The easiest way is to right-click in Explorer and selectthen 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'scommand to create the file then it won't be empty and the properties window will complain that the udl file is corrupt.
Open thetab in the properties window and select the OLE DB provider that you want - VFP in this case. Now move to the tab and pick the database that you want to use. Click the button to make sure that all is working properly and then press to save the data and close the properties window.
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:
; Everything after this line is an OLE DB initstring
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:
One final step - delete the temporary udl file.
More details at this MSDN page.