【问题标题】:SSIS Script Task Excel connection open: Exception has been thrown by the target of an invocationSSIS 脚本任务 Excel 连接打开:调用目标已引发异常
【发布时间】:2021-02-25 22:09:27
【问题描述】:

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

我正在将 SSIS 包从 2008R2 升级到 2016。我正在使用 2016 年设置的 VS2019。脚本任务在执行 Connection.Open(); 的代码行之前运行良好

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

这是返回的异常:

DTS 脚本任务在用户代码中遇到异常: 项目名称:ST_6bceaa360e1d4200a203f7c688acd0fd 调用的目标已引发异常。 在 System.RuntimeMethodHandle.InvokeMethod(对象目标,对象 [] 参数,签名 sig,布尔构造函数) 在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(对象 obj,对象 [] 参数,对象 [] 参数) 在 System.Reflection.RuntimeMethodInfo.Invoke(对象 obj,BindingFlags invokeAttr、Binder binder、Object[] 参数、CultureInfo 文化) 在 System.RuntimeType.InvokeMember(字符串名称,BindingFlags bindingFlags,Binder binder、Object 目标、Object[] providedArgs、ParameterModifier[] 修饰符、 CultureInfo 文化,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();
                }
            }
        }
    }

提前感谢您的观看。

理查德

【问题讨论】:

  • 如果你使用了sConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"{0}\";Extended Properties=\"Excel 8.0;HDR={1};IMEX=1\"", sFile);,这行得通吗?我认为问题在于您没有正确构建连接字符串。 billfellows.blogspot.com/2013/04/… 我在那篇博文中有 JET 和 ACE 示例。另一件事是 FireInformation 并记录 sConnectionString 的实际值是什么,以便您将预期值与实际值进行比较。
  • billinkc - 成功了!太感谢了。如何将此标记为答案?

标签: c# excel ssis dts


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    作为我已经开始使用Microsofts OpenXML 的那些喷气机驱动程序的替代方案,它在 VS 中运行时在 32 位上运行,在部署时在 64 位上运行。它安装到 GAC,因此您可以轻松地从脚本组件中引用它

    Read data from excel using OpenXML

    【讨论】:

      猜你喜欢
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多