C#ace oledb 12只读文件 [英] C# ace oledb 12 read-only file

查看:137
本文介绍了C#ace oledb 12只读文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有脚本任务的SSIS软件包. C#脚本使用ACE Oledb 12.0提供程序连接到Excel文件.问题是,如何以只读模式连接到excel文件(如果有人打开该文件,我的脚本应该没有错误-应该可以工作).代码,我在这里尝试过:

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"];

如果有人打开文件,我会收到oledbexception.

I receive oledbexception, if someone open the file.

推荐答案

使用google搜索.

Use google to search for that.

我搜索:"microsoft.ace.oledb.12.0只读"

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

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.

在该页面上:

实际上,使用OleDbConnection(此处为.net).您可以在OleDbConnection的连接字符串中指定只读模式.以下连接字符串将阻止您更改数据源中的数据:

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";

看来,向连接字符串添加; Mode = Read应该可以解决问题.

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

这篇关于C#ace oledb 12只读文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆