我有一个有多个表的数据库,但这些表在数据库中没有任何关系定义(参照完整性)。它们相互关联,但由触发器和应用程序维护。我在 Microsoft SQL Server 2008 R2 合并复制上创建了自定义冲突解决程序。在自定义冲突解决程序中,我通过来自发布者数据库的 SQL 查询并基于该优先级检查订阅者优先级。我正在解决复制数据表中的冲突。
自定义冲突解决程序代码如下
public override ActionOnUpdateConflict UpdateConflictsHandler(DataSet publisherDataSet, DataSet subscriberDataSet, ref DataSet customDataSet,
ref ConflictLogType conflictLogType, ref string customConflictMessage, ref int historyLogLevel, ref string historyLogMessage)
{
// Priority column AcceptDate
DateTime? publisherModifiedDate = string.IsNullOrWhiteSpace(Convert.ToString(publisherDataSet.Tables[0].Rows[0]["AcceptDate"])) ? new Nullable<DateTime>() : DateTime.Parse(publisherDataSet.Tables[0].Rows[0]["AcceptDate"].ToString());
DateTime? subscriberModifiedDate = string.IsNullOrWhiteSpace(Convert.ToString(subscriberDataSet.Tables[0].Rows[0]["AcceptDate"])) ? new Nullable<DateTime>() : DateTime.Parse(subscriberDataSet.Tables[0].Rows[0]["AcceptDate"].ToString());
//Get priority via userid
int publisherUserId = Convert.ToInt32(publisherDataSet.Tables[0].Rows[0]["UserId"]);
int subcriberUserId = Convert.ToInt32(subscriberDataSet.Tables[0].Rows[0]["UserId"]);
DataTable dt = new DataTable();
//Publisher
using (SqlConnection sqlConnection = new SqlConnection(AppConfig.PubliosherConnectionString))
{
SqlCommand cmd = new SqlCommand("select * from RISubscriber where UserId=" + publisherUserId, sqlConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
int publisherPriority = Convert.ToInt32(dt.Rows[0]["SubscriberPriority"]);
//Subcriber
dt = new DataTable();
using (SqlConnection sqlConnection = new SqlConnection(AppConfig.PubliosherConnectionString))
{
SqlCommand cmd = new SqlCommand("select * from RISubscriber where UserId=" + subcriberUserId, sqlConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
int subcriberPriority = Convert.ToInt32(dt.Rows[0]["SubscriberPriority"]);
if (subcriberPriority > publisherPriority)
{
customDataSet = subscriberDataSet.Copy();
}
else
{
customDataSet = publisherDataSet.Copy();
}
return ActionOnUpdateConflict.AcceptCustomConflictData;
}
问题:我想基于自定义冲突解决器在订阅者之间复制数据,并希望保持表之间的数据完整性。
请告诉我。如果你有任何想法。
我们怎么做?
谢谢