保存自訂物件
您不需要為所建立的自定義物件實作自定義持續性,只要其屬性只使用 和 等integer
string
簡單數據類型即可。 預設的持續性實作可以儲存物件的中繼資料,及其所有屬性值。
不過,如果您的物件具有使用複雜資料類型的屬性,或是如果您想要在載入和儲存屬性值時,在它們上面執行自訂處理,可以實作 IDTSComponentPersist 介面及其 LoadFromXML 與 SaveToXML 方法。 在這些方法中,您從封裝的 XML 定義載入 XML 片段 (或是將它儲存到封裝的 XML 定義),而 XML 片段包含物件的屬性以及其目前的值。 並未定義這個 XML 片段的格式,它必須只能是格式正確的 XML。
重要
當您實作自訂持續性時,必須保存物件的所有屬性,包括繼承的屬性和您加入的自訂屬性。
範例
雖然 Sql Server 自定義 連線管理員 範例不需要自定義持續性的三個類型string
屬性,下列程式代碼會顯示保存連接管理員及其屬性所需的自定義程式碼範例。 包含此程式碼的類別必須實作 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);
}
使用 Integration Services 保持最新狀態
如需來自Microsoft的最新下載、文章、範例和影片,以及來自社群的所選解決方案,請流覽 MSDN 上的 Integration Services 頁面:
流覽 MSDN 上的 Integration Services 頁面
如需這些更新的自動通知,請訂閱頁面上可用的 RSS 摘要。