1

I am facing very strange issue. I have written class to which reads dbf file through oledb connection. I have downloaded dbf file from internet and it is reading all data correctly.

DBF file location: E:\Projects\SLAVE.DBF

I am facing following 2 issues

1) When I try to read other dbf file then it is reading only its table fields. it is not reading table fields data. E:\Projects\line75.dbf

2) The other issue I am facing I have DBF files when I put these files in location then i am getting exception that

microsoft jet database engine does not find required object. Are you missing some directive or path. E:\Projects\SDW_plnParcel.dbf

I am totally confused why it is reading SLAVE.DBF downloaded from internet correct, why it is not reading TABLE FIELDS DATA of line75.dbf and why it is throwing exception on SDW_plnParcel.dbf.

My class and one function for this class is as follows:

public class dbfHandler
{
    public dbfHandler()
    {
        this.dbfTable = new DataTable();
    }
    public void initconnection(String filepath) // initialise dbconnection
    {
        String[] splitString = filepath.Split('\\');
        this.filename = splitString[splitString.Length - 1];
        splitString = splitString.Where(w => w != splitString[splitString.Length - 1]).ToArray();
        String folderPath = String.Join("\\", splitString);
        this.dbConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + folderPath + ";Extended Properties=dBase III");
        this.dbConnection.Open();
    }
    public List<String> getcolumnvalues(int fieldIndex, List<int> rowIndexes)
    {
        List<String> columnvalues = new List<string>();
        try
        {
            if(this.dbConnection.State == ConnectionState.Open)
            {
                string mySQL = "select * from " + this.filename;  // dbf table name
                OleDbCommand MyQuery = new OleDbCommand(mySQL, this.dbConnection);
                OleDbDataReader reader = MyQuery.ExecuteReader();
                int rowCount = 0;
                while(reader.Read())
                {
                    bool match = rowIndexes.Any(item => item == rowCount);
                    if(match == true)
                    {
                        String value = reader.GetValue(fieldIndex).ToString();
                        columnvalues.Add(value);
                    }
                    rowCount++;
                }
                reader.Close();
            }
        }
        catch(Exception e)
        {
            throw e;
        }
        return columnvalues;
    }
    private String filename;
    private DataTable dbfTable;
    private OleDbConnection dbConnection;
}
Muneem Habib
  • 1,046
  • 4
  • 18
  • 49

1 Answers1

1

When dealing with .DBF files, I have always had better results working with Microsoft's Visual Foxpro OleDb Provider

The connection string in simplified format

var connString = @"Provider=VFPOLEDB.1;Data Source=C:\SomePathToData;";

Now, instead of doing the data reader -- just to make sure you can get / see what you are expecting, try using a DataAdapter...

var da = new OleDataAdapter( yourSqlCmdObject, yourConnection)
var dt = new DataTable();
da.Fill(dt);

It should pull all columns from your query and all rows into proper data column types... Then you could cycle through all the column names, rows, etc..

foreach( DataColumn dc in dt.Columns )
   var tmp = dc.ColumnName;

foreach( DataRow dr in dt.Rows )
{
   object x = dr[0];   // get VALUE from column 0
   x = dr["SpecificColumn"];   // if you KNOW the column name
}

Of which, you could tweak as needed. But if you only need a SPECIFIC column (or limited columns), change your query to quantify that.

Select OneField from YourTable...
DRapp
  • 47,638
  • 12
  • 72
  • 142
  • sir i am getitng error DBF is not a table. I have used the almost same code as aboce. it is reading some DBF file but in most cases it is giving error that DBF is not table – Muneem Habib Nov 17 '15 at 10:27
  • @MuneemHabib, is it possible that the dbf file you are attempting to use has been encrypted by a 3rd party utility? Such as Cryptor? If so, the native OleDb won't recognize it – DRapp Nov 17 '15 at 12:28
  • @MuneemHabib - I get this message when opening a table with more than 255 fields/columns. I've been googling this but I can't seem to fix this without pulling a rabbit out of my a... hat! – DerpyNerd Jan 07 '16 at 09:23
  • 1
    @RobbieVercammen, DBF tables have a limit of 255 columns. If your table has more than that, then I suspect a different database provider, such as SyBase Advantage Database Server or SyBase Advantage Local Server which can read / work with DBF files, but has its own database container to handle larger than standard DBF context. – DRapp May 05 '16 at 10:14