Using foxpro database file (dbf) with c#
Have your ever needed to connect to foxpro databases ?
Hopefully not , but sometimes you need , and the last day I needed.
Well you can easily OleDb classes defined in System.Data.OleDb namespace
to connecting and working with your database
But beforehand you need to installVisual FoxPro Ole DB Provider , you can get it from here
Well now its easy to connect to you dbf file with OleDbCommand and OleDbConnection and … dont forget to specify that you are using foxpro in your connection string , a simple connection string would be
Provider=vfpoledb.1;Data Source=C:\db\
c:\db\ is where your database file resides, but be careful that if you have a database file called mydb.dbf and its in c:\db\ then if you wanna get all rows of it you should just put database file location in connection string and database file name itself is a table name so in OleDbCommand we write
select * from mydb
now you can do whatever you want with your database
but just memo field in foxpro needs a trick
you can easily set any value to your memo field , but that value must be lower than 255 character
and we all know that Memo fields are limited only by the amount of available disk space so its a lot bigger than 255 character
the workaround is dividing your value into 255 charactered values.
I mean that “update mydb set memofield = ‘ string with more than 255 character ‘” wouldnt work but if you divide string into 255 charactered values like this “update mydb set memofield = ‘ string with 255 character ‘ + ‘ string with 255 character ‘ + ‘ string with 255 character ‘ ”
it would work
here is a sample code in c# , it gets all rows of mydb database
var oConn = new OleDbConnection(@"Provider=vfpoledb.1;Data Source=C:\db\");
oConn.Open();
using (var oCom = new OleDbCommand("select * from mydb", oConn))
{
var oReader = oCom.ExecuteReader();
while(oReader.Read())
{
// do what you wanna do !
}
}
oConn.Close();
well , till next time
p.s. DBFView is a cool tool for opening dbf file , check it out here
well that was sum thing really nice…….
thx