SSIS脚本任务Excel连接打开:调用的目标引发了异常



我已经读了尽可能多的文章。是时候寻求帮助了。

我正在将SSIS包从2008R2升级到2016。我使用的是为2016年设置的VS2019。脚本任务一直工作到执行Connection.Open();的代码行都很好

我试过两个提供者,结果都一样。我使用了一个Package参数,但注释掉了它,并对值进行了硬编码。

这是返回的异常:

DTS脚本任务在用户代码中遇到异常:项目名称:ST_6bceaa360e1d4200a203f7c688acd0fd调用的目标引发了异常。在系统中。RuntimeMethodHandle。InvokeMethod(Object目标、Object[]参数、Signature sig、布尔构造函数(在系统中。反射运行时方法信息。UnsafeInvokeInternal(Object obj、Object[]参数、Object[]自变量(在系统中。反射运行时方法信息。Invoke(Object obj,BindingFlagsinvokeAttr、Binder绑定器、Object[]参数、CultureInfo区域性(在系统中。运行时间类型。InvokeMember(字符串名称,BindingFlags BindingFlags,Binderbinder,Object target,Object[]providedArgs,ParameterModifier[]修饰符,CultureInfo区域性,String[]namedParams(在微软SqlServer。Dts。任务。ScriptTask。VSTATaskScriptingEngine。ExecuteScript((

这是我正在使用的代码:

public void Main()
{
Dts.TaskResult = (int)ScriptResults.Success;
string sFile;
OleDbConnection connection;
string sConnectionString;
int dataloop = 0;
//sFile = Dts.Variables["$Project::InputFilePath01"].Value.ToString();
sFile = @"C:AccknowledgeDataInvoiceXLS.xls";
if (File.Exists(sFile))
{
if (File.GetAttributes(sFile) == FileAttributes.ReadOnly)
{
//File.SetAttributes(sFile, FileAttributes.Normal);
Dts.TaskResult = (int)ScriptResults.Failure;
return;
}
//sConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sFile + ";Extended Properties=Excel 8.0";
sConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source=" + sFile + @";Extended Properties=Excel 8.0;HDR=NO";
using (connection = new OleDbConnection(sConnectionString))
{
connection.Open();
try
{
DataTable tables = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
while (dataloop < tables.Rows.Count)
{
try
{
DataRow table = tables.Rows[dataloop];
OleDbDataAdapter cmdLoadExcel = new System.Data.OleDb.OleDbDataAdapter("select count(*) from [" + table["TABLE_NAME"].ToString() + "]", connection);
DataSet ldExcelDS = new DataSet();
cmdLoadExcel.Fill(ldExcelDS);
if (ldExcelDS.Tables[0].Rows[0].ItemArray[0].ToString() != "0")
{
Dts.Variables["User::Tab"].Value = table["TABLE_NAME"].ToString();
dataloop = tables.Rows.Count;
}
}
finally
{
dataloop++;
}
}
}
catch (Exception e)
{
Dts.TaskResult = (int)ScriptResults.Failure;
Dts.ExecutionValue = "Connection String = " + connection.ConnectionString;
}
finally
{
connection.Close();
}
}
}
}

提前感谢您的光临。

Richard

这可能是一个架构问题,您正试图从64位运行时使用32位驱动程序,您是否尝试将包设置为32位运行时?

作为一种替代方案,我已经开始使用微软的OpenXML,它在VS中运行32位,在部署时运行64位。它安装到GAC,因此您可以轻松地从脚本组件引用它

使用OpenXML 从excel读取数据

如果您使用sConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="{0}";Extended Properties="Excel 8.0;HDR={1};IMEX=1"", sFile);,它能工作吗?我认为问题是你没有正确构建连接字符串。

我在这篇博客文章中有JET和ACE示例SSIS Excel Source via Script

另一件事是FireInformation事件,并记录sConnectionString的实际值,以便比较预期值和实际值。

最新更新