Search code examples
c#.netoledb

OleDb conflicts with Windows 7 running MS Office 2007


The program runs perfectly in Windows XP running MS Office 2007 but not in Windows 7 running MS Office 2007. So I decided to use Microsoft.Ace, so I used this connectionString:

string connString = "Provider=Microsoft.ACE.OLEDB.12.0;"
    + "Data Source=\"" + strDir + "\\\";"
    + "Extended Properties=\"text;HDR=YES;IMEX=1\"";

This is the method:

public DataTable Load(string path, int columnCount)
{
    CreateSchema(path, columnCount);
    DataTable dtData = new DataTable();

    string fullPath = Path.GetFullPath(path);
    string csvFile = Path.GetFileName(fullPath);
    string directoryName = Path.GetDirectoryName(fullPath);

    string query;

    string connString = "Provider=Microsoft.ACE.OLEDB.12.0;"
      + "Data Source=\"" + directoryName + "\\\";"
      + "Extended Properties=\"text;HDR=YES;IMEX=1\"";

    query = "SELECT * FROM " + csvFile;

    OleDbDataAdapter dtAdapter = new OleDbDataAdapter(query, connString);

    dtAdapter.Fill(dtData);

    dtAdapter.Dispose();

    return dtData;
}

My main problem is, the program runs in Windows XP running MS Office 2007 but not in Windows 7 running MS Office 2007 when I use this connectionString:

string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
    + "Data Source=\"" + strDir + "\\\";"
    + "Extended Properties=\"text;HDR=Yes;FMT=Delimited\"";

I came up with my research that I need to use ACE so I used this connectionString:

string connString = "Provider=Microsoft.ACE.OLEDB.12.0;"
    + "Data Source=\"" + strDir + "\\\";"
    + "Extended Properties=\"text;HDR=YES;IMEX=1\"";

But still doesnt help. Please help me! THanks!


Solution

  • The two connection strings are for different kind of files.
    The first one is for text files, the second one is for Excel 2007

    The first one use as DataSource a directory name,
    the second one requires the name of an Excel file as input on strDir

    See connectionstrings for Excel2007 and connectionstrings for Text Files

    This is probably the source of the error you have now on ACE.
    Regarding the error on XP I can only suppose that is related to a missing JET/ACE driver. In that case, the problem is due to the Platform for which you have compiled your application. Please look at this question and related answer