2011-06-06 61 views
2

目前,我有一个SSIS包的脚本任务内硬编码的文件路径值。如何在脚本任务中使用包变量?

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

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

回答

7

以下是在Script Task中使用变量的一种可能方式。假设你有一个变量名为FilePath在你的软件包上声明,如截图#所示,那么你可以使用下面的代码来使用Script Task中的变量。这是使用变量的可能方法之一。这里该变量仅用于使用方法LockForRead读取该值。如果使用LockForWrite方法声明变量,则也可以将值写入变量。

顺便说一下,Scrip Task代码中描述的功能也可以使用SSIS Control Flow任务列表中提供的File System 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; 
} 

截图#1:

1

+0

这就是完美的答案!谢谢 – goofyui 2011-06-06 18:29:50

+0

不知道为什么这不被接受。 非常有帮助,谢谢。 – rpcutts 2012-04-05 10:10:11

+0

我们应该在编译器的'using'语句中包含哪些名称空间来解决'Variables'声明? 我不得不用'IDTSVa​​riables100'替换'Variables',并且在调用'GetVariables'方法时用'out'代替'ref'修饰符。 – Zarepheth 2012-07-30 18:00:41

相关问题