gpt4 book ai didi

sql-server - 如何在脚本任务中使用包变量?

转载 作者:行者123 更新时间:2023-12-03 09:55:01 28 4
gpt4 key购买 nike

目前,我在 SSIS 包的脚本任务中硬编码了一个文件路径值。

我有一个字符串变量 sPath。我应该如何在 Script Task 中使用这个变量 sPath?

string strPath = "C:\\File.xls";
if( File.Exists(strPath))
{
File.Delete(strPath);
}

最佳答案

这是在 Script Task 中使用变量的一种可能方式。假设您在包中声明了一个名为 FilePath 的变量,如屏幕截图 #1 所示,那么您可以使用以下代码在 Script Task 中使用该变量。这是使用变量的可能方法之一。此处变量仅用于使用方法 LockForRead 读取值。如果变量是使用 LockForWrite 方法声明的,您还可以将值写入变量。

顺便说一下,Scrip Task 代码中描述的功能也可以使用 SSIS Control Flow 中的File System Task 执行> 任务列表。

希望对您有所帮助。

在脚本任务中使用包变量:

C# 代码,只能在 SSIS 2008 及更高版本 中使用。.

public void Main()
{
Variables varCollection = null;
string FilePath = string.Empty;

Dts.VariableDispenser.LockForRead("User::FilePath");
Dts.VariableDispenser.GetVariables(ref varCollection);

FilePath = varCollection["User::FilePath"].Value.ToString();

if (File.Exists(FilePath))
{
File.Delete(FilePath);
}

varCollection.Unlock();

Dts.TaskResult = (int)ScriptResults.Success;
}

屏幕截图 #1:

1

关于sql-server - 如何在脚本任务中使用包变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6254907/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com