Sometimes you have to reinvent the wheel
I got it to work, but neither with the help of the answers here nor with It's possible to use OleDbConnections with the Script Component?, and the default remarks of the "main.cs" that loads in VS at the beginning were misleading, see further below.
But it was worth the time: now I do not need to write down any connection string anymore in the C# code but just load the chosen connection manager's connection string without needing to write its name in the code.
Code
Here is the code that takes up the connection manager of the script component so that you do not need to hardcode the connection string anymore:
public override void PreExecute()
{
base.PreExecute();
string connectionString = Connections.Connection.ConnectionString;
conn = new SqlConnection() { ConnectionString = connectionString };
conn.Open();
// create temp table, make sure it matches your input
using (SqlCommand cmd = new SqlCommand(@"CREATE TABLE ##tmpTable(
[my_column1][int] NULL,
[my_column2][int] NULL,
...
Trick 1
The one trick that the other answers already tell is to make an ADO.NET connection instead of an OLE DB connection.
Trick 2
Do not listen to the remarks in the default file, the main help inside the default "main.cs" C# file in "VstaProjects" in Visual Studio that opens up if you click on "Edit script" the first time:
#region Help: Using Integration Services Connection Managers
/* Some types of connection managers can be used in this script component. See the help topic
* "Working with Connection Managers Programatically" for details.
*
* To use a connection manager in this script, first ensure that the connection manager has
* been added to either the list of connection managers on the Connection Managers page of the
* script component editor. To add the connection manager, save this script, close this instance of
* Visual Studio, and add the Connection Manager to the list.
*
* If the component needs to hold a connection open while processing rows, override the
* AcquireConnections and ReleaseConnections methods.
*
* Example of using an ADO.Net connection manager to acquire a SqlConnection:
* object rawConnection = Connections.SalesDB.AcquireConnection(transaction);
* SqlConnection salesDBConn = (SqlConnection)rawConnection;
*
* Example of using a File connection manager to acquire a file path:
* object rawConnection = Connections.Prices_zip.AcquireConnection(transaction);
* string filePath = (string)rawConnection;
*
* Example of releasing a connection manager:
* Connections.SalesDB.ReleaseConnection(rawConnection);
*/
You do not need to write the name of the connection manager anywhere, and if I am not mistaken, you cannot get the code to work by writing it down anywhere unless you hardcode the connection string yourself. Instead, the connection manager that you choose in the menu is the one that is loaded into the Connections
object. Since I could not find the name in the attributes of this object as it is said in the default remarks, I debugged the Connections
object live and found that at the point of loading Connections
, the full connection string is already an attribute of the Dynamic View of the Connections
object (and mind: this connection string is loaded from the menu without any hardcoding):
Strangely, you have to get the connection string from the Connections
object in one go: you cannot at first just load the Connections
object like a rawConnection
object that you can ask for attributes, as the default remarks tells you. That is why I replaced the rawConnection = Connections
with string connectionString = Connections.Connection.ConnectionString;
. I guess that this is needed since it is in a "Dynamic View" so that it must be fetched right away, but this is just a guess, remarks are welcome. Here is live how the debugger steps in to see how the object gets built: