Establishing the connection from Script Task(VB) to OLEDB Connection Manager of SSIS Package

一曲冷凌霜 提交于 2021-01-28 14:18:47

问题


How to use the existing OLEDB connection Manager from scripttask Task(VB). i need to execute a SQL statement from "Script Task" Task.

Edit

Attempted the following code but couldn't get the connection with my OLEDB Connectionmanager

Dim MyCM As ConnectionManager = Dts.Connections("MyConnection1") 
Dim CS As String = Dts.Connections(MyCM).ConnectionString 
sqlConn = New SqlConnection(CS) 

回答1:


I would refer you to this most excellent blog post by Todd McDermid, Using Connections Properly in an SSIS Script Task




回答2:


First you must add the connection manager to the script box:

Adding a connection manager to a script component

Then, you access it in the code through the Connections object, e.g:

this.Connections.EBIC;

Check the link in the answer from user bilinkc for more detailed information




回答3:


I don't remember the link where I found this. but it woks! (only you have to change to C# or find the way to put in VB it coud be easy) SSIS has problems with cast Object_COM to IDTSConnectionManager, I wasted a lot of time with it, so OleDbConnection fix it.

Using your Connection Manager and the name of the connection OLEDB_NameConnectionTo_SQLServer, if you use storesProcedures or function in the query the character "?" is important to put the parameters after, I really don't know why so off the hook

you have to add this refences.

using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Data.OleDb; ...

try{

    ConnectionManager cm = Dts.Connections["OLEDB_NameConnectionTo_SQLServer"];
    IDTSConnectionManagerDatabaseParameters100 cmParams = cm.InnerObject as IDTSConnectionManagerDatabaseParameters100;
    OleDbConnection conn = cmParams.GetConnectionForSchema() as OleDbConnection;

    OleDbCommand cmd = new OleDbCommand("EXEC SP ?, ?, ?;", conn);

    OleDbParameter Param = cmd.Parameters.Add("@PIParam", OleDbType.Integer);
    ParametroId.Value = 3;

    OleDbParameter Cons = cmd.Parameters.Add("@PICons", OleDbType.Integer);
    Cons.Value = 2;

    OleDbParameter Cte = cmd.Parameters.Add("@PICte", OleDbType.Integer);
    Cte.Value = 1;

    using ( OleDbDataReader reader = cmd.ExecuteReader()  ){
    if ( reader.HasRows && reader.Read() )
        valor = (reader["VALUE"].ToString());
    }

    cm.ReleaseConnection(conn);

} catch( Exception ex ){
    MessageBox.Show(ex.Message.ToString());
}


来源:https://stackoverflow.com/questions/9096708/establishing-the-connection-from-script-taskvb-to-oledb-connection-manager-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!