Read/Write data to/from a DBF file using C#


How can I read/write data directly to a DBF file? If you suggest a third party tool, please make sure it is freeware without any license implications. I have resonable exposure to c# programming to coding tips might do the trick as well.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

From How to: Access Visual FoxPro Data in Visual Studio

To access a Visual FoxPro data source in Visual C#
Use a connection string to specify the Visual FoxPro OLE DB Provider and access the data source as shown in the following code:

OleDbConnection oleDbConnection1 = new OleDbConnection("Provider=VFPOLEDB.1;" +
      "Data Source=C:\\myVFPDatabase.DBC;");

Microsoft OLE DB Provider for Visual FoxPro

OLE DB Provider for Visual FoxPro

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
raheelasadkhanAuthor Commented:
Thanks for the help guys.

mikegagon, I appreciate your answer and think it is fair but unfortunately the library uses VC7 and I am not comfortable using code I do not understand. Thanks anyways :).

CarlWarner, thank you for a comprehensive response. This is exactly what I was looking for. I like the fact that you took out time to include the VFP OLEDB provider link. I am developing 10 projects simultaneously in 5 languages so not always clear in the head. This saves me a lot of time. I am ready to accept your answer as is but if you like you could answer the following extension question and I will double the points before accepting. Either ways, drop me a line so I can accept.

Once the connection has been made, could you give me an example of how to iterate through the records using the OleDataReader? I would want to stay away from the DataAdapters/DataSet approach. Before your answer, I already managed to read DBF data using the ADODB interop but had trouble with specifying the date formats in date fields in insert/update statements. After some searching, found the {^yyyy-mm-dd[,hh[:mm[:ss]]]} approach. When you provide the OleDataReader example, please consider that I have three datatype fields, integer, string and date. Thanks.
I'm certainly not the expert on OLEDB to anything, but I have toyed around with it enough within an active Visual FoxPro session to know that it is pretty powerful stuff.  And I know zero about C#.  The reason I don't lnow OLEDB to a .dbf is because I use the .dbfs directly in my VFP programming environment.

Here are some commands I just used within VFP9 to get some data back from a .dbf and show a field value and name on the first record and then move on to the second record.  I think you may get the general idea from this.

o.Open('provider=vfpoledb;data source=c:\documents and settings\CJW\my documents\visual foxpro projects\')

ors = o.Execute([SELECT * FROM MATERIAL])    && create a RecordSet object
? ors.Fields(0).Value    && returns the value of the first field at the first record
? ors.Fields(0).Name    && returns the name of the first field at the first record
? ors.Fields(1).Value    && returns the value of the second field at the first record
? ors.Fields(1).Name    && returns the name of the first field at the first record
? ors.MoveNext    && Moves to the next record (the 2nd) in this active RecordSet
? ors.Fields(0).Value    && returns the value of the first field from the second record we just moved to
? ors.Fields(0).Name    && returns the name of the first field from the second record we just moved to

Obviously, you can setup a loop to increment right on through all of the data to grab what you like.  Of course, you can make the contents of the RecordSet smaller by limiting the fields returned in the SQL SELECT call and even further by adding a pertinent WHERE clause into that SQL SELECT command.

Here is a reference to where I picked up details as far as using these commands within VFP.  The techniques, while in VFP syntax, should give you an idea of the general idea on how to proceed in your programming environment.  

ADO Jumpstart for Microsoft Visual FoxPro Developers

Hope this helps.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.