How do I use a package variable inside a Script task?
Currently, I have a file path value hard coded inside the Script task of an SSIS package.
I have a开发者_C百科 string variable sPath. How should I use this variable sPath inside Script Task?
string strPath = "C:\\File.xls";
if( File.Exists(strPath))
{
File.Delete(strPath);
}
Here is one possible way of using variables inside Script Task
. Assuming that you have a variable named FilePath declared on your package as shown in screenshot #1 then you can use the following code to use the variable inside the Script Task
. this is one of the possible ways to use the variable. Here the variable is used only to read the value using the method LockForRead
. You can also write values to the variable if the variable is declared with LockForWrite
method.
By the way, the functionality described in the Scrip Task
code can also be carried out using the File System Task
available in SSIS Control Flow
task list.
Hope that helps.
Using package variables inside Script Task:
C# code that can be used only in 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;
}
Screenshot #1:
精彩评论