2015-06-16 60 views
3

我有兩個不同的數據庫,客戶端從.MDF文件附加到.\SQLEXPRESS服務器。主服務器在名爲COMPUTER_NAME的另一臺計算機上的服務器上運行。如何合併兩個不同服務器上的兩個數據庫?

我想合併這些使用C#運行.SQL文件。我會貼上我的代碼如下供參考,但基本上我的問題是,如果我連接到使用

string sqlConnectionString = @"Server=.\SQLEXPRESS; Trusted_Connection=True"; 

服務器然後我找不到COMPUTER_NAME數據庫。如果我用

string sqlConnectionString = @"Server=COMPUTER_NAME; Trusted_Connection=True"; 

它會尋找在C我.MDF申報:COMPUTER_NAME驅動,而不是本地計算機。

如何連接到不同服務器上的這兩個數據庫?

附加信息:

我使用的SQL腳本。當兩個數據庫都在同一臺服務器上時,這種方式可以很好地工作,但我不能再這樣做了。

CREATE DATABASE ClientDB 
ON (Filename = 'C:\Clayton.mdf') 
    , (Filename = 'C:\Clayton_log.ldf') 
FOR ATTACH; 

-- update the client from the master 
MERGE [ClientDB].[dbo].[table] trgt 
using [MasterDB].[dbo].[table] src 
ON trgt.id = src.id 

WHEN matched AND trgt.lastmodified <= src.lastmodified THEN -- if master row is newer 
    UPDATE SET trgt.[info] = src.[info], ...     -- update the client 

WHEN NOT matched BY source         -- delete rows added by client 
    THEN DELETE 

WHEN NOT matched BY target         -- insert rows added by master 
    THEN INSERT ([info], ...) VALUES (src.[info], ...); 


-- close all connections to database 
ALTER DATABASE ClientDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE; 

-- detach database 
EXEC sp_detach_db 'ClientDB', 'true'; 

我使用它的C#這樣運行:

string sqlConnectionString = @"Server=.\SQLEXPRESS; Trusted_Connection=True"; 

string script = File.ReadAllText(Environment.CurrentDirectory + @"\MergeTotal.sql"); 
SqlConnection conn = new SqlConnection(sqlConnectionString); 

IEnumerable<string> commandStrings = Regex.Split(script, @"^\s*GO\s*$", 
          RegexOptions.Multiline | RegexOptions.IgnoreCase); 

conn.Open(); 
foreach (string commandString in commandStrings) 
{ 
    if (commandString.Trim() != "") 
    { 
     using (var command = new SqlCommand(commandString, conn)) 
     { 
      command.ExecuteNonQuery(); 
     } 
    } 
} 

如果整個過程中.SQLC#發生的,只要它有預期的效果我不在乎。

在此先感謝您的任何指導或建議。

+0

你多久需要運行呢? –

+0

通常我很害怕。現在,它被配置爲每次程序啓動時,它都會檢查它是否與主數據庫連接到網絡,如果是,則會合並。 –

+0

你可能不得不考慮重新設計它的工作方式,因爲你不能這樣真正地合併。您應該跟蹤更改,然後在主服務器上重新執行它們。 –

回答

2

鏈接服務器將幫助您能夠同時訪問數據,如果這是要求。但是,如果您想將數據合併在一起,我建議您查看sp_generate_merge以將數據提取到合併腳本中(對於移動數據非常方便)。另請參閱關於生成合並數據的問題here

+0

感謝您的迴應!你能否給我舉一個例子說明在這種情況下我會如何使用它?我需要合併兩種方式來提交兩個版本的更改,但是我一直收到錯誤「MERGE語句的目標不能是遠程表」。 –

+0

您需要在一臺機器上生成合並腳本,複製輸出並在另一臺機器上運行合併。然後你會扭轉這個過程。 –

0

好的,我不得不完全拋棄整個.MDF的東西。我只是設置了數據庫,而不是從.MDF附加和重新附加數據庫。

這裏是我的代碼來初始化平板電腦上的本地數據庫:

CREATE DATABASE LocalClaytonDB 
ON (Filename = 'C:\ProgramData\Clayton\Clayton.mdf') 
    , (Filename = 'C:\ProgramData\Clayton\Clayton_log.ldf') 
FOR ATTACH; 
GO 

EXEC sp_addlinkedserver @server='Server' 

這裏是我的代碼,這兩個數據庫同步:

-- update the client from the master 
MERGE [LocalClaytonDB].[dbo].[tableName] trgt 
using [Server].[Clayton].[dbo].[tableName] src 

ON trgt.id = src.id 

WHEN matched AND trgt.lastmodified <= src.lastmodified THEN 
    -- if the master has a row newer than the client 
    -- update the client      
    UPDATE SET trgt.[allColumns]  = src.[allColumns], 
      trgt.[id]    = src.[id], 
      trgt.[lastmodified] = src.[lastmodified] 

-- delete any rows added by a client 
WHEN NOT matched BY source 
THEN 
    DELETE 

-- insert any rows added by the master 
WHEN NOT matched BY target 
THEN 
    INSERT ([allColumns], 
      [id], 
      [lastmodified]) 
    VALUES (src. [allColumns], 
      src.[id], 
      src.[lastmodified]); 


-- now we update the master from the client 
-- Note: 
-- because the serverDB is a linked server 
-- we can't use another MERGE statement, otherwise 
-- we get the error: "The target of a MERGE statement 
-- cannot be a remote table, a remote view, or a view over remote tables." 

UPDATE 
    serverDB 

SET 
    [allColumns]  = [localDB].[allColumns], 
    [id]    = [localDB].[id], 
    [lastmodified]  = [localDB].[lastmodified] 

FROM 
    [Server].[Clayton].[dbo].[tableName] serverDB 

INNER JOIN 
    [LocalClaytonDB].[dbo].[tableName] localDB 

-- update where the id is the same but the client is newer than the master 

ON serverDB.id = localDB.id 
     AND localDB.lastmodified >= serverDB.lastmodified 
相關問題