Search code examples
c#c#-4.0oledb

Reading A Fixed Format Text File - Part 2


This is a followup question to the question:

I am attempting to read a Fixed Format Text file using the Microsoft.ACE.OLEDB.12.0 Provider. I have a half dozen different ways to setup the driver and/or provider and pretty much run into the same problem every time. I am for some reason unable to even "get started" because of "Could not find installable ISAM" exceptions or errors and exceptions with the driver.

The system has Office 2007 installed so the "Could not find installable ISAM" does not make a great deal of sense.

Does anyone see the problem with the following code?

string DATABASE_PROVIDER = "Provider=Microsoft.ACE.OLEDB.12.0";
string CVS = Application.StartupPath + @"\Data.txt";
string connectionString = DATABASE_PROVIDER = ";Data Source=" + CVS +";Extended Properties=text;HDR=Yes;FMT=Fixed";
string field ="*";
string table ="Data";
string StringQueryCMD = "SELECT" + field+" FROM " + table;
OleDbConnection myConnection = new OleDbConnection( connectionString );
OleDbCommand cmd = myConnection.CreateCommand();
cmd.CommandText = StringQueryCmd;
myConnection.Open(); // <---- "Could not find installable ISAM" exception here
OleDataAdapter myDataAdapter = new OleDbDataAdapter(cmd);
DataTable Table = new DataTable("Data");// <---- "Could not find installable ISAM" exception here

myDataAdapter.Fill(Table);

Solution

  • I ended up going with a slightly different solution. The solution to the "Could not find installable ISAM" exception was to use the following:

    string EXTENDED_PROPERTIES = @"Extended Properties=""Text;HDR=YES;FMT=FixedLength;""";
    

    The key to the solution is the "(s) around the "Extended Properties" values. I was able to populate the DataTable with the contents of the file, I think there was a problem with the ini file, so it contained strings of "-----" which was useless to me.

    So I ended up simply reading the access database.

    string DATABASE_PROVIDER = "Provider=Microsoft.ACE.OLEDB.12.0";
    string CVS Application.StartupPath + ""\\Database.accdb";
    string DATA_SOURCE = "Data Source" + CVS;
    string connectionString = DATABASE_PROVIDER + DATA_SOURCE;
    string TABLE = " FROM STUFF";
    string SELECT = "SELECT CODE, NAME, ICON, FUNCTION;
    string StringQueryCmd = SELECT + TABLE;
    
    OleDbConnection MyConnection = new OleDbConnection(connectionString);
    OleDbCommand Command = OleDbCommand(StringQueryCmd,MyConnection);
    OleDbAdapter MyDataAdapter = new OleDbAdapter(Command);
    DataSet MyDataSet = new DataSet();
    DataTable MyDataTable = new DataTable();
    MyConnection.Open();
    MyDataAdapter.Fill(MyDataSet,"STUFF");
    MyConnection.Close();
    

    Once you have a DataTable you could in theory use LINQ to DataSet instead of dealing with the DataTable.