共用方式為


升級資料流程元件的版本

使用舊版元件建立的封裝可能包含不再有效的中繼資料,例如在新版元件中已修改其使用方式的自訂屬性。您可以覆寫 PipelineComponent 基底類別的 PerformUpgrade 方法,以更新之前儲存在舊封裝的中繼資料,從而反映元件的目前屬性。

[!附註]

當您為新版本的 Integration Services 重新編譯自訂元件時,如果未變更元件的屬性,就不必變更 DtsPipelineComponentAttribute..::..CurrentVersion 屬性的值。

範例

下列範例包含 2.0 版的虛構資料流程元件之程式碼。新版本號碼是定義在 DtsPipelineComponentAttributeCurrentVersion 屬性中。該元件具有一個屬性,它定義如何處理超過臨界值的數值。在 1.0 版的虛構元件中,這個屬性名為 RaiseErrorOnInvalidValue 並接受 True 或 False 的布林值。在 2.0 版的虛構元件中,該屬性重新命名為 InvalidValueHandling 並接受自訂列舉的四個可能值之一。

下列範例中覆寫的 PerformUpgrade 方法會執行下列動作:

  • 取得元件的目前版本。

  • 取得舊的自訂屬性值。

  • 移除自訂屬性集合中的舊屬性。

  • 若有可能的話,根據舊屬性值設定新自訂屬性值。

  • 將版本中繼資料設定成元件的目前版本。

[!附註]

資料流程引擎會在 pipelineVersion 參數中將自己的版本號碼傳遞至 PerformUpgrade 方法。此參數在 1.0 版的 Integration Services 中沒有用處,但是在後續版本中可能會變成有用。

範例程式碼只會使用兩個列舉值,以直接對應至自訂屬性之前的布林值。使用者可以透過元件的自訂使用者介面、在 [進階編輯器] 中或是以程式設計方式,選取其他可用的列舉值。如需有關在 [進階編輯器] 中顯示自訂屬性之列舉值的詳細資訊,請參閱<資料流程元件的設計階段方法>中的<建立自訂屬性>。

Imports Microsoft.SqlServer.Dts.Pipeline
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper

<DtsPipelineComponent(ComponentType:=ComponentType.Transform, CurrentVersion:=2)> _
Public Class PerformUpgrade
  Inherits PipelineComponent

  ' Define the set of possible values for the new custom property.
  Private Enum InvalidValueHandling
    Ignore
    FireInformation
    FireWarning
    FireError
  End Enum

  Public Overloads Overrides Sub PerformUpgrade(ByVal pipelineVersion As Integer)

    ' Obtain the current component version from the attribute.
    Dim componentAttribute As DtsPipelineComponentAttribute = _
      CType(Attribute.GetCustomAttribute(Me.GetType, _
      GetType(DtsPipelineComponentAttribute), False), _
      DtsPipelineComponentAttribute)
    Dim currentVersion As Integer = componentAttribute.CurrentVersion

    ' If the component version saved in the package is less than
    '  the current version, Version 2, perform the upgrade.
    If ComponentMetaData.Version < currentVersion Then

      ' Get the current value of the old custom property, RaiseErrorOnInvalidValue, 
      ' and then remove the property from the custom property collection.
      Dim oldValue As Boolean = False
      Try
        Dim oldProperty As IDTSCustomProperty100 = _
          ComponentMetaData.CustomPropertyCollection("RaiseErrorOnInvalidValue")
        oldValue = CType(oldProperty.Value, Boolean)
        ComponentMetaData.CustomPropertyCollection.RemoveObjectByIndex("RaiseErrorOnInvalidValue")
      Catch ex As Exception
        ' If the old custom property is not available, ignore the error.
      End Try

      ' Set the value of the new custom property, InvalidValueHandling,
      '  by using the appropriate enumeration value.
      Dim newProperty As IDTSCustomProperty100 = _
        ComponentMetaData.CustomPropertyCollection("InvalidValueHandling")
      If oldValue = True Then
        newProperty.Value = InvalidValueHandling.FireError
      Else
        newProperty.Value = InvalidValueHandling.Ignore
      End If

    End If

    ' Update the saved component version metadata to the current version.
    ComponentMetaData.Version = currentVersion

  End Sub

End Class
using System;
using Microsoft.SqlServer.Dts.Pipeline;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;

[DtsPipelineComponent(ComponentType = ComponentType.Transform, CurrentVersion = 2)]
public class PerformUpgradeCS :
  PipelineComponent

  // Define the set of possible values for the new custom property.
{
  private enum InvalidValueHandling
  {
    Ignore,
    FireInformation,
    FireWarning,
    FireError
  };

  public override void PerformUpgrade(int pipelineVersion)
  {

    // Obtain the current component version from the attribute.
    DtsPipelineComponentAttribute componentAttribute = 
      (DtsPipelineComponentAttribute)Attribute.GetCustomAttribute(this.GetType(), typeof(DtsPipelineComponentAttribute), false);
    int currentVersion = componentAttribute.CurrentVersion;

    // If the component version saved in the package is less than
    //  the current version, Version 2, perform the upgrade.
    if (ComponentMetaData.Version < currentVersion)

    // Get the current value of the old custom property, RaiseErrorOnInvalidValue, 
    // and then remove the property from the custom property collection.
    {
      bool oldValue = false;
      try
      {
        IDTSCustomProperty100 oldProperty = 
          ComponentMetaData.CustomPropertyCollection["RaiseErrorOnInvalidValue"];
        oldValue = (bool)oldProperty.Value;
        ComponentMetaData.CustomPropertyCollection.RemoveObjectByIndex("RaiseErrorOnInvalidValue");
      }
      catch (Exception ex)
      {
        // If the old custom property is not available, ignore the error.
      }

      // Set the value of the new custom property, InvalidValueHandling,
      //  by using the appropriate enumeration value.
      IDTSCustomProperty100 newProperty = 
         ComponentMetaData.CustomPropertyCollection["InvalidValueHandling"];
      if (oldValue == true)
      {
        newProperty.Value = InvalidValueHandling.FireError;
      }
      else
      {
        newProperty.Value = InvalidValueHandling.Ignore;
      }

    }

    // Update the saved component version metadata to the current version.
    ComponentMetaData.Version = currentVersion;

  }

}
Integration Services 圖示 (小) 掌握 Integration Services 的最新狀態

若要取得 Microsoft 的最新下載、文件、範例和影片以及社群中的選定解決方案,請瀏覽 MSDN 或 TechNet 上的 Integration Services 頁面:

若要得到這些更新的自動通知,請訂閱該頁面上所提供的 RSS 摘要。