繁体   English   中英

从一台 SQL Server 获取数据到另一台 SQL Server(使用 C#)

[英]Get Data From one SQL Server to Other SQL server (using C#)

我想要将数据从一个 sql 复制到另一个 sql 的代码(使用 C#)

当我点击按钮时

复制我的db2_connect数据库数据,如:

select * from importdb where bhand = 3 and store = 14

我的另一个数据库是: abdConnection

在 abdConnection 表中是 importabd 并且归档是相同的( bhand

谁能写出完整的代码。

感谢您

using (SqlConnection connection = new SqlConnection("yourConnectionString"))
{
    connection.Open();

    SqlCommand command = new SqlCommand("select bhand from importdb where bhand = 3 and store = 14", connection);
    SqlDataReader reader = command.ExecuteReader();

    SqlConnection connection2 = new SqlConnection("yourConnectionStringTo2ndDB");
    connection2.Open();

    while (reader.Read())
    {
        SqlCommand command = new SqlCommand("insert into importabd ('bhand') values ('" +  reader[0]+"')", connection2);
        command.ExecuteNonQuery();
    }
    connection2.close();
}

在按钮单击事件中尝试此 sql 命令

insert into [server2name].adbconnection.dbo.tablename(select * from[server1name].  
    importdb.abo.tablename where bhand=3 and store-14)

暂无
暂无

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

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