在C#Console应用程序中,我创建了一个新的SSIS项目(.ispac文件)。但是,当我尝试从现有SSIS项目复制包并将其添加到新项目时,会引发错误(请参见下图)。错误表示已添加包但新项目完全为空。必须说它已被添加到项目中,我不允许将其添加到另一个项目中。
如何复制现有的SSIS包并将其添加到新的SSIS项目?
// Get existing project.
var ssisProject = Project.OpenProject(@"C:\Packages\CommonPackageTest.ispac");
// Get existing package.
Package package = ssisProject.PackageItems["CommonPackageTest.dtsx"].Package;
// Create new project.
Project newProject = Project.CreateProject();
// Copy the original package.
Package newPackage = package;
// Attempt to add the new package to the new project
newProject.PackageItems.Add(newPackage, "NewPackage.dtsx");
答案 0 :(得分:1)
Navig8tr,
这是我观察到的。从Project获取现有包时,该包对象具有对Microsoft.SqlServer.Dts.Runtime.Interop.ProjectInterop对象的引用。您可以通过获取package.Project的值来检查这一点。如果此属性的值不为null,但具有对Project的引用,则无法将该包添加到另一个项目。这就是为什么你看到你看到的错误。
要解决此问题,请将原始软件包保存到磁盘,然后从磁盘加载该软件包并将其添加到新项目中。代码如下:
// Get existing project.
var ssisProject = Project.OpenProject(@"C:\Users\Administrator\Documents\visual studio 2015\projects\Integration Services Project9\Integration Services Project9\bin\Development\Integration Services Project9.ispac");
// Get existing package.
Package package = ssisProject.PackageItems["Package.dtsx"].Package;
// Check the value of Project property of Package.
// You will notice that Project is not equal to null; which causes problems if you try to add this to another project.
if (package.Project != null) {
Console.WriteLine("Original-Package has Project ref: " + package.Project);
}
else {
Console.WriteLine("Original-Package Project ref IS NULL");
}
// Create new project.
Project newProject = Project.CreateProject();
// Copy the original package. (your original code)
Package newPackage = package;
// Check the value of Project property of this Package.
// You will notice that Project is not equal to null; which causes problems if you try to add this to another project.
if (newPackage.Project != null) {
Console.WriteLine("Direct-Copy-Package has Project ref: " + newPackage.Project);
}
else {
Console.WriteLine("Direct-Copy-Package Project ref IS NULL");
}
// Let us try to serializ and deserialize using Application class.
// Instantiate an Application object. We will use this to serde the original package.
Application application = new Application();
// Save/Seriliaze the original package to disk
application.SaveToXml(@"C:\Temp\PackageToCopy.dtsx", package, null);
// Load the package back
Package serdedPackage = application.LoadPackage(@"C:\Temp\PackageToCopy.dtsx", null);
// Check the value of Project property of this Package.
// You will notice that Project is equal to null; which means you can add this to another project.
if (serdedPackage.Project != null) {
Console.WriteLine("[Before adding to Project] Serded-Package has Project ref: " + serdedPackage.Project);
}
else {
Console.WriteLine("[Before adding to Project] Serded-Package Project ref IS NULL");
}
// Add our serded package to newProject.
newProject.PackageItems.Add(serdedPackage, "NewPackage.dtsx");
// Now Check the value of Project property of this Serded Package Again
// You will notice that Project is NOT equal to null again.
if (serdedPackage.Project != null) {
Console.WriteLine("[After adding to Project] Serded-Package has Project ref: " + serdedPackage.Project);
}
else {
Console.WriteLine("[After adding to Project] Serded-Package Project ref IS NULL");
}
// FInally Save our new project to disk
newProject.SaveTo(@"C:\Temp\Copy.ispac");
Console.WriteLine("Done");