locked
Having Relationship with tables in dataset? RRS feed

  • Question

  • Hi,
    Is it possible to have relationship with tables in dataset?
    If so, How ? Expecting any example code please.
    Friday, September 9, 2011 12:58 PM

Answers

  • Sure.

    How? Check this exmple code:

           DataSet ds;
            public Form1()
            {
                InitializeComponent();
                ds = new DataSet();
                DataTable table1 = new DataTable("Orders");
                DataColumn[] columns = new DataColumn[] { new DataColumn("Number", typeof(int)),  //creating columns
                                                          new DataColumn("Time", typeof(DateTime)) };
                table1.Columns.AddRange(columns);  //adding columns to table
                table1.PrimaryKey = new DataColumn[] { table1.Columns[0] };  //setting primary key      
    
                DataTable table2 = new DataTable("Items");
                columns = new DataColumn[] { new DataColumn("Number_FK", typeof(int)), 
                                                          new DataColumn("Name", typeof(string)), 
                                                          new DataColumn("Quantity", typeof(int)) };
                table2.Columns.AddRange(columns);
    
                ds.Tables.AddRange(new DataTable[] { table1, table2 }); //adding tables to dataSet
                DataRelation relation = new DataRelation("OrdersItems", table1.Columns[0], table2.Columns[0]);  //creating a relation between these tow tables
                ds.Relations.Add(relation); //adding (actually using) the relation in the dataSet
           }
    

     

    I hope this simple example code explains all what is about relations (creating them) between dataTables in a dataSet.


    Mitja
    • Marked as answer by parote hitachi Friday, September 9, 2011 1:07 PM
    Friday, September 9, 2011 1:01 PM
  • Yes, you can have relationship between tables in DataSet.

    DataSet is a very strong and comprehensive object that exposes a lot of functionality that many are not aware about. Most of us just know and use it as a holder of data from database or getting multiple result sets from database and hold in it.

    Look at the code snippet to create relationship here.

    http://www.dotnetfunda.com/interview/exclusive/x874-how-can-we-add-relation-between-tables-in-a-dataset-.aspx
    http://msdn.microsoft.com/en-us/library/ay82azad(v=vs.80).aspx
    Pravin Arote, MCTS - Web Technologies. If reply answers your question, Please mark as Answer :)
    • Proposed as answer by Pravin S Arote Friday, September 9, 2011 1:06 PM
    • Marked as answer by parote hitachi Friday, September 9, 2011 1:07 PM
    Friday, September 9, 2011 1:06 PM

All replies

  • Sure.

    How? Check this exmple code:

           DataSet ds;
            public Form1()
            {
                InitializeComponent();
                ds = new DataSet();
                DataTable table1 = new DataTable("Orders");
                DataColumn[] columns = new DataColumn[] { new DataColumn("Number", typeof(int)),  //creating columns
                                                          new DataColumn("Time", typeof(DateTime)) };
                table1.Columns.AddRange(columns);  //adding columns to table
                table1.PrimaryKey = new DataColumn[] { table1.Columns[0] };  //setting primary key      
    
                DataTable table2 = new DataTable("Items");
                columns = new DataColumn[] { new DataColumn("Number_FK", typeof(int)), 
                                                          new DataColumn("Name", typeof(string)), 
                                                          new DataColumn("Quantity", typeof(int)) };
                table2.Columns.AddRange(columns);
    
                ds.Tables.AddRange(new DataTable[] { table1, table2 }); //adding tables to dataSet
                DataRelation relation = new DataRelation("OrdersItems", table1.Columns[0], table2.Columns[0]);  //creating a relation between these tow tables
                ds.Relations.Add(relation); //adding (actually using) the relation in the dataSet
           }
    

     

    I hope this simple example code explains all what is about relations (creating them) between dataTables in a dataSet.


    Mitja
    • Marked as answer by parote hitachi Friday, September 9, 2011 1:07 PM
    Friday, September 9, 2011 1:01 PM
  • Yes, you can have relationship between tables in DataSet.

    DataSet is a very strong and comprehensive object that exposes a lot of functionality that many are not aware about. Most of us just know and use it as a holder of data from database or getting multiple result sets from database and hold in it.

    Look at the code snippet to create relationship here.

    http://www.dotnetfunda.com/interview/exclusive/x874-how-can-we-add-relation-between-tables-in-a-dataset-.aspx
    http://msdn.microsoft.com/en-us/library/ay82azad(v=vs.80).aspx
    Pravin Arote, MCTS - Web Technologies. If reply answers your question, Please mark as Answer :)
    • Proposed as answer by Pravin S Arote Friday, September 9, 2011 1:06 PM
    • Marked as answer by parote hitachi Friday, September 9, 2011 1:07 PM
    Friday, September 9, 2011 1:06 PM
  • Thanks Pravin.

    Providing such a good knowledge base.

    Friday, September 9, 2011 1:06 PM