我有一个 SSIS 包,其中设置了一些 Project.params。
如何通过 C# 将这些参数的值传递给 SSIS 包?
我正在尝试以下操作:
const string pkgLocation = @"export.dtsx";
var app = new Application();
var pkg = app.LoadPackage(pkgLocation, null);
var results = pkg.Execute();
这将返回一个失败,错误集合包含“在变量集合中找不到变量“$Project::connString”。该变量可能不存在于正确的范围内。”
所以我尝试添加
var param = pkg.Parameters.Add("connString", TypeCode.String);
param.Value = "test";
var results = pkg.Execute();
但这会抛出 DtsGenericException。
最佳答案
我想我明白了。诀窍是将您的 ispac 文件(VS 构建此文件,但您可以通过 msbuild 执行此操作)反序列化为 Project
对象。 Project 对象允许我们设置项目级参数(以及访问项目级连接管理器)。
从那里我们需要获得对我们想要的特定包的引用,但它将是一个 PackageItem
。 PackageItems 无法运行,但它们确实具有我们将用于实例化 Package
类的 Package 属性,该类确实具有Execute
方法
public static void final()
{
string isPacPath = @"C:\sandbox\so_31812951\so_31812951\bin\Development\so_31812951.ispac";
string packageName = "Package.dtsx";
Application app = new Application();
Package pkg = null;
// https://msdn.microsoft.com/en-us/library/ff930196(v=sql.110).aspx
Project proj = null;
PackageItem pi = null;
DTSExecResult results;
///////////////////////////////////////////////////////////////////
// Run an SSIS package that has a Project parameter
///////////////////////////////////////////////////////////////////
proj = Project.OpenProject(isPacPath);
// Yes, I can see the packages in there
foreach (var item in proj.PackageItems)
{
Console.WriteLine(string.Format("Project {0} contains package {1}", proj.Name, item.StreamName));
}
//Also able to see the project level parameters
foreach (Parameter item in proj.Parameters)
{
Console.WriteLine(string.Format("Project {0} contains parameter {1} type of {2} current value {3}", proj.Name, item.Name, item.DataType, item.Value));
}
// assign a value to my project level parameter
proj.Parameters["ProjectParameter"].Value = 10;
// Get the package from the project collection
pi = proj.PackageItems[packageName];
// Convert the package into a package object
pkg = pi.Package;
// This is how we specify a package parameter value
pkg.Parameters["PackageParam"].Value = 777;
results = pkg.Execute();
Console.WriteLine(results);
}
假设您有一个名为 so_31812951
的 SSIS 项目,该项目编译为一个位于 C:\sandbox\so_31812951\so_31812951\bin\Development\so_31812951.ispac 的 ispac 这个项目有一个名为 Package 的包.dtsx。将有一个名为 ProjectParameter
的项目级参数 Int32 以及一个名为 PackageParam
关于c# - 从 C# 传递 SSIS 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31812951/