同步数据库除已删除的记录 [英] Sync database except deleted records

查看:86
本文介绍了同步数据库除已删除的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在过去几天里经历了很多文档。我知道它一定是可能的,但仍然没有找到
如何。所以我希望有人可以帮助我。


我想将SQL Server数据库与另一个SQL Server数据库同步,但只添加和编辑记录。例如,如果我从数据库A中删除
记录X并进行同步,则数据库B仍将具有记录X,但所有其他创建和修改的记录将被同步。


我理解我’我必须设置方向只下载/上传。并且它无法在这种情况下使用
SQL Server的更改跟踪。但是如何手动设置?! (我不&rsquo的;吨介意我必须使用同步框架的4.0 CTP版)

解决方案

如果要在上传本地更改并将其应用于远程提供程序之前对其进行操作,则可以在本地提供程序上订阅ChangesSelected事件,然后通过更改数据集并对其进行操作。


这里是一个示例(只是CHK YHE语法如果我没有它的权利,因为我只复制和修改我的其他样品)


空隙LocalDataCache1ServerSyncProvider_ChangesSelected(对象发件人,Microsoft.Synchronization .Data.ChangesSelectedEventArgs e)

        {

            //让我们检查一下我们是否正在同步我们感兴趣的表格。
           如果(e.Context.DataSet.Tables.Contains(QUOT;工作订单"))

            {

                VAR的dataTable = e.Context.DataSet.Tables ["工作订单"];

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;对(INT J = 0; J< dataTable.Rows.Count; J ++)

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; {

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的DataRow行= dataTable.Rows [J];


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP; //我们只以 兴趣;删除

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;如果(row.RowState == DataRowState.Deleted)

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP; {

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //我们删除的行,以便它不被发送和应用在远程侧


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; dataTable.Rows.Remove(行);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
                }
            }
       }


I’ve been going through a lot of documentation the last few days. And I know it must be possible, but still haven’t found how. So I hope that someone can help me.

I want to synchronize a SQL Server database with another SQL Server database but only add and edit the records. For example, if I remove record X from database A and synchronize, database B will still have record X, but all other created and modified records will be synchronized.

I understand I’ll have to set the direction on only download/upload. And that it’s not possible to use change tracking of SQL Server for this situation. But how do I set this manually?! (I don’t mind if I have to use the 4.0 CTP version of the Sync Framework).

解决方案

if you want to manipulate local changes before they are uploaded and applied to the remote provider, you can subscribe to the ChangesSelected event on the local provider and go thru the change dataset and manipulate it.

here's a sample (just chk yhe syntax if i did it right as i only copied and modified my other sample)

void LocalDataCache1ServerSyncProvider_ChangesSelected(object sender, Microsoft.Synchronization.Data.ChangesSelectedEventArgs e)
        {
            //let's check if we're synching the table we're interested
            if (e.Context.DataSet.Tables.Contains("WorkOrders"))
            {
                var dataTable = e.Context.DataSet.Tables["WorkOrders"];
                for (int j = 0; j < dataTable.Rows.Count; j++)
                {
                    DataRow row = dataTable.Rows[j];

                    // we're only interested in deletes
                    if (row.RowState == DataRowState.Deleted)
                    {
                            // let's remove the row so it doesnt get sent and applied at the remote side

                            dataTable.Rows.Remove(row);
                        
                    }
                }
            }
       }


这篇关于同步数据库除已删除的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆