简体   繁体   中英

C# ace oledb 12 read-only file

I have a SSIS package with script task. c# script use ACE Oledb 12.0 provider to connect to excel file. The question is, how to connect to excel file in read-only mode (if someone open the file, my script should not have an error - it should work). The code, I tried here:

string fileToTest = Dts.Variables["User::FileName"].Value.ToString();
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
    "Data Source=" + fileToTest + @";Extended Properties=""Excel 8.0;READONLY=1""";
OleDbConnection excelConnection = new OleDbConnection(connectionString);
excelConnection.Open();

string sqlQuery = "SELECT * FROM [SheetName$A1:FZ1000]";
OleDbDataAdapter dataAdt = new OleDbDataAdapter(sqlQuery, excelConnection);
DataSet dataSt = new DataSet();
dataAdt.Fill(dataSt, "TblName1");
DataTable dataTbl = dataSt.Tables["TblName1"];

I receive oledbexception, if someone open the file.

Use google to search for that.

I searched for: "microsoft.ace.oledb.12.0 read only"

https://social.msdn.microsoft.com/Forums/office/en-US/498cd52a-b0ee-4c8d-8943-2b76055b4130/oledbconnection-read-only-mode?forum=accessdev

It looks like you can add to the connection string.

From that page:

Actually, with an OleDbConnection (assuming .net here). You can specify a read only mode in your connection string of the OleDbConnection. The following connection string will prevent you from changing data in your datasource:

const string cnnString = "Provider=Microsoft.ACE.OLEDB.12.0" 
      + ";Mode=Read" 
      + @";Data Source=|DataDirectory|\Northwind 2010.accdb";

It looks like adding ;Mode=Read to the connection string should do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM