Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / April 2007

Tip: Looking for answers? Try searching our database.

Reading DBF files in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hilton - 23 Apr 2007 20:10 GMT
Hi,

What is the easiest way to read DBF files in C#?  This is a preprocessing
step and performance is a very low priority.  I don't need to process
indices etc, just read each record, grab text data out of a few fields and
move to the next record.

Thanks,

Hilton
Michael Nemtsev - 23 Apr 2007 20:34 GMT
Hello Hilton,

Have u tried smth? For example using DBF over ODBC/OLEDB provides?
If you really anxion about performantc in can turn out that only direct access
to DB will help u - I'm not shure whether C# native libs for DBF exists but
there are several unmanaged like codebase or alxbase

---
WBR,  Michael  Nemtsev [.NET/C# MVP].  
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

H> Hi,
H>
H> What is the easiest way to read DBF files in C#?  This is a
H> preprocessing step and performance is a very low priority.  I don't
H> need to process indices etc, just read each record, grab text data
H> out of a few fields and move to the next record.
H>
H> Thanks,
H>
H> Hilton
H>
Cindy Winegarden - 24 Apr 2007 15:29 GMT
Hi Hilton,

Assuming your DBFs are FoxPro DBFs, you can download the FoxPro and Visual
FoxPro OLE DB data provider from the link before my signature. It works with
all versions of FoxPro DBFs.

Here's some example VB code:

Try

   Dim cn1 As New OleDbConnection( _
       "Provider=VFPOLEDB.1;Data Source=C:\Temp\;")
   cn1.Open()
   '-- Make some VFP data to play with
   Dim cmd1 As New OleDbCommand( _
       "Create Table TestDBF (Field1 I, Field2 C(10))", cn1)
   Dim cmd2 As New OleDbCommand( _
       "Insert Into TestDBF Values (1, 'Hello')", cn1)
   Dim cmd3 As New OleDbCommand( _
       "Insert Into TestDBF Values (2, 'World')", cn1)
   cmd1.ExecuteNonQuery()
   cmd2.ExecuteNonQuery()
   cmd3.ExecuteNonQuery()
   cn1.Close()

   Dim cn2 As New OleDbConnection( _
       "Provider=VFPOLEDB.1;Data Source=C:\Temp\;")
   cn2.Open()

   Dim cmd4 As New OleDbCommand( _
       "Select * From TestDBF", cn2)
   Dim da1 As New OleDbDataAdapter(cmd4)
   Dim ds1 As New DataSet
   Dim dr1 As DataRow
   da1.Fill(ds1)
   For Each dr1 In ds1.Tables(0).Rows
       Console.WriteLine(dr1.Item(1).ToString())
   Next
   Console.ReadLine()
   cn2.Close()

Catch e As Exception
   MsgBox(e.ToString())
End Try

Signature

Cindy Winegarden
cindy@cindywinegarden.com

VFP OLE DB: http://msdn2.microsoft.com/en-us/vfoxpro/bb190232.aspx
VFP ODBC: http://msdn2.microsoft.com/en-us/vfoxpro/bb190233.aspx

> What is the easiest way to read DBF files in C#?  ... just read each
> record, grab text data out of a few fields and move to the next record.
Paul Clement - 24 Apr 2007 15:30 GMT
¤ Hi,
¤
¤ What is the easiest way to read DBF files in C#?  This is a preprocessing
¤ step and performance is a very low priority.  I don't need to process
¤ indices etc, just read each record, grab text data out of a few fields and
¤ move to the next record.

You can use the Jet OLEDB Provider and dBase ISAM driver (should be easy to convert the following to
C#):

       Dim ConnectionString As String

       ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
           "Data Source=e:\My Documents\dBase;Extended Properties=dBase IV"
       Dim dBaseConnection As New System.Data.OleDb.OleDbConnection(ConnectionString)
       dBaseConnection.Open()

       Dim dBaseCommand As New System.Data.OleDb.OleDbCommand("SELECT * FROM MyDBase",
dBaseConnection)
       Dim dBaseDataReader As System.Data.OleDb.OleDbDataReader =
dBaseCommand.ExecuteReader(CommandBehavior.SequentialAccess)

       While dBaseDataReader.Read
           Console.WriteLine(dBaseDataReader("Column1").ToString)
           Console.WriteLine(dBaseDataReader("Column2").ToString)
           Console.WriteLine(dBaseDataReader("Column3").ToString)
       End While

       dBaseConnection.Close()

Just keep in mind that the Data Source for a dBase file is the folder path of the file location The
filename is specified in the SQL statement (sans file extension).

Paul
~~~~
Microsoft MVP (Visual Basic)
Cindy Winegarden - 24 Apr 2007 20:14 GMT
Hi Paul,

Just so you'll know, DBFs created with VFP3 and above (now in version 9) are
not compatible with Jet. In fact, DBFs with new data features added in VFP7
and above aren't compatible with the VFP ODBC driver and OLE DB must be
used. The latest FoxPro and Visual FoxPro OLE DB data provider is totally
backward compatible to work with all versions of FoxPro DBFs.

Also, you're right about pointing the connection string to the folder where
the DBFs are, but if there is a DBC (database container, holds stored
procedures, referential integrity info, etc.) then the DBFs are most likely
"contained" by the DBC and the connection string should point to the DBC.
("Provider=VFPOLEDB.1;Data Source=C:\Temp\MyDBC.dbc;")

As you pointed out, in call cases the table itself is only referenced in the
SQL statement itself.

Signature

Cindy Winegarden
cindy@cindywinegarden.com

VFP OLE DB: http://msdn2.microsoft.com/en-us/vfoxpro/bb190232.aspx
VFP ODBC: http://msdn2.microsoft.com/en-us/vfoxpro/bb190233.aspx

> You can use the Jet OLEDB Provider and dBase ISAM driver ....
> Just keep in mind that the Data Source for a dBase file is the folder path
> of the file location The
> filename is specified in the SQL statement (sans file extension).
Paul Clement - 24 Apr 2007 20:41 GMT
¤ Hi Paul,
¤
¤ Just so you'll know, DBFs created with VFP3 and above (now in version 9) are
¤ not compatible with Jet. In fact, DBFs with new data features added in VFP7
¤ and above aren't compatible with the VFP ODBC driver and OLE DB must be
¤ used. The latest FoxPro and Visual FoxPro OLE DB data provider is totally
¤ backward compatible to work with all versions of FoxPro DBFs.
¤
¤ Also, you're right about pointing the connection string to the folder where
¤ the DBFs are, but if there is a DBC (database container, holds stored
¤ procedures, referential integrity info, etc.) then the DBFs are most likely
¤ "contained" by the DBC and the connection string should point to the DBC.
¤ ("Provider=VFPOLEDB.1;Data Source=C:\Temp\MyDBC.dbc;")
¤
¤ As you pointed out, in call cases the table itself is only referenced in the
¤ SQL statement itself.

Hi Cindy,

Yeah, whenever I see DBF I tend to assume dBase. I tend to ignore the fact that these files
represent other database types as well. ;-)

Paul
~~~~
Microsoft MVP (Visual Basic)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.