Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法通过编程方式将新包添加到新项目中_C#_Ssis - Fatal编程技术网

C# 无法通过编程方式将新包添加到新项目中

C# 无法通过编程方式将新包添加到新项目中,c#,ssis,C#,Ssis,在一个C#控制台应用程序中,我创建了一个新的SSIS项目(.ispac文件)。但是,当我尝试从现有SSIS项目复制包并将其添加到新项目时,会抛出一个错误(请参见下图)。错误表示已添加包,但新项目完全为空。它必须说,它已经被添加到一个项目,我不允许添加到另一个项目 如何复制现有SSIS包并将其添加到新的SSIS项目 // Get existing project. var ssisProject = Project.OpenProject(@"C:\Pack

在一个C#控制台应用程序中,我创建了一个新的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");
Navig8tr

以下是我观察到的情况。从项目获取现有包时,该包对象将引用Microsoft.SqlServer.Dts.Runtime.Interop.ProjectInterop对象。您可以通过获取package.Project的值来检查这一点。如果此属性的值不为null,但引用了某个项目,则无法将包添加到另一个项目中。这就是为什么你看到了你所看到的错误

要解决此问题,请将原始包保存到磁盘,然后从磁盘加载该包并将其添加到新项目中。代码如下:

    // 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");