繁体   English   中英

SSIS 脚本任务 Excel 连接打开:调用目标抛出异常

[英]SSIS Script Task Excel connection open: Exception has been thrown by the target of an invocation

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

我正在将 SSIS 个包从 2008R2 升级到 2016。我正在使用 VS2019 设置为 2016。脚本任务工作正常,直到执行Connection.Open();

我已经尝试了两个提供商,结果相同。 我使用的是 Package 参数,但将其注释掉并对该值进行了硬编码。

这是返回的异常:

DTS 脚本任务在用户代码中遇到异常:项目名称:ST_6bceaa360e1d4200a203f7c688acd0fd 调用目标引发了异常。 在 System.RuntimeMethodHandle.InvokeMethod(对象目标,对象 [] arguments,签名 sig,Boolean 构造函数)在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(对象对象,对象 [] 参数,对象 [] 参数)在 System.Reflection.RuntimeMethodInfo。 Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 在 System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture , String[] namedParams) 在 Microsoft.SqlServer.Dts.Tasks.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:\AccknowledgeData\InvoiceXLS.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();
                }
            }
        }
    }

提前感谢您的关注。

理查德

这可能是一个体系结构问题,您正在尝试从 64 位运行时使用 32 位驱动程序,您是否尝试过将 package 设置为 32 位运行时?

作为与那些喷气式驱动程序作斗争的替代方案,我开始使用Microsofts 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的实际值是什么,以便您将预期值与实际值进行比较。

暂无
暂无

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

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