【问题标题】:Loading SSIS package and it's connection managers in a WPF application在 WPF 应用程序中加载 SSIS 包及其连接管理器
【发布时间】:2016-10-07 15:55:38
【问题描述】:

我创建了一个包含三个连接管理器(在项目级别定义)的集成服务项目,现在我有一个 SSIS 包(.dtsx 文件),我想在我的应用程序中使用它。 所以我使用 Microsoft.SqlServer.Dts 命名空间如下:

string pkgLocation;
Package pkg;
Microsoft.SqlServer.Dts.Runtime.Application app;
DTSExecResult pkgResults;
pkgLocation = @"D:\My Job\Tadbirgaran\Integration Services Project\MyPackage.dtsx";
app = new Microsoft.SqlServer.Dts.Runtime.Application();
pkg = app.LoadPackage(pkgLocation, null);
pkgResults = pkg.Execute();

但是这段代码显然没有加载项目连接管理器,那么有什么方法可以以编程方式加载连接管理器文件并将它们添加到包连接属性中?还是应该在集成服务项目的包中定义连接?

【问题讨论】:

    标签: wpf ssis


    【解决方案1】:

    您可以使用配置以编程方式控制连接属性。这是一个例子。

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.SqlServer.Dts.Runtime;
    using Microsoft.SqlServer.Dts.Tasks.BulkInsertTask;
    
    namespace configuration_API
    {
        class Program
        {    
            static void Main(string[] args)
            {
            // Create a package and set two properties.
                Package pkg = new Package();
                pkg.EnableConfigurations = true;
                pkg.ExportConfigurationFile(@"C:\conf.xml");
    
                // Create a variable object and add it to the 
                // package Variables collection.
                Variable varPkg = pkg.Variables.Add("var", false, "", 100);
                varPkg.Value = 1;
                string packagePathToVariable = varPkg.GetPackagePath();
    
                // Create a configuration object and add it to the 
               // package configuration collection.
                Configuration config = pkg.Configurations.Add();
    
               // Set properties on the configuration object.
                config.ConfigurationString = "conf.xml";
                config.Description = "My configuration description";
                config.ConfigurationType = DTSConfigurationType.ConfigFile;
                config.PackagePath = packagePathToVariable;
    
                // Save the package and its configuration.
                Application app = new Application();
                app.SaveToXml(@"c:\pkg.xml", pkg, null);
    
                // Reload the package.
                Package p1 = app.LoadPackage(@"c:\pkg.xml", null);
                // Review the configuration information.
                Configurations configs_After = pkg.Configurations;
                foreach(Configuration confAfter in configs_After)
                {
                    Console.WriteLine("ConfigurationString is {0}", confAfter.ConfigurationString);
                    Console.WriteLine("ConfigurationType is {0}", confAfter.ConfigurationType);
                    Console.WriteLine("CreationName is {0}", confAfter.CreationName);
                    Console.WriteLine("Description is {0}", confAfter.Description);
                    Console.WriteLine("Assigned ID is {0}", confAfter.ID);
                    Console.WriteLine("Name is {0}", confAfter.Name);
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      我最终加载了项目而不是包,并设置了从项目到包的连接,如下所示:

              private void SSISLoadData()
              {
                  Project ssisProject = null;
                  DTSExecResult pkgResults;
                  try
                  {
                      ssisProject = Project.OpenProject(@"D:\My Job\TDB\Integration Services Project\bin\Development\Integration Services Project.ispac");
                      Package pkg = ssisProject.PackageItems[0].LoadPackage(null);
                      for (int i = 0; i < ssisProject.ConnectionManagerItems.Count; i++)
                          pkg.Connections.Join(ssisProject.ConnectionManagerItems[i].ConnectionManager);
                      pkg.Connections[0].ConnectionString = dataFolderPath + "\\*.csv";
                      pkg.Connections[1].ConnectionString = string.Format("Data Source =.; Initial Catalog = TDB; Provider = SQLNCLI11.1; Integrated Security = SSPI; Initial File Name = {0};", dbPath);
                      pkg.Connections[2].ConnectionString = string.Format("Data Source =.; Initial Catalog = TDBRawData; Provider = SQLNCLI11.1; Integrated Security = SSPI; Initial File Name = {0}\\TDBRawData.mdf;", Environment.CurrentDirectory);
                      pkgResults = pkg.Execute();
                      MessageBox.Show(pkgResults.ToString());
                  }
                  catch (Exception e)
                  {
                      MessageBox.Show(e.Message);
                  }
                  finally
                  {
                      if (ssisProject != null)
                          ssisProject.Dispose();
                  }
              }
      

      我不知道这个问题是否有更好的解决方案,但它确实有效。

      【讨论】:

        猜你喜欢
        • 2011-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-25
        • 1970-01-01
        相关资源
        最近更新 更多