We help IT Professionals succeed at work.

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

Last Modified: 2011-08-18

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.


Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)


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.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.