【问题标题】:Get Data From one SQL Server to Other SQL server (using C#)从一个 SQL Server 获取数据到另一个 SQL Server(使用 C#)
【发布时间】:2014-05-04 13:20:36
【问题描述】:

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

当我点击按钮时

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

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

我的另一个数据库是: abdConnection

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

谁能写出完整的代码。

谢谢你

【问题讨论】:

  • 有人能写出完整的代码吗 不行! 这里不是问的好方法。首先展示你的努力,这样人们才能展示他们的努力。请阅读FAQHow to Askhelp center 作为开始..

标签: c# mysql sql sql-server


【解决方案1】:
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();
}

【讨论】:

  • 能否请您查看我的代码并告诉我如何修复此错误。谢谢。
  • 您尚未设置要使用的命令的连接。 SqlCommand command = new SqlCommand("SELECT * FROM MM410LIB.INVBAL WHERE INUMBR = 79610 AND ISTORE=14", this.cn);此外,您正在使用 ODBC,因此请尝试使用 OdbcDataReader DbReader = command.ExecuteReader();
  • 是的,我正在使用 ODBC 连接,并通过它与 AS400 DB2 连接。
  • 现在我需要改变什么,我应该使用这个:OdbcConnection odbcco = new OdbcConnection("SELECT * FROM MM410LIB.INVBAL WHERE INUMBR = 79610 AND ISTORE=14"); //OdbcConnection command = new OdbcConnection("SELECT * FROM MM410LIB.INVBAL WHERE INUMBR = 79610 AND ISTORE=14");
  • 我发布新答案并显示所有代码,请您检查并提出建议。
【解决方案2】:

在按钮点击事件中尝试这个 sql 命令

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

【讨论】:

  • 在此之前我需要通过用户名和密码添加此连接?
  • 是的,您还必须在 sqlserver goto serverobject -> linked servers 中的 sql 命令之前添加 sql 连接字符串,右键单击 ->newlinked Server 然后将目标服务器添加为链接服务器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-22
  • 2017-12-01
  • 1970-01-01
  • 2011-12-03
相关资源
最近更新 更多