如何通过SSIS脚本任务打开OLEDBConnection?

时间:2014-11-14 16:49:07

标签: c# sql-server ssis oledb oledbconnection

我需要处理保存在特定文件夹中的Excel文件。

在我的SSIS包中,我添加了一个Foreach循环,配置为FileEnumérator,填充filepath变量。然后,脚本任务使用此变量打开Excel文件并进行处理。

enter image description here

但是,我无法在脚本任务中打开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.xlsC:\TestFolder\export_20140102.xls

这是错误:

enter image description here

英文

  

调用目标抛出了异常

2 个答案:

答案 0 :(得分:1)

无需更换" \"用" \\"就像你要处理的检查文件一样。

编译器在转换DTS变量时会识别出这一点。

编译器将您的路径识别为C:\\ TestFolder \\ export_20140102.xls

当发生这种情况时,它会看到两个空目录,并且无法返回值。

当编译器读取DTS变量时,它将其作为文字字符串读取。

如果你改变

它应该有用
string rawfilePath = Dts.Variables["User::FilePath"].Value.ToString().Replace(@"\",@"\\");

string rawfilePath = Dts.Variables["User::FilePath"].Value.ToString());

答案 1 :(得分:0)

最后,我的剧本有效。

我的本​​地计算机上未安装数据库引擎ACE 12.0。 我从Microsoft website下载了.exe软件包,然后我将软件包的执行模式从64位更改为32位,因为安装的Microsoft Office版本是32位版本( Run64BitRuntime 项目设置中为FALSE

以下是关于how to run a 32 bit package in a 64 bit environnent的帖子。

相关问题