简体   繁体   中英

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

I've tried both providers, same result. I was using a Package Parameter, but commented that out and hard coded the value.

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. 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()

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?

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. It installs to the GAC so you can easily reference it from a script component

Read data from excel using OpenXML

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

The other thing is to FireInformation event and log what the actual value of sConnectionString is so you compare expected to actual.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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