代码之家  ›  专栏  ›  技术社区  ›  Jonathan Wood

连接不同数据库中的表(ADO.NET)

  •  -1
  • Jonathan Wood  · 技术社区  · 7 年前

    我有两个数据库,每个数据库都有一个UDL连接字符串。我想构造一个连接两个数据库中的表的查询。

    我正在从一个数据库中进行这样的查询。

    string query = "...";
    
    using (OleDbConnection connection = new OleDbConnection(ConnectionString))
    using (OleDbCommand command = new OleDbCommand(query, connection))
    {
        int count = command.ExecuteNonQuery();
    }
    

    有人能给我举个例子,用一个连接两个数据库中的表的查询来做同样的事情吗?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Jonathan Wood    7 年前

    所以,让我感到困惑的是我需要两个连接字符串。在ADO.NET中似乎没有任何方法可以构造一个包含两个连接的查询。

    SELECT [DB1].[dbo].Field1, [DB2].[dbo].Field2 FROM ...
    
        2
  •  0
  •   Cleber Machado    7 年前

    var results = from par in dt1.AsEnumerable()
                    join chi in dt2.AsEnumerable()
                      on (int)par["ID"] equals (int)chi["ParentID"]
                    select new //Here you can leave it that way or use your own object.
                               // select new MyResultObject(){prop1 = x, prop2 = y ...}
                    {
                        ParentID = (int)par["ParentID"],
                        ChildID = (int)par["ChildID"],
                        ColA = (string)par["ColA"],
                        ColB = (int)par["ColB"],
                        ColC = (double)chi["ColC"],
                        ColD = (date)chi["ColD"]
                    };