C# and DBF files

This post was kindly contributed by SAS from Out in Left Field - go there to comment and to read the full post.

I have a client that asked me to read DBF files generated by their SAS application. Well, this posting is to explain to someone what i have learned in reading DBF files using C#. The main thing I learned was no spaces in the file path.

Here is the code that worked for me:

string connectionString = “Driver={Microsoft dBase Driver (*.dbf)};SourceType=DBF;SourceDB=C:\projects\Client\Data\WeeklyAvailableComparison;Exclusive=No; Collate=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;”;
string selectCommand = @”SELECT * FROM C:\projects\Client\Data\WeeklyAvailableComparison\ac_141.dbf”;
DataTable dt = new DataTable();

OdbcConnection oConn = new OdbcConnection();
oConn.ConnectionString = connectionString;
oConn.Open();
OdbcCommand oCmd = oConn.CreateCommand();
oCmd.CommandText = selectCommand;

dt.Load(oCmd.ExecuteReader());

I tried doing a 8.3 conversion and it wouldn’t work for the select clause.

I hope this helps someone.

Alan

This post was kindly contributed by SAS from Out in Left Field - go there to comment and to read the full post.