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

goo*_*yui 2 sql-server ssis sql-server-2008

目前,我在SSIS包的Script任务中有一个硬编码的文件路径值.

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

string strPath = "C:\\File.xls";
if( File.Exists(strPath))
{
    File.Delete(strPath);
}
Run Code Online (Sandbox Code Playgroud)

小智 7

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

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

希望有所帮助.

在Script Task中使用包变量:

只能使用的C#代码SSIS 2008 and above..

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;
}
Run Code Online (Sandbox Code Playgroud)

截图#1:

1