如何:定義合併資料表發行項之間的邏輯記錄關聯性 (RMO 程式設計)
[!附註]
未來的 Microsoft SQL Server 版本將移除這項功能。請避免在新的開發工作中使用這項功能,並規劃修改目前使用這項功能的應用程式。
合併式複寫可讓您在不同資料表內定義相關資料列之間的關聯性。然後這些資料列在同步處理期間,可以當做交易式單位來處理。可以在兩個發行項之間定義邏輯記錄,不論這些發行項是否有聯結篩選關聯性。如需詳細資訊,請參閱<使用邏輯記錄分組相關資料列的變更>。
您可以使用複寫管理物件 (RMO),以程式設計方式指定發行項之間的邏輯記錄關聯性。
[!附註]
合併式複寫可允許您指定在邏輯記錄層級追蹤及解決衝突,但這些選項無法使用 RMO 來設定。如需有關使用複寫預存程序設定這些選項的詳細資訊,請參閱<如何:定義合併資料表發行項之間的邏輯記錄關聯性 (複寫 Transact-SQL 程式設計)>。
定義沒有相關聯結篩選的邏輯記錄關聯性
使用 ServerConnection 類別建立與發行者的連接。
建立 MergePublication 類別的執行個體、為發行集設定 Name 和 DatabaseName 屬性,以及將 ConnectionContext 屬性設定為步驟 1 中所建立的連接。
呼叫 LoadProperties 方法以取得物件的屬性。如果此方法傳回 false,則表示步驟 2 中的發行集屬性定義不正確,或者該發行集不存在。
如果 PartitionGroupsOption 屬性設定為 False,請將它設定為 True。
如果組成此邏輯記錄的發行項不存在,請建立 MergeArticle 類別的執行個體,並設定以下屬性:
將 Name 設定為發行項名稱。
將 PublicationName 設定為發行集名稱。
(選擇性) 如果以水平方式篩選此發行項,請為 FilterClause 屬性指定資料列篩選子句。使用此屬性可指定靜態或參數化資料列篩選器。如需詳細資訊,請參閱<參數化資料列篩選器>。
如需詳細資訊,請參閱<如何:定義發行項 (RMO 程式設計)>。
呼叫 Create 方法。
針對組成此邏輯記錄的每一個發行項重複執行步驟 5 和 6。
建立 MergeJoinFilter 類別的執行個體,以定義發行項之間的邏輯記錄關聯性。然後,設定下列屬性:
將 ArticleName 屬性設定為邏輯記錄關聯性中的子發行項名稱。
將 JoinArticleName 屬性設定為邏輯記錄關聯性中的現有父發行項名稱。
將 FilterName 屬性設定為邏輯記錄關聯性的名稱。
將 JoinFilterClause 屬性設定為定義此關聯性的運算式。
將 FilterTypes 屬性設定為 LogicalRecordLink 的值。如果邏輯記錄關聯性也是聯結篩選,請針對這個屬性指定 JoinFilterAndLogicalRecordLink 的值。如需詳細資訊,請參閱<使用邏輯記錄分組相關資料列的變更>。
在代表關聯性中子發行項的物件上,呼叫 AddMergeJoinFilter 方法。傳遞步驟 8 中的 MergeJoinFilter 物件來定義關聯性。
針對發行集中每一個剩餘的邏輯記錄關聯性重複步驟 8 和 9。
範例
這個範例會針對 SalesOrderHeader 和 SalesOrderDetail 資料表建立組成兩個新發行項的邏輯記錄。
// Define the Publisher and publication names.
string publisherName = publisherInstance;
string publicationName = "AdvWorksSalesOrdersMerge";
string publicationDbName = "AdventureWorks";
// Specify article names.
string articleName1 = "SalesOrderHeader";
string articleName2 = "SalesOrderDetail";
// Specify logical record information.
string lrName = "SalesOrderHeader_SalesOrderDetail";
string lrClause = "[SalesOrderHeader].[SalesOrderID] = "
+ "[SalesOrderDetail].[SalesOrderID]";
string schema = "Sales";
MergeArticle article1 = new MergeArticle();
MergeArticle article2 = new MergeArticle();
MergeJoinFilter lr = new MergeJoinFilter();
MergePublication publication = new MergePublication();
// Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);
try
{
// Connect to the Publisher.
conn.Connect();
// Verify that the publication uses precomputed partitions.
publication.Name = publicationName;
publication.DatabaseName = publicationDbName;
publication.ConnectionContext = conn;
// If we can't get the properties for this merge publication, then throw an application exception.
if (publication.LoadProperties())
{
// If precomputed partitions is disabled, enable it.
if (publication.PartitionGroupsOption == PartitionGroupsOption.False)
{
publication.PartitionGroupsOption = PartitionGroupsOption.True;
}
}
else
{
throw new ApplicationException(String.Format(
"Settings could not be retrieved for the publication. " +
"Ensure that the publication {0} exists on {1}.",
publicationName, publisherName));
}
// Set the required properties for the PurchaseOrderHeader article.
article1.ConnectionContext = conn;
article1.Name = articleName1;
article1.DatabaseName = publicationDbName;
article1.SourceObjectName = articleName1;
article1.SourceObjectOwner = schema;
article1.PublicationName = publicationName;
article1.Type = ArticleOptions.TableBased;
// Set the required properties for the SalesOrderDetail article.
article2.ConnectionContext = conn;
article2.Name = articleName2;
article2.DatabaseName = publicationDbName;
article2.SourceObjectName = articleName2;
article2.SourceObjectOwner = schema;
article2.PublicationName = publicationName;
article2.Type = ArticleOptions.TableBased;
if (!article1.IsExistingObject) article1.Create();
if (!article2.IsExistingObject) article2.Create();
// Define a logical record relationship between
// PurchaseOrderHeader and PurchaseOrderDetail.
// Parent article.
lr.JoinArticleName = articleName1;
// Child article.
lr.ArticleName = articleName2;
lr.FilterName = lrName;
lr.JoinUniqueKey = true;
lr.FilterTypes = FilterTypes.LogicalRecordLink;
lr.JoinFilterClause = lrClause;
// Add the logical record definition to the parent article.
article1.AddMergeJoinFilter(lr);
}
catch (Exception ex)
{
// Do error handling here and rollback the transaction.
throw new ApplicationException(
"The filtered articles could not be created", ex);
}
finally
{
conn.Disconnect();
}
' Define the Publisher and publication names.
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publicationDbName As String = "AdventureWorks"
' Specify article names.
Dim articleName1 As String = "SalesOrderHeader"
Dim articleName2 As String = "SalesOrderDetail"
' Specify logical record information.
Dim lrName As String = "SalesOrderHeader_SalesOrderDetail"
Dim lrClause As String = "[SalesOrderHeader].[SalesOrderID] = " _
& "[SalesOrderDetail].[SalesOrderID]"
Dim schema As String = "Sales"
Dim article1 As MergeArticle = New MergeArticle()
Dim article2 As MergeArticle = New MergeArticle()
Dim lr As MergeJoinFilter = New MergeJoinFilter()
Dim publication As MergePublication = New MergePublication()
' Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)
Try
' Connect to the Publisher.
conn.Connect()
' Verify that the publication uses precomputed partitions.
publication.Name = publicationName
publication.DatabaseName = publicationDbName
publication.ConnectionContext = conn
' If we can't get the properties for this merge publication, then throw an application exception.
If publication.LoadProperties() Then
' If precomputed partitions is disabled, enable it.
If publication.PartitionGroupsOption = PartitionGroupsOption.False Then
publication.PartitionGroupsOption = PartitionGroupsOption.True
End If
Else
Throw New ApplicationException(String.Format( _
"Settings could not be retrieved for the publication. " _
& "Ensure that the publication {0} exists on {1}.", _
publicationName, publisherName))
End If
' Set the required properties for the SalesOrderHeader article.
article1.ConnectionContext = conn
article1.Name = articleName1
article1.DatabaseName = publicationDbName
article1.SourceObjectName = articleName1
article1.SourceObjectOwner = schema
article1.PublicationName = publicationName
article1.Type = ArticleOptions.TableBased
' Set the required properties for the SalesOrderDetail article.
article2.ConnectionContext = conn
article2.Name = articleName2
article2.DatabaseName = publicationDbName
article2.SourceObjectName = articleName2
article2.SourceObjectOwner = schema
article2.PublicationName = publicationName
article2.Type = ArticleOptions.TableBased
If Not article1.IsExistingObject Then
article1.Create()
End If
If Not article2.IsExistingObject Then
article2.Create()
End If
' Define a logical record relationship between
' SalesOrderHeader and SalesOrderDetail.
' Parent article.
lr.JoinArticleName = articleName1
' Child article.
lr.ArticleName = articleName2
lr.FilterName = lrName
lr.JoinUniqueKey = True
lr.FilterTypes = FilterTypes.LogicalRecordLink
lr.JoinFilterClause = lrClause
' Add the logical record definition to the parent article.
article1.AddMergeJoinFilter(lr)
Catch ex As Exception
' Do error handling here and rollback the transaction.
Throw New ApplicationException( _
"The filtered articles could not be created", ex)
Finally
conn.Disconnect()
End Try