Share via


開發自訂連接管理員的使用者介面

在您覆寫基底類別的屬性和方法的實作以提供自訂功能後,可能會想為連接管理員建立自訂使用者介面。如果您不建立自訂使用者介面,使用者只能透過使用 [屬性] 視窗設定您的連接管理員。

在自訂使用者介面專案或是組件中,通常有兩個類別:實作 IDtsConnectionManagerUI 的類別,以及它所顯示的 Windows Form,以蒐集使用者資訊。

重要事項重要事項

在簽署和建立自訂使用者介面,並且安裝在全域組件快取之後 (如<Coding a Custom Connection Manager>中所述),請記得在 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;

    }

建立使用者介面的新執行個體

當使用者建立連接管理員的新執行個體時,會在 Initialize 方法之後呼叫 New 方法 (不是建構函式)。在 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 Form,讓使用者能夠在其中設定連接管理員的屬性。

初始化使用者介面表單

當您顯示供編輯之用的自訂表單時,可以傳遞編輯中連接管理員的參考。您可以使用表單類別的自訂建構函式,或是建立自己的 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 摘要。