Reading dbase file without standard .dbf extension
I'm trying to read a file that is just a database file, but without the standard extension, the file looks something like this:
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 it fails because it cannot find the dbase file called test, but if I rename the file to Test.dbf it works fine. I cannot rename the file all the time because a third party application is using it as the file format.
Does anyone know a way to read a database file without the standard extension in C #.
Thanks.
a source to share
What confuses me with your code is how 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 filename in the data source part of the path, I never had any problems with any file type / extension, no matter what it is.
As for your SELECT statement, "SELECT * FROM Test" fetches all data from the table named "Test" in your database, not your file named "Test".
I have not worked with dBase files, but my guess is that what is happening is that your datasource is enough for C # to figure out which file you want the default dBase to use, and it crashes when populating the data adapter when you are not using the extension default. Try adding a specific file name and see if it works.
a source to share