4

I am using SSIS 2019 and am able to execute SQL Command with ADO.Net Connection Manager. I want to use OLEDB connection manager in a Script component within a Data Flow Task and I am getting the below error:

System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to class type 'System.Data.SqlClient.SqlConnection'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

enter image description here Can someone please let me know if we can use OLEDB Connection in SSIS script component in a Data flow.

Here is code for connection strings

  public override void PreExecute()
    {
        string conn = this.Connections.Connection.ConnectionString;
    }

I am not getting build error in script C# code. But getting error at script component.

5
  • Why not just use .Net SqlConnection since you’re writing a .Net script task? Commented Dec 17, 2021 at 19:03
  • Thanks for suggestion. I want to use Package.config as it deploys in multiple environments. Commented Dec 17, 2021 at 19:20
  • Ok. That will work either way. Commented Dec 17, 2021 at 19:41
  • You need to provide the C# code you are using within the script Commented Dec 21, 2021 at 9:13
  • Updated description with c# code.but issue at script component. Commented Dec 21, 2021 at 15:20

1 Answer 1

7

Using a Script Component

To access a connection manager within a script component, you should first specify this connection manager from the script component editor as shown in the image below:

enter image description here

Then, within the script component, you should use the Connections variable to access this connection manager (In this example, the connection manager assigned name is Connection):

var constr  = Connections.Connection.ConnectionString;

screenshot

enter image description here

Executing a SQL command using C#

Next, you should use this connection string to initiate a SqlConnection object in order to use it by a SqlCommand as follows:

using(SqlConnection conn = new SqlConnection(constr))
{
    using (SqlCommand cmd = new SqlCommand("Write here your SQL command", conn))
    {
        conn.Open();
        cmd.ExecuteNonQuery();
    }
}

Make sure you added a reference for using System.Data.SqlClient to use the SqlConnection and SqlCommand objects.

Important Note: If you are using SQL authentication, you should re-add the password to the connection string since it will not be retrieved from the connection manager for security reasons.

Using a Script Task

You should first retrieve the OLE DB connection string from the connection manager using the following command:

string constr = Dts.Connections["OledbCM"].ConnectionString;

Demonstration

I added Messagebox.Show(constr) command to the script task to show how the connection string looks like once retrieved:

enter image description here


Helpful Links:


Update 1: SqlConnection vs. OleDbConnection

Since you are using SqlConnection class which represents a connection to a SQL Server database. You will encounter the following error while trying to use the connection string retrieved from an OleDb Connection:

Keyword not supported "Provider"

The main reason is that the OleDB connection requires that the connection string contains the provider information since it has the ability to connect to different providers such as Oracle, Excel, Access, SQL. While SqlConnection class does not support this information since it is only using the SQL Server Native client provider.

How to solve this issue?

You can simply use a System.Data.OleDb.OleDbConnectionStringBuilder object to remove this part using the following code:

string con = Dts.Connections["OledbCM"].ConnectionString;
var connBldr = new OleDbConnectionStringBuilder(con);
connBldr.Remove("Provider");
con  = connBldr.ConnectionString;

Or you can use Linq: (Remember to add the System.Linq namespace)

string con = Dts.Connections["OledbCM"].ConnectionString;
con = string.Join(";", con.Split(';').Where(c =>
    !c.TrimStart().StartsWith("Provider")));

References

Sign up to request clarification or add additional context in comments.

12 Comments

I tried to access DTS.Connections which is giving me error stating "The type or namespace name 'Connections' does not exist in the namespace 'Microsoft.SqlServer.Dts.Runtime'" even after adding Microsoft.SqlServer.Managed.DTS.dll. Please let me know if the script task is on Dataflow or Control flow. I am facing issue and need it on Dataflow with script task and able to access OLEDB connection
I am using script component. I want to use OLEDB connection in Script component.
@AMDI check my answer update
@AMDI you need to get the connection string into a string variable then use it to create a new SQLConnection object as is mentioned in my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.