簡體   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