在dataset中有两个表,如何对两张表进行关联,并用SQL语句进行查询?如不能,则如何用最简便的方法实现。(两张表有一个共同的字段,根据共同字段向每张表取一个字段形成一个查询结果,如果是物理表的话是很简单的,关键是没有物理表,是在DATASET里的内存表)

解决方案 »

  1.   

    DataSet.Relations 属性  
    获取用于将表链接起来并允许从父表浏览到子表的关系的集合。
    [Visual Basic]
    Public ReadOnly Property Relations As DataRelationCollection
    [C#]
    public DataRelationCollection Relations {get;}
    [C++]
    public: __property DataRelationCollection* get_Relations();
    [JScript]
    public function get Relations() : DataRelationCollection;
    属性值
    包含 DataRelation 对象的集合的 DataRelationCollection;否则为空值(如果不存在任何 DataRelation 对象)。
    示例
    [Visual Basic] 以下示例通过 Relations 属性打印所有子表的列名。
    [Visual Basic] 
    Private Sub PrintChildRelationRows()
       ' Declare variable to hold the row values.
       Dim strRowVals As String
       Dim myDataSet As DataSet
       ' Get the DataSet of a DataGrid that is displaying data of at least two
       ' tables.
       Dim myTable As DataTable = CType(DataGrid1.DataSource, DataTable)
       ' Navigate using the Relations.
       Dim myRel As DataRelation
       Dim row As DataRow
       Dim col As DataColumn
       ' Print the names of each column in each table through the Relations.
       For Each myRel In myDataSet.Relations
          For Each col in myRel.ChildTable.Columns
              strRowVals &= col.ColumnName & " "
          Next
        Next
        ' Display results.
        Console.WriteLine(strRowVals)
       End Sub
      

  2.   

    使用DataView,通过dv.Table.Columns.Add()来把两个不同表的字段添加,形成一个新的视图。
      

  3.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=54F4C732-AAE2-4135-FB1B-7B4B613BAA33
      

  4.   

    DataColumn parentCol;
        DataColumn childCol;
        // Code to get the DataSet not shown here.
        parentCol = DataSet1.Tables["Customers"].Columns["CustID"];
        childCol = DataSet1.Tables["Orders"].Columns["CustID"];
        // Create DataRelation.
        DataRelation relCustOrder;
        relCustOrder = new DataRelation("CustomersOrders", parentCol, childCol);
        // Add the relation to the DataSet.
        DataSet1.Relations.Add(relCustOrder);
      

  5.   

    知道怎么给分了!
    有没有更详细的说明啊?
    感谢simon8181() 、LoveCherry(最讨厌你们这些要代码的了,一点技术含量都没有)、wangrenda(浪人) 和其他支持关心的,我有一点点明白了,但还不是很清晰
      

  6.   

    DataSet有Relations属性的。添加关系就可以了。