简体   繁体   English

C#ace oledb 12只读文件

[英]C# ace oledb 12 read-only file

I have a SSIS package with script task. 我有一个带有脚本任务的SSIS软件包。 c# script use ACE Oledb 12.0 provider to connect to excel file. C#脚本使用ACE Oledb 12.0提供程序连接到Excel文件。 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). 问题是,如何以只读模式连接到excel文件(如果有人打开该文件,我的脚本应该没有错误-应该可以工作)。 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. 如果有人打开文件,我会收到oledbexception。

Use google to search for that. 使用谷歌搜索。

I searched for: "microsoft.ace.oledb.12.0 read only" 我搜索:“ microsoft.ace.oledb.12.0只读”

https://social.msdn.microsoft.com/Forums/office/en-US/498cd52a-b0ee-4c8d-8943-2b76055b4130/oledbconnection-read-only-mode?forum=accessdev https://social.msdn.microsoft.com/Forums/office/zh-CN/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). 实际上,使用OleDbConnection(此处为.net)。 You can specify a read only mode in your connection string of the OleDbConnection. 您可以在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. 看起来,向连接字符串添加; Mode = Read应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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