Wrox Home  
Search P2P Archive for: Go

  Return to Index  

ado_dotnet thread: Re: Accessing Relations


Message #1 by "Fredrik Normén" <fnormen@h...> on Tue, 18 Dec 2001 13:09:14
Hi, 



What I know, you don't use the DataRelation class to iterate through the 

ralations of the joins you have in the SQL-query. You use the 

DataRelation class to create a realation between two DataTables in a 

DataSet and use the Datarealation to iterate through that realation.



Here is an example:



private void myRelation()

{

    DataColumn parentCol;

    DataColumn childCol;



    // select * from Customers

    da.Fill(myDataSet, "Customers");



    // select * from Orders

    da.Fill(myDataSet, "Orers");



    parentCol = myDataSet.Tables["Customers"].Columns["CustID"];

    childCol = myDataSet.Tables["Orders"].Columns["CustID"];



    // Create DataRelation.

    DataRelation relCustOrder;

    relCustOrder = new DataRelation("CustomersOrders", parentCol, 

childCol);



    // Add the relation to the DataSet.

    myDataSet.Relations.Add(relCustOrder);



    foreach (DataRow custRow in myDataSet.Tables["Customers"].Rows)

    {

        Console.WriteLine(custRow["CustomerID"]);

        foreach (DataRow orderRow in custRow.GetChildRows(custOrderRel))

           Console.WriteLine(orderRow["OrderID"]);

    }

}



/Fredrik Normén





> Hi,

> 

> Given any two tables in a database, I have to determine the common key

> between them in order to perform a join.

> 

> I have tried using a SqlDataAdaptor to populate a DataSet and then 

iterate

> through the relations, but 'Return' is always blank. All the material 

I've

> seen on the web or in books only talks about establishing these

> relationships and not reading them. Is this not possible with ADO.NET?

> 

> Will I have to use a COM wrapper for SQL-DMO to perform this instead?

> 

> Thanks in advance. Code follows.

> 

>       String Return = "";

> 

>       SqlDataAdapter da = new SqlDataAdapter

>       (

>         "select * from products" ,

>         "server=FUDGE;database=Northwind;Trusted_Connection=yes"

>       );

> 

>       DataSet ds = new DataSet();

> 

>       da.Fill(ds, "products");

> 

>       foreach (DataTable t in ds.Tables)

>       {

>         foreach (DataRelation r in t.ChildRelations)

>         {

>           Return += r.RelationName + "|";

>         }

>       }

> 

> Akshay Luther

> 


  Return to Index