使自定义对象持久化
适用范围:SQL Server Azure 数据工厂中的 SSIS Integration Runtime
只要所创建的自定义对象的属性仅使用简单数据类型(如整数和字符串),就不需要为其实现自定义持久性。 持久性的默认实现可保存对象的元数据及其所有属性的值。
但是,如果您的对象具有使用复杂数据类型的属性,或您希望在加载和保存属性值时对其执行自定义处理,则可以实现 IDTSComponentPersist 接口及其 LoadFromXML 和 SaveToXML 方法。 在这些方法中,可以从包的 XML 定义加载包含这些对象属性及其当前值的 XML 片段,或将这些片段保存到包的 XML 定义中。 此 XML 片段的格式未定义;只要求必须是格式正确的 XML。
重要
实现自定义持久性时,必须使对象的所有属性持久化,包括继承的属性和添加的自定义属性。
示例
尽管 SQL Server 自定义连接管理器示例不要求对其字符串类型的三个属性实现自定义持久性,但以下代码演示了使连接管理器及其属性持久化所需的自定义代码的示例。 包含此代码的类必须实现 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);
}