我在DataSet中有三张表,
table1(tb1_id,name,...) 
table2(tb2_id,name,...)
table3(tb1_id,tb2_id)要根据tb2_id查询出table1中关联的数据,(table2.name,table1.tb1_id,table1.name,.....)应该怎么实现?

解决方案 »

  1.   

    首先两个表要建立外键索引关系。
    然后用DataRow.GetChildRows方法来获得相关数据
      

  2.   

    select tb1_id,name from table1 where tb1_id in (select tb1_id from table3 where tb2_id=@tb2_id)
      

  3.   

    怎么不在数据库中直接查询好,然后放在DataSet的一个DataTable中?
      

  4.   

    1。建立两个 DataTable 对象之间的父/子关系
      DataColumn parentCol;
        DataColumn childCol;
        parentCol = DataSet1.Tables["Customers"].Columns["CustID"];
        childCol = DataSet1.Tables["Orders"].Columns["CustID"];
        DataRelation relCustOrder;
        relCustOrder = new DataRelation("CustomersOrders", parentCol, childCol);
        DataSet1.Relations.Add(relCustOrder);
    2。获得相关数据
    private void GetChildRowsFromDataRelation(DataTable myTable ) {
        DataRow[] arrRows;  
        foreach(DataRelation myRelation in myTable.ChildRelations){
           foreach(DataRow myRow in myTable.Rows){
              arrRows = myRow.GetChildRows(myRelation, DataRowVersion.Proposed);
              // Print values of rows.
              for(int i = 0; i < arrRows.Length; i++){
                 foreach(DataColumn myColumn in myTable.Columns){
                    Console.WriteLine(arrRows[i][myColumn]);
                 }
              }
           }
        }
     }
      

  5.   

    wh8254(为了俺老婆,努力写程序) 说的没错啊,在sql里写好连接查询,直接填充到dataset里就可以了,多方便啊。