简体   繁体   English

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

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

I've read about as many articles as I can stand.我已经阅读了尽可能多的文章。 Time to ask for help.是时候寻求帮助了。

I am upgrading SSIS packages from 2008R2 to 2016. I'm using VS2019 set for 2016. The script task works fine right up to the line of code that does the Connection.Open();我正在将 SSIS 个包从 2008R2 升级到 2016。我正在使用 VS2019 设置为 2016。脚本任务工作正常,直到执行Connection.Open();

I've tried both providers, same result.我已经尝试了两个提供商,结果相同。 I was using a Package Parameter, but commented that out and hard coded the value.我使用的是 Package 参数,但将其注释掉并对该值进行了硬编码。

This is the returned Exception:这是返回的异常:

DTS Script Task has encountered an exception in user code: Project name: ST_6bceaa360e1d4200a203f7c688acd0fd Exception has been thrown by the target of an invocation. DTS 脚本任务在用户代码中遇到异常:项目名称:ST_6bceaa360e1d4200a203f7c688acd0fd 调用目标引发了异常。 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()在 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()

Here is the code I'm using:这是我正在使用的代码:

    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();
                }
            }
        }
    }

Thank you in advance for taking a look.提前感谢您的关注。

Richard理查德

It might be an architecture issue, you are trying to use a 32bit driver from a 64bit runtime, have you tried setting the package to 32bit runtime?这可能是一个体系结构问题,您正在尝试从 64 位运行时使用 32 位驱动程序,您是否尝试过将 package 设置为 32 位运行时?

As an alternative to struggling with those jet drivers I've started using Microsofts OpenXML , its runs on 32bit while in VS and 64bit when deployed.作为与那些喷气式驱动程序作斗争的替代方案,我开始使用Microsofts OpenXML ,它在 VS 中运行在 32 位上,在部署时运行在 64 位上。 It installs to the GAC so you can easily reference it from a script component它安装到 GAC,因此您可以轻松地从脚本组件中引用它

Read data from excel using OpenXML 使用 OpenXML 从 excel 读取数据

If you used sConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"{0}\";Extended Properties=\"Excel 8.0;HDR={1};IMEX=1\"", sFile);如果您使用sConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"{0}\";Extended Properties=\"Excel 8.0;HDR={1};IMEX=1\"", sFile); does that work?那样有用吗? I think the issue is you aren't building your connection string properly.我认为问题是您没有正确构建连接字符串。

I have JET and ACE samples in this blog post SSIS Excel Source via Script我在这篇博文中有 JET 和 ACE 示例SSIS Excel Source via Script

The other thing is to FireInformation event and log what the actual value of sConnectionString is so you compare expected to actual.另一件事是FireInformation事件并记录sConnectionString的实际值是什么,以便您将预期值与实际值进行比较。

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

相关问题 调用的目标已引发异常 - SSIS 脚本任务错误 - Exception has been thrown by the target of an invocation - SSIS Script Task Error SSIS 脚本任务获取“调用目标已抛出异常”。 使用第 3 方 dll 运行时 - SSIS Script Task getting "Exception has been thrown by the target of an invocation." when running with 3rd party dll 调用的目标已引发异常(SSIS &amp; WinSCP &amp; C#) - Exception has been thrown by the target of an invocation (SSIS & WinSCP & C#) SSIS:调用目标已抛出错误异常 - SSIS: Error Exception has been thrown by the target of an invocation 脚本任务中的“调用目标抛出了运行时错误异常” - “Runtime error Exception has been thrown by the target of an invocation” from Script task 通过SQL Server代理在SSIS包中运行的C#脚本有时会触发调用目标抛出的异常 - C# script run in SSIS package through SQL Server Agent sometimes triggers Exception has been thrown by the target of an invocation 调用MVC的目标已引发异常 - Exception has been thrown by the target of an invocation MVC 调用的目标已抛出 sqlite 异常 - sqlite Exception has been thrown by the target of an invocation 目标调用已抛出MVC异常 - MVC Exception has been thrown by the target of an invocation LINQ和Exception已被调用目标抛出 - LINQ and Exception has been thrown by the target of an invocation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM