Conservar objetos personalizados

No tiene que implementar la persistencia personalizada para los objetos personalizados que crea con tal de que sus propiedades utilicen solo tipos de datos simples como integer y string. La implementación predeterminada de persistencia guarda los metadatos del objeto junto con los valores de todas sus propiedades.

Sin embargo, si el objeto tiene propiedades que usan tipos de datos complejos o si desea realizar el procesamiento personalizado en valores de propiedad cuando se cargan y guardan, puede implementar la interfaz IDTSComponentPersist y los métodos SaveToXML y LoadFromXML. En estos métodos puede cargar desde (o guardar en) la definición XML del paquete un fragmento XML que contiene las propiedades del objeto y sus valores actuales. No está definido el formato de este fragmento XML; solo debe tener un formato XML correcto.

Nota importanteImportante

Al implementar la persistencia personalizada, debe conservar todas las propiedades del objeto, incluidas las propiedades heredadas y las personalizadas que ha agregado.

Ejemplo

Aunque en el ejemplo de administrador de conexiones personalizado de Sql Server de Codeplex no se requiere la persistencia personalizada para sus tres propiedades de tipo string, el código siguiente muestra un ejemplo del código personalizado que se requeriría para conservar el administrador de conexiones y sus propiedades. La clase que contiene este código debe implementar la interfaz IDTSComponentPersist.

  Private Const PERSIST_ELEMENT As String = "SqlConnectionManager"
  Private Const PERSIST_SERVER As String = "Server"
  Private Const PERSIST_DATABASE As String = "Database"
  Private Const PERSIST_CONNECTIONSTRING As String = "ConnectionString"

  Public Sub LoadFromXML(ByVal node As System.Xml.XmlElement, _
    ByVal infoEvents As Microsoft.SqlServer.Dts.Runtime.IDTSInfoEvents) _
    Implements Microsoft.SqlServer.Dts.Runtime.IDTSComponentPersist.LoadFromXML

    Dim propertyNode As XmlNode

    ' Make sure that the correct node is being loaded.
    If node.Name <> PERSIST_ELEMENT Then
      Throw New Exception("Persisted element is not of type " & PERSIST_ELEMENT)
    End If

    ' Load the three properties of the object from XML into variables.
    For Each propertyNode In node.ChildNodes
      Select Case propertyNode.Name
        Case PERSIST_SERVER
          _serverName = propertyNode.InnerText
        Case PERSIST_DATABASE
          _databaseName = propertyNode.InnerText
        Case PERSIST_CONNECTIONSTRING
          _connectionString = propertyNode.InnerText
      End Select
    Next

  End Sub

  Public Sub SaveToXML(ByVal doc As System.Xml.XmlDocument, _
    ByVal infoEvents As Microsoft.SqlServer.Dts.Runtime.IDTSInfoEvents) _
    Implements Microsoft.SqlServer.Dts.Runtime.IDTSComponentPersist.SaveToXML

    Dim elementRoot As XmlElement
    Dim propertyNode As XmlNode

    ' Create a new node to persist the object and its properties.
    elementRoot = doc.CreateElement(String.Empty, PERSIST_ELEMENT, String.Empty)

    ' Save the three properties of the object from variables into XML.
    propertyNode = doc.CreateNode(XmlNodeType.Element, PERSIST_SERVER, String.Empty)
    propertyNode.InnerText = _serverName
    elementRoot.AppendChild(propertyNode)

    propertyNode = doc.CreateNode(XmlNodeType.Element, PERSIST_DATABASE, String.Empty)
    propertyNode.InnerText = _databaseName
    elementRoot.AppendChild(propertyNode)

    propertyNode = doc.CreateNode(XmlNodeType.Element, PERSIST_CONNECTIONSTRING, String.Empty)
    propertyNode.InnerText = _connectionString
    elementRoot.AppendChild(propertyNode)

    doc.AppendChild(elementRoot)

  End Sub
    private const string PERSIST_ELEMENT = "SqlConnectionManager";
    private const string PERSIST_SERVER = "Server";
    private const string PERSIST_DATABASE = "Database";
    private const string PERSIST_CONNECTIONSTRING = "ConnectionString";

    public void LoadFromXML(System.Xml.XmlElement node,
      Microsoft.SqlServer.Dts.Runtime.IDTSInfoEvents infoEvents)
    {

      // Make sure that the correct node is being loaded.
      if (node.Name != PERSIST_ELEMENT)
      {
        throw new Exception("Persisted element is not of type " + PERSIST_ELEMENT);
      }

      // Save the three properties of the object from variables into XML.
      foreach (XmlNode propertyNode in node.ChildNodes)
      {
        switch (propertyNode.Name)
        {
          case PERSIST_SERVER:
            _serverName = propertyNode.InnerText;
            break;
          case PERSIST_DATABASE:
            _databaseName = propertyNode.InnerText;
            break;
          case PERSIST_CONNECTIONSTRING:
            _connectionString = propertyNode.InnerText;
            break;
        }
      }

    }

    public void SaveToXML(System.Xml.XmlDocument doc,
      Microsoft.SqlServer.Dts.Runtime.IDTSInfoEvents infoEvents)
    {

      XmlElement elementRoot;
      XmlNode propertyNode;

      // Create a new node to persist the object and its properties.
      elementRoot = doc.CreateElement(String.Empty, PERSIST_ELEMENT, String.Empty);

      // Save the three properties of the object from variables into XML.
      propertyNode = doc.CreateNode(XmlNodeType.Element, PERSIST_SERVER, String.Empty);
      propertyNode.InnerText = _serverName;
      elementRoot.AppendChild(propertyNode);

      propertyNode = doc.CreateNode(XmlNodeType.Element, PERSIST_DATABASE, String.Empty);
      propertyNode.InnerText = _databaseName;
      elementRoot.AppendChild(propertyNode);

      propertyNode = doc.CreateNode(XmlNodeType.Element, PERSIST_CONNECTIONSTRING, String.Empty);
      propertyNode.InnerText = _connectionString;
      elementRoot.AppendChild(propertyNode);

      doc.AppendChild(elementRoot);

    }
Icono de Integration Services (pequeño) Manténgase al día con Integration Services

Para obtener las más recientes descargas, artículos, ejemplos y vídeos de Microsoft, así como soluciones seleccionadas de la comunidad, visite la página de Integration Services en MSDN o TechNet:

Para recibir notificaciones automáticas de estas actualizaciones, suscríbase a las fuentes RSS disponibles en la página.