简体   繁体   English

如何从数据库中的表读取数据并将其插入另一个数据库?

[英]how to read data from a table in database and insert into another database?

i want to create a windows service for read data from one database and then insert data into an other database. 我想创建一个Windows服务来从一个数据库中读取数据,然后将数据插入另一个数据库中。 how can i do it ? 我该怎么做 ?

Can I read and copy my data with this stored procedure in sql without windows service or any other programs ? 没有Windows服务或任何其他程序,是否可以在sql中使用此存储过程读取和复制数据?

My data changes every day and I need to update my database when data is changed can i run a stored procedure with a schedule? 我的数据每天都在变化,因此我需要在更改数据时更新数据库。我可以按时间表运行存储过程吗?

If i can do it then my data is replace o duplicate ? 如果可以,那么我的数据将被替换或重复吗?

i want to save all my data and don't want to replace or duplicate it 我想保存我所有的数据,不想替换或复制它

What you are looking for is a Master-Slave Database Architecture. 您正在寻找的是主从数据库体系结构。

This architecture will replicate the datas of one database to another. 这种体系结构会将一个数据库的数据复制到另一个数据库。 Read in here for more info. 在此处阅读更多信息。 All the CRUD operatiuons that you will be performing in Master DB will get replicated to Slave DB. 您将在主数据库中执行的所有CRUD操作都将复制到从数据库。

http://en.wikipedia.org/wiki/Master/slave_(technology) http://en.wikipedia.org/wiki/Master/slave_(technology)

There are different techniques to make such copying. 有不同的技术可以进行这种复制。 1. Database replication. 1.数据库复制。 2. A trigger may copy specific changes to another database. 2.触发器可以将特定更改复制到另一个数据库。

I'll assume you use sql server (you didn't write it, but it's natural for c# developers) 我假设您使用sql server(您没有编写它,但是对于C#开发人员来说是自然的)

  1. Yes, it is possible to create a windows service to synchronize 2 different databases. 是的,可以创建一个Windows服务来同步2个不同的数据库。 assuming you have the same table in both databases all you have to do is read the data from one database into a datatable, and send it to the other database (it's possible to send a datatable as a stored procedure parameter). 假设两个数据库中都有相同的表,那么您要做的就是将一个数据库中的数据读入数据表,然后将其发送到另一个数据库(可以将数据表作为存储过程参数发送)。

However, there might be better ways to handle this (such as replication). 但是,可能有更好的方法来处理此问题(例如复制)。

  1. yes, it is possible to create a scheduled job on sql server, that can run stored procedured. 是的,可以在sql服务器上创建可以运行存储过程的计划作业。

  2. it's up to you to decide if you want to duplicate the data or replace it. 由您决定是要复制数据还是替换数据。 it depends on the sql statement you will write. 这取决于您要编写的sql语句。

You can do it using SSIS packages as well which is quite easy to use. 您也可以使用SSIS包来做到这一点,这非常容易使用。

If you go with this option then you need to give source database and destination database conncetionstring. 如果使用此选项,则需要提供源数据库和目标数据库的连接字符串。

Also you need to have a clone table of the source table into the destination database. 另外,您还需要将源表的克隆表插入目标数据库。

Then you need to create a sql job which will call SSIS package in first step and in second step you have to call created procedures which will get records from the clone table of the destination database after process is completed then truncate clone table. 然后,您需要创建一个sql作业,该作业将在第一步中调用SSIS程序包,而在第二步中,您必须调用创建的过程,该过程将在完成过程后从目标数据库的克隆表中获取记录,然后截断克隆表。

So that you have always new records to update existing records. 这样您就可以始终拥有新记录来更新现有记录。

Let me know if you need more clarification how to setup job and SSIS package. 让我知道是否需要进一步说明如何设置作业和SSIS包。

Also, you can use this option when database are located on different servers. 另外,当数据库位于不同的服务器上时,可以使用此选项。

Regards, 问候,
Vishal Bagdai 维沙尔·巴代(Vishal Bagdai)

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

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