简体   繁体   English

在C#中的两个SQL Server数据库之间复制数据

[英]Copy data between two SQL Server databases in C#

I have two databases. 我有两个数据库。 One of them belongs to a CRM software and is the source. 其中一个属于CRM软件,是源代码。

The other one will be the destination used by a tool I'm developing. 另一个将是我正在开发的工具使用的目的地。

The destination will contain a table ADDRESSES with a subset of the columns of a table of the same name in the source database. 目标将包含一个表ADDRESSES ,其中包含源数据库中同名表的列的子集。

What is the best (most efficient) way to copy the data between those databases (btw: they're on different SQL Server instances if that's important). 在这些数据库之间复制数据的最佳(最有效)方法是什么(顺便说一下:如果这很重要,它们就在不同的SQL Server实例上)。

I could write a loop which does INSERT into the destination for each row obtained from the source but I don't think that this is efficient. 我可以编写一个循环,它对从源获得的每一行执行INSERT到目标,但我不认为这是有效的。

My thoughts and information: 我的想法和信息:

  • The data won't be altered on its way from source to destination 数据在从源到目的地的途中不会被更改
  • It will be altered on its way back 它会在回来的路上改变
  • I don't have the complete structure of the source but I know which fields I need and that they're warranted to be in the source (hence, access to the rows obtained from source isn't possible using the index of columns) 我没有源的完整结构,但我知道我需要哪些字段,并且他们被保证在源中(因此,使用列索引无法访问从源获取的行)
  • I can't use LINQ. 我不能使用LINQ。

Anything leading me in the right direction here is appreciated. 在这里引导我正确方向的任何事情都值得赞赏。

Edit: 编辑:
I really need a C# way to copy the data. 我真的需要一种C#方式来复制数据。 I also need to know how to merge the copied rows back to the source. 我还需要知道如何将复制的行合并回源。 Is it really necessary (or even best practise) to do this row after row? 是否真的有必要(甚至是最佳实践)一排一排地做这一行?

Why write code to do this? 为什么要写代码呢?

The single fastest and easiest way is just to use SQL Server's bcp.exe utility (bcp: B ulk C opy P rogram). 最简单,最简单的方法就是使用SQL Server的bcp.exe实用程序(bcp: B ulk C opy P rogram)。

  • Export the data from the source server. 从源服务器导出数据。
  • Zip it or tar it if it needs it. 如果需要,可以将其压缩或tar。
  • FTP it over to where it needs to go, if you need to move it to another box. 如果你需要将它移动到另一个盒子,请将其FTP到需要去的地方。
  • Import it into the destination server. 将其导入目标服务器。

You can accomplish the same thing via SQL Server Management Studio in a number of different ways . 您可以通过SQL Server Management Studio以多种不同方式完成相同的任务。 Once you've defined the task, it can be saved and it can be scheduled. 一旦定义了任务,就可以保存它并安排它。

You can use SQL Server's Powershell objects to do this as well. 您也可以使用SQL Server的Powershell对象来执行此操作。

If you're set on doing it in C#: 如果您已经开始在C#中执行此操作:

  • write your select query to get the data you want from the source server. 编写您的select查询以从源服务器获取所需的数据。
  • execute that and populate a temp file with the output. 执行该操作并使用输出填充临时文件。
  • execute SQL Server's bulk insert statement against the destination server to insert the data. 对目标服务器执行SQL Server的bulk insert语句以插入数据。

Note: For any of these techniques, you'll need to deal with identity columns if the target table has them. 注意:对于这些技术中的任何一种,如果目标表具有标识列,则需要处理标识列。 You'll also need to deal with key collisions. 您还需要处理关键冲突。 It is sometimes easier to bulk load the data into a perma-temp table first, and then apply the prerequisite transforms and manipulations to get it to where it needs to go. 有时更容易将数据批量加载到perma-temp表中,然后应用先决条件转换和操作以使其到达需要的位置。

According to your comment on Jwrit's answer, you want two way syncs. 根据你对Jwrit答案的评论,你想要双向同步。

If so, you might want to look into Microsoft Sync Framework . 如果是这样,您可能需要查看Microsoft Sync Framework

We use it to sync 200+ tables on Premise SQL to SQL Azure and SQL Azure to SQL Azure. 我们使用它将Premise SQL上的200多个表同步到SQL Azure,将SQL Azure同步到SQL Azure。

You can use purely C#. 你可以纯粹使用C#。 However, it might offer a lot more than you want, or it might be over kill for a small project. 但是,它可能会提供比您想要的更多,或者它可能会超过一个小项目。

I'm just saying so that you can have different option for your project. 我只是这样说,你可以为你的项目提供不同的选择。

If these databases exist on two servers you can setup a link between the servers by executing sp_addlinkedserver there are instructions for setting this up here. 如果这两个服务器上存在这些数据库,您可以通过执行sp_addlinkedserver在服务器之间建立链接,这里有说明进行设置。 This may come in handy if you plan on regularly "Sharing" data. 如果您计划定期“共享”数据,这可能会派上用场。

http://msdn.microsoft.com/en-us/library/ff772782.aspx http://msdn.microsoft.com/en-us/library/ff772782.aspx

Once the servers are linked a simple select statement can copy the rows from one table to another 连接服务器后,简单的select语句可以将行从一个表复制到另一个表

INSERT INTO db1.tblA( Field1,Field2,Field2 ) SELECT Field1,Field2,Field2 FROM db2.tblB INSERT INTO db1.tblA(Field1,Field2,Field2)SELECT Field1,Field2,Field2 FROM db2.tblB

If the Databases are on the same instance you only need to execute similar SQL to the above 如果数据库位于同一实例上,则只需执行与上述类似的SQL

如果这是一次 - 最好的选择通常是SSIS(SQL服务器集成服务),除非有复杂的数据转换 - 你可以快速轻松地进行列映射并在15分钟内完成(可靠)..... 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM