Relation between DataTables

Hi,

< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 

I have Order and OrderDetail tables in my database.

 

Can I create a DataSet based on the two tables in such a way that whenever I fill the Order table, OrderDetail table also automatically get filled by related order detail items

 

Thank you,

Alan

 



Answer this question

Relation between DataTables

  • BikramS

    You will create you dataset by adding the two tables and their relation and then create two DataAdapters that will fill the dataset with the data for the two tables. You can  not fill the dataset with a single command.
    here is the code in vb



    dim mydataset = new dataset
    dim myAdapter1 as new sqldataadapter("select * from order where orderID=6")
    myAdapter1.fill(mydataset.tables("order"))
    dim myAdapter2 as new sqldataadapter("select * from orderdetail where orderID=6")
    myAdapter2.fill(mydataset.tables("orderdetail"))

     



  • jjparker

    Hi,


    I think what you need is to set a relationship in the dataset upon filling the two datatables. Here is a pseudo code on how to achieve that.


    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Orders; SELECT * FROM OrderDetails;", myActiveConnection);
    da.Fill(ds);
    // This would result on two tables being filled by the Order and Order Details Values
    // Set the relationships
    ds.Relations.Add("rel1", ds.Tables[0]["Id"], ds.Tables[1]["OrderId"]);


    Hope this helps, here is additional info on how to add a relationship on a Dataset. http://msdn2.microsoft.com/en-us/library/ay82azad



    cheers,

    Paul June A. Domag

  • Relation between DataTables