0

I'm trying to read .dbf file with datareader using OleDb like this:

const string OleDbConnectionString =
    @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydbase;Extended Properties=dBASE IV;";
    var connection = new OleDbConnection(OleDbConnectionString);
    connection.Open();

    var command = new OleDbCommand("select * from my.dbf", connection);

    reader = command.ExecuteReader();
    Console.WriteLine(reader.Read()); // true
    Console.WriteLine(reader[0].ToString()); // exception

The exception is of InvalidCastException type and says: Unable to case from System.__ComObject to IRowset. When I tried to use OleDbAdapter to fill a table everything worked fine.
How do I read .dbf file using IDataReader?

1
  • 1
    What is that my.dbf in your select command? Should that be the table name if it is not? Commented Jul 8, 2011 at 5:59

2 Answers 2

1

I think your path might be wrong since you have "C:\mybase" in the connectionstring and then add "my.dbf" which adds up to "C:\mybasemy.dbf".

Ill provide code how i open and read a dbf using a dataset instead of a reader.

string oledbConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\spcs\;Extended Properties=dBASE IV;User ID=Admin;Password=";

        OleDbConnection oledbConnection = new OleDbConnection(oledbConnectionString);

        string oledbQuery = @"SELECT * FROM KUND";

        try
        {
            OleDbCommand oledbCommand = new OleDbCommand(oledbQuery, oledbConnection);

            OleDbDataAdapter oledbAdapter = new OleDbDataAdapter(oledbCommand);

            DataSet oledbDataset = new DataSet();

            oledbAdapter.FillSchema(oledbDataset, SchemaType.Mapped);

            oledbConnection.Open();

            oledbAdapter.Fill(oledbDataset);

            foreach (DataRow row in oledbDataset.Tables[0].Rows)
            {
                System.Diagnostics.Trace.WriteLine(row[0].ToString());
            }
        }
        catch (Exception ex)
        {
            // Do something with ex
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. But I need to do it with datareader, not dataset.
0

Okay, try using GetString:

const string OleDbConnectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydbase;Extended Properties=dBASE IV;";
OleDbConnection connection = new OleDbConnection(OleDbConnectionString);
connection.Open();

OleDbCommand command = new OleDbCommand("select * from my.dbf", connection);

OleDbDataReader reader = command.ExecuteReader();
Console.WriteLine(reader.Read()); // true
Console.WriteLine("{0}", reader.GetString(0)); // exception

4 Comments

Select command is not an issue. I get everything worked using DataAdapter with the same connection string. I tried what you suggested - the result is the same exception.
@StuffHappens I just ran your code on my own DBF and it worked, could you put the reader.GetString(0)); in a try/catch and provide the exception information? Is it a value that cannot be converted to string?
@StuffHappens The first code you posted also worked for me, could this be something with your DBF file?
yes, probably dbf file is incorrect, even though I can open it with DataAdaper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.