简体   繁体   中英

SSIS C# Script Task Error

All, I'm using SQL 2014 and Visual Studio 2013.

I have a script task that is not firing. It's being read, but not working. This task creates tables and inserts data into them. I need to do this with a script task as there are 100's of TSV files and the fields may change month to month and it's a pain maintaining individual nodes for each table.

If you look at the code snippet, the message boxes (1) do fire, but the script errors right after - I believe at (2):

在此处输入图片说明

The error message is:

在此处输入图片说明

I think this error refers to variables that are not accessible in the task or are misspelled, etc. I've checked these Ad nauseam - don't think that's it.

Any help is greatly appreciated. Thank you.

The problem in your code is that you are creating an ADO.NET (C# standard) connection in your script code, but the base of this - DBconn connection manager - is an OLEDB connection manager. These two connections could not be casted into one another.

Suggestions:

  • If possible, create DBconn connection manager as an ADO.NET. Then your code should work.
  • In case you have to keep DBconn as an OLEDB connection manager, you have to create a SqlConnection connection in script task based on DBconn . I have done that building connection string for ADO.NET from OLEDB conn string and creating a new SqlConnection with that connection string.

Below is a code sample for function generating Connection String.

using RuntimeWrapper = Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Data.OleDb;
using System.Data.SqlClient;
using Microsoft.SqlServer.Dts.Runtime;

static string Get_ManagedConnString(string Src_Name, ConnectionManager CM)
    {
    if (CM.CreationName != "OLEDB")
        throw new Exception(string.Format("Cannot get Conn String from non-OLEDB Conn manager {0}", CM.Name));

    RuntimeWrapper.IDTSConnectionManagerDatabaseParameters100 cmParams_Src = CM.InnerObject as RuntimeWrapper.IDTSConnectionManagerDatabaseParameters100;
    OleDbConnection oledbConn_Src = cmParams_Src.GetConnectionForSchema() as OleDbConnection;
    OleDbConnectionStringBuilder oledbCSBuilder_Src = new OleDbConnectionStringBuilder(oledbConn_Src.ConnectionString);
    SqlConnectionStringBuilder sqlCSBuilder_Src = new SqlConnectionStringBuilder();
    sqlCSBuilder_Src.DataSource = oledbCSBuilder_Src["Data Source"].ToString();
    sqlCSBuilder_Src.InitialCatalog = oledbCSBuilder_Src["Initial Catalog"].ToString();
    if (oledbCSBuilder_Src["integrated security"].ToString() == "SSPI")
        {
        sqlCSBuilder_Src.IntegratedSecurity = true;
        }
    else
        {
        sqlCSBuilder_Src.UserID = oledbCSBuilder_Src["User ID"].ToString();
        sqlCSBuilder_Src.Password = oledbCSBuilder_Src["Password"].ToString();
        }

    return sqlCSBuilder_Src.ConnectionString;
    }

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