C# ace oledb 12 read-only file

左心房为你撑大大i 提交于 2019-12-12 04:12:48

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/45165570/c-sharp-ace-oledb-12-read-only-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!