Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中合并来自不同SQL Server数据库的两个表中的数据_C#_Sql Server_Winforms_Sql Server 2008_Merge - Fatal编程技术网

C# 在C中合并来自不同SQL Server数据库的两个表中的数据

C# 在C中合并来自不同SQL Server数据库的两个表中的数据,c#,sql-server,winforms,sql-server-2008,merge,C#,Sql Server,Winforms,Sql Server 2008,Merge,我有一个中央数据库和两个具有相同结构标识id的客户端数据库。该应用程序允许用户将中央数据库中选定表的数据与一个客户端数据库合并 例如: [db_central]。[表格]: [db_客户端_1]。[表]: [db_客户端_2]。[表]: 合并两次后的预期结果: [db_central]。[表格]: 目前,我只能从数据库加载表 当用户单击手动同步按钮时,应用程序将从左到右比较和合并选定表的数据,反之亦然 如果表不存在,它将创建新表。若表确实存在,它将比较和合并数据,但我不知道完成此任务的最佳解决方

我有一个中央数据库和两个具有相同结构标识id的客户端数据库。该应用程序允许用户将中央数据库中选定表的数据与一个客户端数据库合并

例如:

[db_central]。[表格]:

[db_客户端_1]。[表]:

[db_客户端_2]。[表]:

合并两次后的预期结果:

[db_central]。[表格]:

目前,我只能从数据库加载表

当用户单击手动同步按钮时,应用程序将从左到右比较和合并选定表的数据,反之亦然

如果表不存在,它将创建新表。若表确实存在,它将比较和合并数据,但我不知道完成此任务的最佳解决方案是什么


任何建议都将不胜感激。

理想情况下,您应该在中央数据库的表中有两列

启用标识的主键

ChildKey子数据库的主键


中央数据库中的主键列将负责排序,chiild列将为您提供各自数据库中的主键

如果数据库位于同一服务器上,或者如果您使用Exception在它们之间有一个链接的服务器,这似乎是一个简单的sql查询

如果必须在Linq中执行此操作,则非常类似:

// Get the list of names to add
var newNames = dbContext.db_client_1.Select(e => e.name).Except(dbContext.db_central.Select(e => e.name));
// Convert the names to a list of entity objects and add them.
dbContext.db_central.Add(newNames.Select(e => new db_central { name = e.name });
dbContext.SaveChanges();

这是假设您不想在db_central中重复,谢谢,但问题是中央数据库和客户端数据库的表具有相同的结构,我的应用程序不应该修改表结构。我尝试将此查询插入到[UniversityB].[dbo].[Student]选择[name],[day_of_birth],[address]FROM[UniversityA].[dbo]。[Student]其中[UniversityA].[dbo].[Student].[Student\u id]不在选择[UniversityB].[dbo].[Student].[Student\u id]来自[UniversityB].[dbo].[Student]但它给了我这个错误:当identity_INSERT设置为ON或复制用户正在插入NOT for replication identity列时,必须为表“Student”中的identity列指定Msg 545,Level 16,State 1,Line 1显式值。是否理想?@user1728585,看起来其中一列最有可能是Student_id实体列,但已为复制设置了identity_insert on或not。这意味着您需要在该字段中插入一个值。但是,这使我认为,对于您尝试完成的任何任务,可能都无法正确设置复制。我怀疑有更好的方法来解决此问题。我想到了合并复制。
+-------+-------+
| id    | name  |
+-------+-------+
| 3     | D     |
+---------------+
+-------+-------+
| id    | name  |
+-------+-------+
| 3     | E     |
+---------------+
+-------+-------+
| id    | name  |
+-------+-------+
| 1     | A     |
| 2     | B     |
| 3     | C     |
| 4     | D     |
| 5     | E     |
+---------------+
insert into db_central       // Target table
select name from db_client_1 // Source table
except
select name from db_central  // Target table
// Get the list of names to add
var newNames = dbContext.db_client_1.Select(e => e.name).Except(dbContext.db_central.Select(e => e.name));
// Convert the names to a list of entity objects and add them.
dbContext.db_central.Add(newNames.Select(e => new db_central { name = e.name });
dbContext.SaveChanges();