tags:

views:

591

answers:

1

I am trying to read a file which is just a dbase file but without the standard extension the file is something like:

Test.Dat

I am using this block of code to try and read the file:

string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Temp;Extended Properties=dBase III";
            OleDbConnection dBaseConnection = new OleDbConnection(ConnectionString);
            dBaseConnection.Open();

            OleDbDataAdapter oDataAdapter = new OleDbDataAdapter("SELECT * FROM Test", ConnectionString);
            DataSet oDataSet = new DataSet();
            oDataAdapter.Fill(oDataSet);//I get the error right here...
            DataTable oDataTable = oDataSet.Tables[0];
            foreach (DataRow dr in oDataTable.Rows)
            {
                Console.WriteLine(dr["Name"]);
            }

Of course this crashes because it can't find the dbase file called test, but if I rename the file to Test.dbf it works fine. I can't really rename the file all the time because a third party application uses it as its file format.

Does anyone know a way to read a dbase file without a standard extension in C#.

Thanks.

+1  A: 

What confuses me about your code is the way you create your ConnectionString. I would do it like this:

string databaseFile = "test.dat";
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Temp\" + databaseFile +";Extended Properties=dBase III";

When setting the file name in the Data Source part of the path, I've never had a problem with any file type/extension, no matter what it is.

As for your SELECT statement, "SELECT * FROM Test" is selecting all data from the table named "Test" in your database, not your file named "Test".

I've not worked with dBase files, but my guess is what is happening is that your data source is enough for C# to figure out what file you want using the default dBase extension, and it crashes on filling the data adapter when you aren't using the default extension. Try adding the specific file name and see if it works.

Jared Harley