为自定义连接管理器开发用户界面

重写基类的属性和方法的实现以提供自定义功能后,您可能需要为连接管理器创建自定义用户界面。如果未创建自定义用户界面,用户只能使用“属性”窗口配置连接管理器。

在自定义用户界面项目或程序集中,通常有两个类,实现 IDtsConnectionManagerUI 的类以及为收集用户信息而显示的 Windows 窗体。

重要说明重要提示

在按照编写自定义连接管理器代码中的说明生成自定义用户界面并签名而且在全局程序集缓存中安装后,还要在 DtsConnectionAttributeUITypeName 属性中提供此类的完全限定名称。

有关自定义连接管理器的工作示例,请参阅 Codeplex 上的 Integration Services 示例。本主题中演示的代码示例来自 SQL Server 自定义连接管理器示例。

注意注意

内置于 Integration Services 的大多数任务、源和目标都只能与特定类型的内置连接管理器一起工作。因此,不能使用内置任务和组件测试这些示例。

编写用户界面类代码

IDtsConnectionManagerUI 接口有四个方法:InitializeNewEditDelete。下面各部分将介绍这四个方法。

注意注意

如果在用户删除连接管理器实例时不需要执行清除操作,则可能不需要为 Delete 方法编写任何代码。

初始化用户界面

Initialize 方法中,设计器将提供对正在配置的连接管理器的引用,以便用户界面类可以修改连接管理器的属性。如下面的代码所示,您的代码需要缓存对连接管理器的引用,以便稍后使用。

Public Sub Initialize(ByVal connectionManager As Microsoft.SqlServer.Dts.Runtime.ConnectionManager, ByVal serviceProvider As System.IServiceProvider) Implements Microsoft.SqlServer.Dts.Runtime.Design.IDtsConnectionManagerUI.Initialize

    _connectionManager = connectionManager
    _serviceProvider = serviceProvider

  End Sub
    public void Initialize(Microsoft.SqlServer.Dts.Runtime.ConnectionManager connectionManager, System.IServiceProvider serviceProvider)
    {

      _connectionManager = connectionManager;
      _serviceProvider = serviceProvider;

    }

创建新的用户界面实例

当用户创建新的连接管理器实例时,非构造函数 New 方法在调用 Initialize 方法之后调用。在 New 方法中,通常要显示用于编辑的窗体,除非用户已复制并粘贴了现有连接管理器。下面的代码演示了此方法的实现。

  Public Function [New](ByVal parentWindow As System.Windows.Forms.IWin32Window, ByVal connections As Microsoft.SqlServer.Dts.Runtime.Connections, ByVal connectionUIArgs As Microsoft.SqlServer.Dts.Runtime.Design.ConnectionManagerUIArgs) As Boolean Implements Microsoft.SqlServer.Dts.Runtime.Design.IDtsConnectionManagerUI.New

    Dim clipboardService As IDtsClipboardService

    clipboardService = _
      DirectCast(_serviceProvider.GetService(GetType(IDtsClipboardService)), IDtsClipboardService)
    If Not clipboardService Is Nothing Then
      ' If the connection manager has been copied and pasted, take no action.
      If clipboardService.IsPasteActive Then
        Return True
      End If
    End If

    Return EditSqlConnection(parentWindow)

  End Function
  public bool New(System.Windows.Forms.IWin32Window parentWindow, Microsoft.SqlServer.Dts.Runtime.Connections connections, Microsoft.SqlServer.Dts.Runtime.Design.ConnectionManagerUIArgs connectionUIArgs)
    {
      IDtsClipboardService clipboardService;

      clipboardService = (IDtsClipboardService)_serviceProvider.GetService(typeof(IDtsClipboardService));
      if (clipboardService != null)
      // If connection manager has been copied and pasted, take no action.
      {
        if (clipboardService.IsPasteActive)
        {
          return true;
        }
      }

      return EditSqlConnection(parentWindow);
    }

编辑连接管理器

由于用于编辑的窗体是从 NewEdit 方法调用的,因此可以方便地使用 Helper 函数来封装用于显示该窗体的代码。下面的代码演示了此 Helper 函数的实现。

  Private Function EditSqlConnection(ByVal parentWindow As IWin32Window) As Boolean

    Dim sqlCMUIForm As New SqlConnMgrUIFormVB

    sqlCMUIForm.Initialize(_connectionManager, _serviceProvider)
    If sqlCMUIForm.ShowDialog(parentWindow) = DialogResult.OK Then
      Return True
    Else
      Return False
    End If

  End Function
   private bool EditSqlConnection(IWin32Window parentWindow)
    {

      SqlConnMgrUIFormCS sqlCMUIForm = new SqlConnMgrUIFormCS();

      sqlCMUIForm.Initialize(_connectionManager, _serviceProvider);
      if (sqlCMUIForm.ShowDialog(parentWindow) == DialogResult.OK)
      {
        return true;
      }
      else
      {
        return false;
      }

    }

Edit 方法中,您只需要显示用于编辑的窗体。下面的代码演示了 Edit 方法的实现,该方法使用 Helper 函数来封装窗体的代码。

  Public Function Edit(ByVal parentWindow As System.Windows.Forms.IWin32Window, ByVal connections As Microsoft.SqlServer.Dts.Runtime.Connections, ByVal connectionUIArg As Microsoft.SqlServer.Dts.Runtime.Design.ConnectionManagerUIArgs) As Boolean Implements Microsoft.SqlServer.Dts.Runtime.Design.IDtsConnectionManagerUI.Edit

    Return EditSqlConnection(parentWindow)

  End Function
    public bool Edit(System.Windows.Forms.IWin32Window parentWindow, Microsoft.SqlServer.Dts.Runtime.Connections connections, Microsoft.SqlServer.Dts.Runtime.Design.ConnectionManagerUIArgs connectionUIArg)
    {

      return EditSqlConnection(parentWindow);

    }

编写用户界面窗体代码

在创建用于实现 IDtsConnectionManagerUI 接口的方法的用户界面类后,必须创建用户可在其中配置连接管理器的属性的 Windows 窗体。

初始化用户界面窗体

显示用于编辑的自定义窗体后,可以传递对正在编辑的连接管理器的引用。可以使用窗体类的自定义构造函数传递此引用,也可以按如下所示创建自己的 Initialize 方法来传递此引用。

  Public Sub Initialize(ByVal connectionManager As ConnectionManager, ByVal serviceProvider As IServiceProvider)

    _connectionManager = connectionManager
    _serviceProvider = serviceProvider
    ConfigureControlsFromConnectionManager()
    EnableControls()

  End Sub
   public void Initialize(ConnectionManager connectionManager, IServiceProvider serviceProvider)
    {

      _connectionManager = connectionManager;
      _serviceProvider = serviceProvider;
      ConfigureControlsFromConnectionManager();
      EnableControls();

    }

设置用户界面窗体的属性

最后,如果首次加载窗体类时使用的是连接管理器属性的现有值或默认值,则该窗体类需要一个 Helper 函数,用于填充窗体上的控件。您的窗体类还需要一个类似这样的函数:当用户单击“确定”并关闭窗体时,该函数可将属性的值设置为用户输入的值。

  Private Const CONNECTIONNAME_BASE As String = "SqlConnectionManager"

  Private Sub ConfigureControlsFromConnectionManager()

    Dim tempName As String
    Dim tempServerName As String
    Dim tempDatabaseName As String

    With _connectionManager

      tempName = .Properties("Name").GetValue(_connectionManager).ToString
      If Not String.IsNullOrEmpty(tempName) Then
        _connectionName = tempName
      Else
        _connectionName = CONNECTIONNAME_BASE
      End If

      tempServerName = .Properties("ServerName").GetValue(_connectionManager).ToString
      If Not String.IsNullOrEmpty(tempServerName) Then
        _serverName = tempServerName
        txtServerName.Text = _serverName
      End If

      tempDatabaseName = .Properties("DatabaseName").GetValue(_connectionManager).ToString
      If Not String.IsNullOrEmpty(tempDatabaseName) Then
        _databaseName = tempDatabaseName
        txtDatabaseName.Text = _databaseName
      End If

    End With

  End Sub

  Private Sub ConfigureConnectionManagerFromControls()

    With _connectionManager
      .Properties("Name").SetValue(_connectionManager, _connectionName)
      .Properties("ServerName").SetValue(_connectionManager, _serverName)
      .Properties("DatabaseName").SetValue(_connectionManager, _databaseName)
    End With

  End Sub
   private const string CONNECTIONNAME_BASE = "SqlConnectionManager";

   private void ConfigureControlsFromConnectionManager()
    {

      string tempName;
      string tempServerName;
      string tempDatabaseName;

      {
        tempName = _connectionManager.Properties["Name"].GetValue(_connectionManager).ToString();
        if (!String.IsNullOrEmpty(tempName))
        {
          _connectionName = tempName;
        }
        else
        {
          _connectionName = CONNECTIONNAME_BASE;
        }

        tempServerName = _connectionManager.Properties["ServerName"].GetValue(_connectionManager).ToString();
        if (!String.IsNullOrEmpty(tempServerName))
        {
          _serverName = tempServerName;
          txtServerName.Text = _serverName;
        }

        tempDatabaseName = _connectionManager.Properties["DatabaseName"].GetValue(_connectionManager).ToString();
        if (!String.IsNullOrEmpty(tempDatabaseName))
        {
          _databaseName = tempDatabaseName;
          txtDatabaseName.Text = _databaseName;
        }

      }

    }

    private void ConfigureConnectionManagerFromControls()
    {

      {
        _connectionManager.Properties["Name"].SetValue(_connectionManager, _connectionName);
        _connectionManager.Properties["ServerName"].SetValue(_connectionManager, _serverName);
        _connectionManager.Properties["DatabaseName"].SetValue(_connectionManager, _databaseName);
      }

    }
Integration Services 图标(小) 使 Integration Services 保持最新

若要从 Microsoft 获得最新的下载内容、文章、示例和视频,以及从社区获得所选解决方案,请访问 MSDN 或 TechNet 上的 Integration Services 页:

若要获得有关这些更新的自动通知,请订阅该页上提供的 RSS 源。