SSIS:C# 脚本任务:如何根据运行 dtsx 的服务器环境更改 SQL 连接字符串?

发布于 2024-12-06 07:55:14 字数 554 浏览 0 评论 0 原文

我有一个脚本任务,我在其中更改数据库的连接字符串,但是,它缺少一条重要信息,即包本身正在运行的服务器。

有没有办法从 ac# 脚本中检索服务器的名称?

我知道在sql中我可以选择@@servername,但我需要包来检查它(dtsx)正在运行的服务器的名称,该服务器不是sql服务器。

我知道这是一个奇怪的请求,但是有很多选择,以下是我现在正在研究的选项:

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

任何人知道的任何其他方法将不胜感激。

编辑/更新:我找到了几种方法来做到这一点:

C#: public string host = System.Environment.MachineName.ToString();
cmd: hostname  ...-->then store in pkg variable

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感悟人生的甜 2024-12-13 07:55:14

虽然您可以使用 .NET 来获取服务器名称,但传统的 SSIS 方法是使用预先存在的“System::MachineName”包变量(顺便说一句,请注意,您可能必须单击“显示系统变量”按钮在包变量窗口中查看它。)假设 SSIS 2008 和 C#(2005/VB 提供相同的变量):

1) 将变量名称“System::MachineName”(不带引号)添加到脚本任务编辑器 ReadOnlyVariables 属性

2) 在脚本内,您可以像这样访问变量:

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

希望它有帮助。
克里斯蒂安

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

凉世弥音 2024-12-13 07:55:14

克里斯蒂安有正确的解决方案,使用本机变量并跳过您试图走的复杂道路。我不想对他们的帖子进行如此激烈的编辑,但我认为屏幕截图将有助于增强答案。

如果您尚未在包中创建任何变量,则默认变量窗口将如下所示

SSIS 变量窗口,默认视图

单击灰色 X 图标,将显示可用的系统变量及其当前值。有些值是在创建时设置的 - CreatorName、CreatorComputerName;其他因事件而发生变化(保存自动增量 VersionBuild);还有一些会因包的执行而发生变化(StartTime、ContainerStartTime、MachineName 等)

SSIS 变量窗口显示系统变量

最后,还请注意,某些系统变量是依赖于上下文的。正如您在范围中看到的,OnError 系统变量公开了 ErrorCode、ErrorDescription 等内容。

On Error event Variables

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 variable window, default view

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 variable window showing System variables

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 event variables

烟若柳尘 2024-12-13 07:55:14

我之前使用过一个环境变量(Windows),我的包在启动时读取它并从中获取连接字符串!
您可以使用包配置来实现此目的。

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.

放肆 2024-12-13 07:55:14

在 SQL 查询任务之前创建一个新的脚本任务。使用脚本任务来更新 ConnectionString,如下所示(假设您的连接名称是“SQLConnection”:

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

我记得 UI 对于设置此变量的位置和时间有特别限制,但我过去已经这样做过。如果您这样做,请告诉我)遇到问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文