对于sql合并复制中的自定义冲突解析程序,表之间的参照完整性是否必需?

时间:2015-12-30 13:00:10

标签: sql-server merge-replication integrity referential

我有一个拥有多个表的数据库,但这些表在数据库中没有任何关系定义(参照完整性)。 它们彼此相关,但由触发器和应用程序维护。 我在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;
}

问题:我想根据自定义冲突解决程序在订阅者之间复制数据,并希望保持表之间的数据完整性。

请告诉我。如果你有任何想法。

我们怎么做?

谢谢

0 个答案:

没有答案