Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSIS: C# Script task: How to change a SQL connection string based on server environment that the dtsx is running on?

Tags:

c#

ssis

I have a script task where i change the connection string for a db, however, it is missing one vital piece of information which is the server that the package itself is running on.

is there a way to retrieve the name of the server from a c# script?

i know in sql i can do select @@servername, but i need the package to check the name of the server that it(dtsx) is running on which is not the sql server.

i know this is an odd request, but there are many options, here are the ones that i am researching now:

+Through A Batch CMD whose results i could store in a pkg level variable
+Through a c# script
+SQL if it is possible

any other ways anyone knows of would be greatly appreciated.

EDIT/UPDATE: I found a couple of ways to do this:

C#: public string host = System.Environment.MachineName.ToString();
cmd: hostname  ...-->then store in pkg variable
like image 879
John Keats Avatar asked Dec 28 '22 13:12

John Keats


1 Answers

While you can use .NET to get the server name, the traditional SSIS way is to use the pre-existing "System::MachineName" package variable (as an aside, note that you might have to click the "Show System Variables" button to see it in the package Variables window.) Assuming SSIS 2008 and C# (2005/VB provides the same variable):

1) Add the variable name "System::MachineName" (without the quotes) to the script task editor ReadOnlyVariables property

2) Inside the script you access the variable like this:

Dts.Variables["System::MachineName"].Value.ToString()

Hope it helps. Kristian

like image 145
Kristian Wedberg Avatar answered Dec 30 '22 01:12

Kristian Wedberg