简体   繁体   中英

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

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 ac# 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

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

Kristian has the proper solution, use the native variables and skip the convoluted roads you were trying to go down. I didn't want to make such a drastic edit as this to their post but thought screenshots would help beef up the answer.

If you haven't created any variables in your package, this is what your default variable window will look like

SSIS变量窗口,默认视图

Click the grey X icon and that will display the available system variables and their current values. Some values are set at creation time - CreatorName, CreatorComputerName; others change as the result of an event (save auto-increments VersionBuild); while still others change as a result of a package being executed (StartTime, ContainerStartTime, MachineName, etc)

显示系统变量的SSIS变量窗口

Finally, please also note that some system variables are context dependent. As you can see in the scope, the OnError system variables expose things like ErrorCode, ErrorDescription, etc.

On Error事件变量

Create a new Script Task before your SQL Query tasks. Use a script task to update the ConnectionString like this (assuming your connection name is "SQLConnection":

Dts.Connections("SQLConnection").ConnectionString = "Data Source =" + System.Environment.MachineName.ToString() + ";Initial Catalog =myDataBase; Integrated Security =SSPI;"; 

I remember the UI being particularly restrictive on where and when this variable is set, but I have done this in the past. Let me know if you encounter issues.

I have earlier used an environment variable (windows) which my package reads at the startup and get is connection string from it! You can use package configuration to achieve this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM