0

Can somebody help me regarding the problem I encountered in C# and Excel. Currently, my code goes like this:

string fname = Globals.ThisAddIn.Application.ActiveWorkbook.FullName;

con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fname + ";Extended Properties=\"Excel 8.0;HDR=" + hdr + "\"");

I am trying to connect to Excel worksheet as my database from my C# application. What bother's me is that, when the workbook or my database (fname) in the connection string above is currently open or when the con.Open() method is invoked, the application opens a readonly copy of the path, How could I prevent this?

Thank you!

Ian Siñel
  • 95
  • 1
  • 11
  • 1
    I doubt you can - Excel's default behaviour is to only allow one writeable version of the file – stuartd Dec 09 '16 at 13:03
  • @stuartd Yes, it should be one instance only. But when I try to connect to the currently opened workbook to perform a query, however, Excel open a readonly copy of the same workbook. It causes me an error, and want to prevent it. :( – Ian Siñel Dec 09 '16 at 13:05
  • Related - http://stackoverflow.com/questions/18798948/read-excel-file-with-oledb-c-when-it-is-used-by-other-process – stuartd Dec 09 '16 at 13:07
  • If so, how could I perform a SELECT query in an existing or opened workbook? – Ian Siñel Dec 09 '16 at 13:07
  • Ian, Are you open to using any other methods of reading the sheet? You could read the range of cells you want without having to go through the ole db connection issues, – dgorti Dec 11 '16 at 19:01
  • Hi @dgorti . Thank you, If so, how? :) – Ian Siñel Dec 12 '16 at 08:02

0 Answers0