More than one Relations : DataRelation « Database ADO.net « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Database ADO.net » DataRelation 




More than one Relations



using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

class Program {
    static void Main(string[] args) {
        SqlConnection thisConnection = new SqlConnection(@"Server=(local)\sqlexpress;Integrated Security=True;" +
             "Database=northwind");

        DataSet thisDataSet = new DataSet();
        SqlDataAdapter custAdapter = new SqlDataAdapter("SELECT * FROM Customers", thisConnection);
        custAdapter.Fill(thisDataSet, "Customers");

        SqlDataAdapter orderAdapter = new SqlDataAdapter("SELECT * FROM Orders", thisConnection);
        orderAdapter.Fill(thisDataSet, "Orders");

        SqlDataAdapter detailAdapter = new SqlDataAdapter("SELECT * FROM [Order Details]", thisConnection);
        detailAdapter.Fill(thisDataSet, "Order Details");

        SqlDataAdapter prodAdapter = new SqlDataAdapter("SELECT * FROM Products", thisConnection);
        prodAdapter.Fill(thisDataSet, "Products");

        DataRelation custOrderRel = thisDataSet.Relations.Add("CustOrders",
                    thisDataSet.Tables["Customers"].Columns["CustomerID"],
                    thisDataSet.Tables["Orders"].Columns["CustomerID"]);

        DataRelation orderDetailRel = thisDataSet.Relations.Add("OrderDetail",
                    thisDataSet.Tables["Orders"].Columns["OrderID"],
                    thisDataSet.Tables["Order Details"].Columns["OrderID"]);

        DataRelation orderProductRel = thisDataSet.Relations.Add(
          "OrderProducts", thisDataSet.Tables["Products"].Columns["ProductID"],
           thisDataSet.Tables["Order Details"].Columns["ProductID"]);

        foreach (DataRow custRow in thisDataSet.Tables["Customers"].Rows) {
            Console.WriteLine("Customer ID: " + custRow["CustomerID"]);

            foreach (DataRow orderRow in custRow.GetChildRows(custOrderRel)) {
                Console.WriteLine("\tOrder ID: " + orderRow["OrderID"]);
                Console.WriteLine("\t\tOrder Date: " + orderRow["OrderDate"]);

                foreach (DataRow detailRow in
                         orderRow.GetChildRows(orderDetailRel)) {
                    Console.WriteLine("\t\tProduct: " +
                    detailRow.GetParentRow(orderProductRel)["ProductName"]);
                    Console.WriteLine("\t\tQuantity: " + detailRow["Quantity"]);
                }
            }
        }
        thisConnection.Close();
    }
}


           
       














Related examples in the same category
1.Set up DataRelation
2.Setting the Nested property of a DataRelation to true
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.