бредущий по жизни (svoysredychuzih) wrote in aspdotnet,
бредущий по жизни
svoysredychuzih
aspdotnet

Using DataRelation in DataSet

Hi everybody!
This is the question about how to use DataRelation object.
Let's say I have 2 tables:
SELECT customerId, customerName FROM customers
and
SELECT orderId, customerId, orderName FROM orders
which are related by customerId.

Assume I have method

getDataSet(sqlTextArray)

which returns me the DataSet based on array of queries sqlTextArray

So, then I can write:
String q1 = 
   "SELECT c.customerName, r.orderId, r.orderName "+
   "FROM customers AS c, orders as r "+
   "WHERE (c.ustomerId = r.customerId) ";
String[] q = new String[] { q1 };

DataSet d = getDataSet(q);
DataTable t = d.Tables[0];
for (int i=0; i < t.Rows.Count; i++) {
   String s = t.Rows[i]["customerName"] + ", " + 
      t.Rows[i]["orderId"] + ", " + t.Rows[i]["orderName"] + ".";
   Console.WriteLine(s);
}

This part is clear for me. Then, I want to achieve the same result by using
DataRelation. In this case I should write something like this:
String[] q = new String[] { 
   "SELECT customerId, customerName FROM customers", 
   "SELECT orderId, customerId, orderName FROM orders" 
};
DataSet d = getDataSet(q);
d.Relations.Add(new DataRelation("customer2order",
   d.Tables[0].Columns["customerId"],
   d.Tables[1].Columns["customerId"]));

How to print out the combined data as I did that in first example?
  • Post a new comment

    Error

    default userpic
  • 0 comments