如何获取数据C#中两个基于服务的sql server

时间:2018-01-19 06:58:04

标签: c# asp.net entity-framework entity-framework-6

我必须将数据从一个基于服务的sql server转发到另一个基于服务的sql server

select * from servernameA.db_name.dbo.table_name 
where column in (select column from servernameb.db_name.dbo.table_name)

1 个答案:

答案 0 :(得分:0)

那么问题是什么?你可以做到。

使用DISTINCT在您的查询中稍微改善一点性能。

select * from servernameA.db_name.dbo.table_name 
where column in (select DISTINCT column from servernameb.db_name.dbo.table_name)

供您参考:
Query across multiple databases on same server
Selecting data from two different servers in SQL Server

<强> ------------ ----------------编辑
这个怎么样?我还没试过这个。试一试。

// connectionString is the connection string for the ServerA..DB1
using (var conn = new SqlConnection(connectionString))
{
     conn.Open();
     // provide commandText as the SQL query having the full name of Server2..DB2
     using (var command = new SqlCommand(commandText, conn))
     {
         using (var reader = command.ExecuteReader())
         {
              while(reader.Read())
              {
                  // read the result
              }
         }
     }
}