如何与本地数据库的远程服务器数据库同步数据库、服务器

2023-09-06 11:17:50 作者:是不是女人都爱一往情深/

我想获得一个表中所有的细节,从远程服务器数据库,以我的本地数据库,在 页面加载事件或其他一些很好的办法,这应该发生,因为后端处理任何一个可以帮助我在这个问题上。照片

I would like to get all the details of a single table from Remote server DB to my local DB, during page load event or some other good approach, which should happen as a back end process can any one help me over this issue.

1,在桌面和Web应用程序中创建单应用。 2 :当用户报名参加新的客户应在Web应用程序分贝时启动的应用程序被添加桌面应用程序的新客户。

1. Single Application created in Desktop and Web Application. 2. When User enroll new customer in Desktop Application that new Customer should be added in the Web Application Db when the Application is started.

注意:

Server数据库表中的列可能会略有从本地数据库不同。 当新用户在服务器中每次加入,应该当UserPage.aspx页面加载更新本地数据库中。

Server DB table Columns may slightly differ from local DB. Each time when a new user is added in the server, it should update the local DB when the UserPage.aspx page is loaded.

使用工具: ASP.NET,SQL Server 2008的

Tools using: ASP.NET,SQL SERVER 2008.

例如: 让DB名称是样品和表名是客户

Eg: Let the DB name be sample and the table name is 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 Application..initially在Web应用程序中,当一个新的用户添加的所有数据都存储在数据库除了 Link_id ,这是获得来自服务器的响应,并保存在本地数据库。

here the Link_id is used as common for Desktop and web Application..initially in web application,when a new user is added all the data are stored in DB except the Link_id, which is get as a response from the server and save in the local DB.

感谢。

推荐答案

我会推荐这种方法:

在本地数据库中创建一个临时表; 在上表的更改本地数据库中创建一个触发器,你需要同步; 更新临时表; 临时表曾经在一段时间(/小时/天每分钟一次,根据您的需要)

与服务器同步; Create a staging table in your local database; Create a trigger in your local database on change of the tables you need to sync; Update the staging table;

Synchronize the staging table to the server once in a while (one per minute / hour / day, depending on your needs);

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

A) Create a linked database connection in your local database. Create a procedure that synchronizes the data from the staging table to the server database;

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

B) Or sync the database using ASP.NET by reading the local database and writing to the server database.

这个解决方案是更好的,然后直接在ASP.NET这样做,因为当你有可用性问题与您的服务器,这仍然可以工作。

This solution is better then doing this directly in ASP.NET, because when you have availability problems with your server, this will still work.

一个完整的工作的例子:

A full working example:

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

调用同步将做同步。请注意,以创建 remote_table 在其他服务器上,并创建一个数据库链接。

Calling sync will do the synchronization. Be aware to create the remote_table on the other server and create a database link.