我正在尝试从C#开始的SSIS包上的代码中设置变量。
Package pkg = app.LoadPackage(ConfigurationManager.AppSettings["SsisPkg"].ToString(), null);
pkg.Variables["User::FolderPath"].Value = Path.GetDirectoryName(e.FullPath);
pkg.Variables["User::FileName"].Value = Path.GetFileNameWithoutExtension(e.FullPath);
在调试时,我会在设置它们后直接检查这些值,但是没有任何变化。我可以深入研究该值(将鼠标悬停,导航至该值,然后编辑该值),这样看来它们并不是不可编辑的。
有任何想法我在这里做错了吗?
更新:感谢billinkc,只要满足其他条件,我就很满意自己的工作。我发现代码中的直接分配失败(没有错误,只是不要“拿走”),而且我无法在监视窗口中编辑值。我可以当我检查他们编辑值。
实际的问题可能是存在将其标记为只读的设置吗?
找到答案:需要在设置值之前使变量可写:
pkg.Variables["User::FileName"].EvaluateAsExpression = false;
您做对了。我创建了一个基本的SSIS包。有一个变量,FolderPath,类型为字符串。有一个脚本任务会触发一个Information事件,并且该事件的内容将显示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
go,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脚本任务未设置变量
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句