简体   繁体   English

我可以从一个 SQL 数据库中获取数据,然后使用 MVC C# 将该数据发布到不同服务器上的另一个 SQL 数据库中吗?

[英]Can I get data from one SQL database and then post that data on another SQL database on a diferent server using MVC C#

I searched endlessly and couldn't find a answer to my problem,我无休止地搜索,找不到问题的答案,

I'm building an MVC project where I need to get data from some tables in an SQL database, display them (no problem in this section) and then is there a way to post that information on another database hosted on a diferent server with identical tables?我正在构建一个 MVC 项目,我需要从 SQL 数据库中的一些表中获取数据,显示它们(在本节中没有问题),然后有没有办法将该信息发布到托管在不同服务器上的另一个数据库上表?

The data I want to post is a new record我要发布的数据是新记录

Thanks for your time !谢谢你的时间 ! :) :)

use two different database connections:使用两个不同的数据库连接:

var con1 = new MySqlConnection("server="+host1+";database="+dbname+";uid="+user+";pwd="+pass+";");
con1.Open();
var cmd1 = new MySqlCommand( "SELECT name FROM mytable ", con1 );
var reader = cmd1.ExecuteReader();

var con2 = new MySqlConnection("server="+host2+";database="+dbname+";uid="+user+";pwd="+pass+";");
con2.Open();

while( reader.Read() )
{
    var name = reader.GetString( 0 );
    var cmd2 = new MySqlCommand( "INSERT INTO mytable ( name ) VALUES ("+name+")", con2 );
    cmd2.ExecuteNonQuery();
}

for better performance, use bulk inserts.为了获得更好的性能,请使用批量插入。

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

相关问题 使用C#动态将所有表的数据从一个SQL Server数据库插入第二个数据库 - Insert data from one SQL Server database into a second database for all tables dynamically using C# 如何在 C# 和 sql 中调用数据库中的数据? - How can I call the data from the database in C# and sql? 使用wcf,c#从json中的sql server数据库中获取数据 - get data from sql server database in json using wcf,c# 尝试从文本框中将数据放入SQL Server数据库中,并从C#中的另一张表中放入一些数据 - Trying to put data into a SQL Server database from textbox and some data from another table in C# 在 C# 中,如何使用 POST 方法将数据从 SQL Server 发送到 HTTP 请求? - In C# how can I send data from SQL Server to a HTTP request using the POST method? 使用 MVC C# 将 Excel 数据导入 SQL Server 数据库中的多个表 - Import Excel Data to Multiple Tables in SQL Server Database using MVC C# 使用C#数据对象将记录插入SQL Server数据库 - Insert Records into SQL Server Database using C# Data Objects 使用 HTTP Post 从 excel 在 SQL Server 数据库中插入数据 - Insert data in SQL Server database from excel using HTTP Post 使用 C# 将数据插入 SQL Server 数据库 - Insert data into SQL Server database using C# 不使用 C# 将数据插入 SQL 服务器数据库 - Not inserting data into SQL Server database using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM