【发布时间】:2015-01-12 03:17:28
【问题描述】:
我需要处理保存在特定文件夹中的 Excel 文件。
在我的 SSIS 包中,我添加了一个配置为文件枚举器的 Foreach 循环,填充了一个 filepathvariable。然后,脚本任务使用此变量打开 Excel 文件并对其进行处理。
但是,我无法在脚本任务中打开指向我的文件的 OLEDB 连接。
filepath包含有效路径。我在脚本中添加了一个测试来检查文件。
这是我的代码示例:
// Check file to process.
string rawfilePath = Dts.Variables["User::FilePath"].Value.ToString().Replace(@"\",@"\\");
if (rawfilePath == String.Empty || !File.Exists(rawfilePath))
Dts.Events.FireError(0, SCRIPT_TASK_NAME, "Invalid input file '" + rawfilePath + "'...", String.Empty, 0);
MessageBox.Show(rawfilePath);
// Open connection.
string rawFileConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='"
+ rawfilePath + "';Extended Properties='Excel 12.0;HDR=NO;IMEX=1'";
MessageBox.Show(rawFileConnectionString);
OleDbConnection rawExcelConnection = new OleDbConnection(rawFileConnectionString);
rawExcelConnection.Open();
我的文件夹是C:\TestFolder。它包含两个文件:C:\TestFolder\export_20140101.xls 和 C:\TestFolder\export_20140102.xls。
这是错误:
英文
调用目标抛出异常
【问题讨论】:
-
你的消息框会触发吗?哪一行失败了,
rawExcelConnection的实例化还是Open调用? -
在调试模式下运行或将整个代码放入 try catch 块中,并在 catch 块中使用带有异常消息的 Dts.FireError
-
try/catch 块救了我的命!。该错误与数据库引擎(ACE 12.0)有关。驱动没找到,之前没安装过。
标签: c# sql-server ssis oledb oledbconnection