你做对了。我创建了一个基本的 SSIS 包。有一个变量,FolderPath,类型字符串。有一个触发信息事件的脚本任务,其内容公开了 FolderPath 变量的值
然后我创建了一个像这样的基本 C# 控制台应用程序
public class InformationListener : DefaultEvents
{
public override void OnInformation(DtsObject source, int informationCode, string subComponent, string description, string helpFile, int helpContext, string idofInterfaceWithError, ref bool fireAgain)
{
//base.OnInformation(source, informationCode, subComponent, description, helpFile, helpContext, idofInterfaceWithError, ref fireAgain);
Console.WriteLine(string.Format("{0} {1}", subComponent, description));
}
}
class Program
{
static void Main(string[] args)
{
string sourcePackage = string.Empty;
string path = string.Empty;
string variableName = string.Empty;
string designValue = string.Empty;
string newValue = string.Empty;
InformationListener listener = null;
sourcePackage = @"J:\Src\SO\SSIS\Package.dtsx";
path = @"J:\runtime";
variableName = "User::FolderPath";
listener = new InformationListener();
Application app = new Application();
Package pkg = null;
Variable ssisVariable = null;
pkg = app.LoadPackage(sourcePackage, null);
ssisVariable = pkg.Variables[variableName];
designValue = ssisVariable.Value.ToString();
Console.WriteLine(string.Format("Designtime value = {0}", designValue));
ssisVariable.Value = path;
newValue = ssisVariable.Value.ToString();
Console.WriteLine(string.Format("new value = {0}", newValue));
DTSExecResult results = DTSExecResult.Canceled;
results = pkg.Execute(null, null, listener, null, null);
Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
}
从变量检查中可以看出
并从我的印刷声明中
的设计时值是C:\designTime
因为J:
我忘记在上面转义我的字符串,但我们可以假装它显示J:\runtime
。
综上所述,除非我们通过调用SaveToXml
方法来序列化包,否则一旦对象超出范围,User::FolderPath 的值就会重置为设计时值。永久更新看起来像
app.SaveToXml(sourcePackage, pkg, null);
OP EDIT这个讨论和例子让我得到了答案:
http ://social.msdn.microsoft.com/Forums/sqlserver/en-US/dad8e218-1fe0-49db-89da-5715fb6d4b21/sql-2008-r2-ssis- c-script-task-not-setting-variable