Accessing OLEDB Connection Managers in a Script

Published Mar 25 2019 02:11 PM 6,304 Views
Not applicable
First published on MSDN on Aug 22, 2008

Accessing ADO.Net Connection Managers from an SSIS script task / script component is pretty easy – you just need to cast the object returned from AcquireConnection() to the appropriate class (i.e. SqlConnection if you’re using SQL Native Client).

SqlConnection conn = (SqlConnection)Dts.Connections["adonet"].AcquireConnection(null);







If you can’t use ADO.Net for some reason, and are using OLEDB connection managers, it’s a little trickier. Since the AcquireConnection() method of the OLEDB connection manager returns a native COM object, I didn’t think there was a way to make this work, but today someone showed me how to do it!



By casting the Connection Manager’s InnerObject to the IDTSConnectionManagerDatabaseParameters100 interface (IDTSxxx90 in 2005), you can call the GetConnectionForSchema() method to return an OleDbConnection object.



2008 (C#):



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













2005 (VB) :



Dim cm As ConnectionManager
Dim cmParam As Wrapper.IDTSConnectionManagerDatabaseParameters90
Dim conn As OleDb.OleDbConnection

cm = Dts.Connections("oledb")
cmParam = CType(cm.InnerObject, Wrapper.IDTSConnectionManagerDatabaseParameters90)
conn = CType(cmParam.GetConnectionForSchema(), OleDb.OleDbConnection)
<br/>.csharpcode, .csharpcode pre<br/>{<br/> font-size: small;<br/> color: black;<br/> font-family: consolas, "Courier New", courier, monospace;<br/> background-color: #ffffff;<br/> /*white-space: pre;*/<br/>}<br/>.csharpcode pre { margin: 0em; }<br/>.csharpcode .rem { color: #008000; }<br/>.csharpcode .kwrd { color: #0000ff; }<br/>.csharpcode .str { color: #006080; }<br/>.csharpcode .op { color: #0000c0; }<br/>.csharpcode .preproc { color: #cc6633; }<br/>.csharpcode .asp { background-color: #ffff00; }<br/>.csharpcode .html { color: #800000; }<br/>.csharpcode .attr { color: #ff0000; }<br/>.csharpcode .alt <br/>{<br/> background-color: #f4f4f4;<br/> width: 100%;<br/> margin: 0em;<br/>}<br/>.csharpcode .lnum { color: #606060; }

Note, you’ll need to add a reference to the Microsoft.SqlServer.DTSRuntimeWrap assembly to get the IDTSConnectionManagerDatabaseParameters100 interface. If you’re doing this in a script task, you’ll need to prefix the Microsoft.SqlServer.Dts.Runtime.Wrapper namespace (or use fully qualified names) so that it doesn’t conflict with the namespace for the VSTA proxy classes.



Keep in mind that there are a couple of limitations with this approach:




  1. You won’t be able to enlist in the current transaction


  2. This connection doesn’t honor the “retain same connection” setting























ADO.Net is still the recommended connection manager type for scripts, but I found this to be a nice work around.

%3CLINGO-SUB%20id%3D%22lingo-sub-387467%22%20slang%3D%22en-US%22%3EAccessing%20OLEDB%20Connection%20Managers%20in%20a%20Script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-387467%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Aug%2022%2C%202008%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3EAccessing%20ADO.Net%20Connection%20Managers%20from%20an%20SSIS%20script%20task%20%2F%20script%20component%20is%20pretty%20easy%20%E2%80%93%20you%20just%20need%20to%20cast%20the%20object%20returned%20from%20AcquireConnection()%20to%20the%20appropriate%20class%20(i.e.%20SqlConnection%20if%20you%E2%80%99re%20using%20SQL%20Native%20Client).%3C%2FP%3ESqlConnection%20conn%20%3D%20(SqlConnection)Dts.Connections%5B%22adonet%22%5D.AcquireConnection(null)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3EIf%20you%20can%E2%80%99t%20use%20ADO.Net%20for%20some%20reason%2C%20and%20are%20using%20OLEDB%20connection%20managers%2C%20it%E2%80%99s%20a%20little%20trickier.%20Since%20the%20AcquireConnection()%20method%20of%20the%20OLEDB%20connection%20manager%20returns%20a%20native%20COM%20object%2C%20I%20didn%E2%80%99t%20think%20there%20was%20a%20way%20to%20make%20this%20work%2C%20but%20today%20someone%20showed%20me%20how%20to%20do%20it!%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3EBy%20casting%20the%20Connection%20Manager%E2%80%99s%20InnerObject%20to%20the%20IDTSConnectionManagerDatabaseParameters100%20interface%20(IDTSxxx90%20in%202005)%2C%20you%20can%20call%20the%20GetConnectionForSchema()%20method%20to%20return%20an%20OleDbConnection%20object.%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3CSTRONG%3E%202008%20(C%23)%3A%20%3C%2FSTRONG%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20ConnectionManager%20cm%20%3D%20Dts.Connections%5B%22oledb%22%5D%3B%20%3CBR%20%2F%3E%20IDTSConnectionManagerDatabaseParameters100%20cmParams%20%3D%20cm.InnerObject%20as%20IDTSConnectionManagerDatabaseParameters100%3B%20%3CBR%20%2F%3E%20OleDbConnection%20conn%20%3D%20cmParams.GetConnectionForSchema()%20as%20OleDbConnection%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3CSTRONG%3E2005%20(VB)%20%3C%2FSTRONG%3E%20%3A%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Dim%20cm%20As%20ConnectionManager%20%3CBR%20%2F%3E%20Dim%20cmParam%20As%20Wrapper.IDTSConnectionManagerDatabaseParameters90%20%3CBR%20%2F%3E%20Dim%20conn%20As%20OleDb.OleDbConnection%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20cm%20%3D%20Dts.Connections(%22oledb%22)%20%3CBR%20%2F%3E%20cmParam%20%3D%20CType(cm.InnerObject%2C%20Wrapper.IDTSConnectionManagerDatabaseParameters90)%20%3CBR%20%2F%3E%20conn%20%3D%20CType(cmParam.GetConnectionForSchema()%2C%20OleDb.OleDbConnection)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E.csharpcode%2C%20.csharpcode%20pre%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%20font-size%3A%20small%3B%3CBR%20%2F%3E%20color%3A%20black%3B%3CBR%20%2F%3E%20font-family%3A%20consolas%2C%20%22Courier%20New%22%2C%20courier%2C%20monospace%3B%3CBR%20%2F%3E%20background-color%3A%20%23ffffff%3B%3CBR%20%2F%3E%20%2F*white-space%3A%20pre%3B*%2F%3CBR%20%2F%3E%7D%3CBR%20%2F%3E.csharpcode%20pre%20%7B%20margin%3A%200em%3B%20%7D%3CBR%20%2F%3E.csharpcode%20.rem%20%7B%20color%3A%20%23008000%3B%20%7D%3CBR%20%2F%3E.csharpcode%20.kwrd%20%7B%20color%3A%20%230000ff%3B%20%7D%3CBR%20%2F%3E.csharpcode%20.str%20%7B%20color%3A%20%23006080%3B%20%7D%3CBR%20%2F%3E.csharpcode%20.op%20%7B%20color%3A%20%230000c0%3B%20%7D%3CBR%20%2F%3E.csharpcode%20.preproc%20%7B%20color%3A%20%23cc6633%3B%20%7D%3CBR%20%2F%3E.csharpcode%20.asp%20%7B%20background-color%3A%20%23ffff00%3B%20%7D%3CBR%20%2F%3E.csharpcode%20.html%20%7B%20color%3A%20%23800000%3B%20%7D%3CBR%20%2F%3E.csharpcode%20.attr%20%7B%20color%3A%20%23ff0000%3B%20%7D%3CBR%20%2F%3E.csharpcode%20.alt%20%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%20background-color%3A%20%23f4f4f4%3B%3CBR%20%2F%3E%20width%3A%20100%25%3B%3CBR%20%2F%3E%20margin%3A%200em%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E.csharpcode%20.lnum%20%7B%20color%3A%20%23606060%3B%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3ENote%2C%20you%E2%80%99ll%20need%20to%20add%20a%20reference%20to%20the%20Microsoft.SqlServer.DTSRuntimeWrap%20assembly%20to%20get%20the%20IDTSConnectionManagerDatabaseParameters100%20interface.%20If%20you%E2%80%99re%20doing%20this%20in%20a%20script%20task%2C%20you%E2%80%99ll%20need%20to%20prefix%20the%20Microsoft.SqlServer.Dts.Runtime.Wrapper%20namespace%20(or%20use%20fully%20qualified%20names)%20so%20that%20it%20doesn%E2%80%99t%20conflict%20with%20the%20namespace%20for%20the%20VSTA%20proxy%20classes.%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3EKeep%20in%20mind%20that%20there%20are%20a%20couple%20of%20limitations%20with%20this%20approach%3A%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3COL%3E%3CBR%20%2F%3E%3CLI%3EYou%20won%E2%80%99t%20be%20able%20to%20enlist%20in%20the%20current%20transaction%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EThis%20connection%20doesn%E2%80%99t%20honor%20the%20%E2%80%9Cretain%20same%20connection%E2%80%9D%20setting%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FOL%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3EADO.Net%20is%20still%20the%20recommended%20connection%20manager%20type%20for%20scripts%2C%20but%20I%20found%20this%20to%20be%20a%20nice%20work%20around.%3C%2FP%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-387467%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Aug%2022%2C%202008%20Accessing%20ADO.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-387467%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Econnectivity%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Escript%20task%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Mar 25 2019 02:12 PM
Updated by: