【问题标题】:how to Synchronize Remote Server DB with Local DB如何将远程服务器数据库与本地数据库同步
【发布时间】:2014-03-12 14:26:05
【问题描述】:

我想在
期间将单个表的所有详细信息从远程服务器数据库获取到我的本地数据库 页面加载事件或其他一些好的方法,应该作为后端进程发生,任何人都可以帮助我解决这个问题。

1. 在桌面应用程序和 Web 应用程序中创建单个应用程序。
2. 当用户在桌面应用程序中注册新客户时,应在 Web 中添加新客户Application 启动时的Application Db。

注意:

服务器数据库表列可能与本地数据库略有不同。 每次在服务器中添加新用户时,都应该在加载 UserPage.aspx 页面时更新本地数据库。

工具使用: ASP.NET、SQL SERVER 2008。

例如: 设 DB 名为 sample,表名为 customer

Table Header in Server DB: Cus_id,Cus_name,Cus_address,Cus_email,Cus_mob,Link_Id
Table Headers in Local DB: Cus_id,Cus_name,Cus_address,Cus_email,Cus_mob,Cus_password,Link_Id

这里 Link_id 被用作桌面和 Web 应用程序的共同点。最初在 Web 应用程序中,
当添加新用户时,所有数据都存储在数据库中,除了
Link_id,作为从服务器获取的响应并保存在本地数据库中。

谢谢。

【问题讨论】:

    标签: c# asp.net .net sql-server-2008


    【解决方案1】:

    我会推荐这种方法:

    1. 在本地数据库中创建临时表;
    2. 在您需要同步的表发生更改时在本地数据库中创建触发器;
    3. 更新暂存表;
    4. 每隔一段时间将 staging table 同步到服务器(每分钟/小时/天一个,取决于您的需要);

      A) 在本地数据库中创建链接数据库连接。创建一个将临时表中的数据同步到服务器数据库的过程;

      B) 或者使用 ASP.NET 通过读取本地数据库并写入服务器数据库来同步数据库。

    此解决方案比直接在 ASP.NET 中执行此操作更好,因为当您的服务器出现可用性问题时,它仍然可以工作。

    一个完整的工作示例:

    create table x
    ( id          numeric(18, 0) identity(1,1)  not null
    , description nvarchar(1000)                not null
    )
    go
    
    create table x_staging
    ( id          numeric(18, 0) not null
    , description nvarchar(1000) not null
    , synced      bit            not null default 0
    )
    go
    
    /*
     * create this one on remote server in a database called test
    
    create table remote_table
    ( id          numeric(18, 0) identity(1,1)  not null
    , source_id   numeric(18, 0)                not null
    , description nvarchar(1000)                not null
    )
    go
    */
    
    create trigger x_ori on x
    after insert
    as
    begin
      insert into x_staging
      ( id
      , description
      , synced
      )
      select id
      ,      description
      ,      0 -- false
      from   inserted
      ;
    end
    go
    
    create procedure sync
    as
    begin
      declare @id numeric(18,0)
      declare @description nvarchar(1000)
      declare @x_cursor cursor
    
      set     @x_cursor = cursor for
      select  id
      ,       description
      from    x_staging
    
      open    @x_cursor
      fetch next
      from  @x_cursor into @id, @description
      while @@fetch_status = 0
      begin
        insert
        into   [REMOTE_SERVER].test.dbo.remote_table
        ( source_id
        , description
        )
        values
        ( @id
        , @description
        )
        ;
        update x_staging
        set    synced = 1
        where  id = @id
        ;
      fetch next
      from @x_cursor into @id, @description
      end
    
      close @x_cursor
      deallocate @x_cursor
    end
    go
    
    insert
    into   x
    ( description
    )
    values
    ( 'test'
    )
    go
    
    begin
      exec sync;
    end
    

    调用sync 将进行同步。请注意在另一台服务器上创建remote_table 并创建数据库链接。

    【讨论】:

    • 我已经完成了第 1,2 和 3 步,你能说一下如何做第 4 步和 A) 选项..
    • @farokemoahmed:查看完整工作代码的更新答案。
    • @PatrickHofman 好答案 upvote 还想补充一点,如果您将其作为 Windows 应用程序,您可以设置任务调度程序来运行程序。作为一个警告说明,我发现这类程序的难点在于让它在服务器上运行,具体取决于安全性。
    • @JPack:感谢您的评论和支持。我建议将其作为服务运行。你必须确保你有尽可能多的错误处理以保持它运行,即使连接丢失一段时间等。
    【解决方案2】:

    您可以使用 ms 同步框架 2.1,它允许用户从双方(客户端-服务器)同步...并且您可以安排同步有趣的调用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 2011-12-14
      • 2017-02-08
      • 1970-01-01
      相关资源
      最近更新 更多