Tuesday, July 1, 2008

Dinner's ready, I already filled the FoxPro table!

Fighting my way through C# and its various... uh... specialities I ran head on into the problem of filling a FoxPro table. Being the smart guy I ventured to the Internet and looked for a solution.

Problem:
A directory of free tables to which I want to connect via C#.

Internet solution:
  1. Download the Visual FoxPro 9.0 OLEDB driver
  2. Install (duh :) )
  3. 'using System.Data;'
  4. Jump around to Connectionstrings.com and get the correct connectionstring
  5. Start filling that table!
Well so far so good, this is what I got (edited so it fits this layout):

  1. string vfpConnectionString="Provider=vfpoledb.1;Data Source=C:\\temp\\fptest\\;Collating Sequence=general;";

  2. conn = new System.Data.OleDb.OleDbConnection(vfpConnectionString);
  3. conn.Open();
  4. System.Data.OleDb.OleDbCommand Com = new System.Data.OleDb.OleDbCommand("[SQL goes here]);

  5. Com.Connection = conn
  6. Com.ExecuteNonQuery();
  7. conn.Close();
Now then... away with testing and... behold... nothing. WTF? The driver is not registered? Okies... go intall again... poo... no luck... what gives? After another round of doing the same... the same.. hehehe... not registered [instert heavy cursing on MS, SQL, FoxPro and Windows].
'Oh crap on FoxPro... I go to bed'

[Birds chirping] Next day at work:
Installed the same sequence again... WOW! Works! What gives?

Now then... education starting, sit down, be quiet and read up:

If you have, like me, a Vista64 Ultimate at home and a Vista32 Business at work... you get the drift? Yes! No 64bit with the FoxPro drivers! [claps] Very good! Get a Snickersbar and be proud.

Solution? Easy:
  1. Create your FoxPro-Accessing-Application-to-be
  2. Go to 'Build' -> 'Configuration Manager...'
  3. Find your Project (should be easy if you only have one in there)
  4. Click on the 'Platform' DropDown and choose ''
  5. 'New Platform' and choose 'x86' and '>'
  6. 'Ok' everything, make sure 'x86' is set as target CPU
  7. Build and frolic
Now you can use the FoxProtables as if they were 'normal' tables in a database. Meaning:
'Select * from ste_q3 where current=3.35'

Oh and by the way I recommend DBFManager which is quite a powerful (and inexpensive) way to sniff around in those tables without having FoxPro installed.

I hope I saved at least a bit of someone's troubles with this :)

Oh.. and I just read up on static variables in functions and C#... I go on with that in the next post, because it is a separate thing to rant about :)

No comments: