Reading DBF files in C#

  • Thread starter Thread starter Hilton
  • Start date Start date
H

Hilton

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
 
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>
 
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

--
Cindy Winegarden
(e-mail address removed)


VFP OLE DB: http://msdn2.microsoft.com/en-us/vfoxpro/bb190232.aspx
VFP ODBC: http://msdn2.microsoft.com/en-us/vfoxpro/bb190233.aspx
 
¤ 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)
 
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.

--
Cindy Winegarden
(e-mail address removed)


VFP OLE DB: http://msdn2.microsoft.com/en-us/vfoxpro/bb190232.aspx
VFP ODBC: http://msdn2.microsoft.com/en-us/vfoxpro/bb190233.aspx
 
¤ 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)
 
Back
Top