Persisting Custom Objects
Nie trzeba implementować niestandardowe trwałość niestandardowych obiektów, które utworzyć tak długo, jak ich właściwości za pomocą takich jak typy danych tylko proste integer i string. Wykonanie domyślnej trwałość zapisuje metadane dla obiektu wraz z wartościami z jego właściwości.
Niemniej jednak, jeśli obiekt ma właściwość, które używają złożone typy danych lub jeśli chcesz przeprowadzić przetwarzania niestandardowych na wartości właściwość, jak są ładowane i zapisywane, można zaimplementować IDTSComponentPersist interfejs i jego LoadFromXML(XmlElement, IDTSInfoEvents) i SaveToXML(XmlDocument, IDTSInfoEvents) metody. W tych metodach jest ładowany z (lub zapisać) definicji XML pakiet fragmentu XML, który zawiera właściwości obiektu i ich bieżące wartości.Format tego fragmentu XML nie jest zdefiniowany; tylko musi być poprawnie sformułowany kod XML.
Important Note: |
---|
Podczas implementowania trwałość niestandardowy musi utrzymują się wszystkie właściwości obiektu, w tym zarówno właściwości odziedziczonych, jak i właściwości niestandardowe, które zostały dodane. |
Przykład
Mimo że Sql Server Custom Connection Manager Sample nie wymaga trwałość niestandardowe dla swoich trzech właściwości typu string, w poniższym kodzie pokazano przykład kodu niestandardowego, wymaganego do menedżer połączeń i jego właściwości będą się powtarzać. Klasa, która zawiera ten kod musi implementować IDTSComponentPersist interfejs.
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);
}
|