两个数据库:A,B;
数据库A中的表T有字段t1,t2,tf;
数据库B中的表F有字段tf,f2,f3;
(根据tf可以把两个表联合起来)已知:绑定A中表T的数据如下:string sql = "SELECT * from T";
DataSet ds=  SqlHelper.ExecuteAdapter(SqlHelper.ConnA, CommandType.Text, sql, null);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();绑定B中表F的数据如下:string sql = "SELECT * from F";
DataSet ds=  SqlHelper.ExecuteAdapter(SqlHelper.ConnB, CommandType.Text, sql, null);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();现在要把他们联合起来到一个DataSet里面,该怎么办?

解决方案 »

  1.   

    向DataSet填充多个数据表:
    http://www.cnblogs.com/notus/archive/2006/06/26/435851.html
    填充DataSet数据集的几种方式 :
    http://www.cnblogs.com/liulanglang/archive/2007/07/08/810019.html
      

  2.   

    string sql = "SELECT A.*,B.f1,b.f2 from A,B where A.tf=B.tf";
    DataSet ds=  SqlHelper.ExecuteAdapter(SqlHelper.ConnB, CommandType.Text, sql, null);
    GridView1.DataSource = ds.Tables[0].DefaultView;
    GridView1.DataBind();
      

  3.   

    --改为一个联合查询,然后绑定!
    select a.t1,a.t2,a.tf,b.f2,b.f3 from a..t a,b..f b where a.tf=b.tf
      

  4.   


    不对呀,你们没看清题目,是两个数据库的两张表,连接第一个数据库用ConnA,连接第二个 数据库用ConnB,用一个SQL语句肯定是不行!
      

  5.   

    或者用一个视图:create view viewname as
    select a.t1,a.t2,a.tf,b.f2,b.f3 from a..t a,b..f b where a.tf=b.tf
      

  6.   

    不行吧,查T表要用连接ConnA,查F表要用连接字符ConnB,写到一起要怎么执行ExecuteAdapter?
      

  7.   


    上线后可能是放在两台上,访问数据库A要用连接字符串ConnA,访问数据库B要用连接字符串ConnB。
      

  8.   

    这样,你在你的ConnA里,访问一下数据库B的表看能不能查询出来,不就可以了吗?
      

  9.   

    前面说的是放在一台上,如果放在2台上的话,你要用链接服务器。
    参考一下这个:
    http://blog.csdn.net/sdhdy/archive/2009/05/25/4215311.aspx
      

  10.   

    因为两个Coon里面指向不同的数据库,用不同的用户名和密码