[Home] DataSet Usage


//********************************************************/
//   Description: usage of DataSet class
//********************************************************/
//
//1. From the File select New -> Project.
//2. Select template C# -> Console Application.
//3. Name project as DataSetosu.
//4. Insert the following code.
//********************************************************/

using System;
using System.Data;

namespace DataSetosu
{
   class Start
   {
     static void Main(string[] args)
     {
       //initializing a new instance of DataSet class
       DataSet dts = new DataSet("Example");
       //initializing a new instance of DataTable class
       DataTable tblProjects = new DataTable();
       DataTable tblStages = new DataTable();
       //initializing a new instance of DataColumn class
       DataColumn col = new DataColumn();


       //************* Projects ***************
       //creating table Projects

       tblProjects = dts.Tables.Add("Projects");
       //creating ID column:
       col = tblProjects.Columns.Add("ID", typeof(Int32));
       //setting ID column as primary key:
       tblProjects.PrimaryKey = new DataColumn[] {col};

       //creating ProjectName column
       col = tblProjects.Columns.Add("ProjectName", typeof(string));
       //creating ConnString column
       col = tblProjects.Columns.Add("Description", typeof(string));
       //****************************************


       //*************** Stages ****************
       //creating table Stages

       tblStages = dts.Tables.Add("Stages");
       //creating ID column:
       col = tblStages.Columns.Add("ID", typeof(Int32));
       //setting ID column as primary key:
       tblStages.PrimaryKey = new DataColumn[] {col};

       //creating StageName column
       col = tblStages.Columns.Add("StageName", typeof(string));
       //creating StageDescription column
       col = tblStages.Columns.Add("StageDescription", typeof(string));
       //creating ProjectID column
       col = tblStages.Columns.Add("ProjectID", typeof(Int32));
       //****************************************

       //add a relation between Projects.ID and Stages.ProjectID:

       DataRelation rel = dts.Relations.Add("ProjectStages", tblProjects.Columns["ID"], tblStages.Columns["ProjectID"]);
      
       //to see XML Schema use:
       Console.WriteLine(dts.GetXmlSchema());




       //***** inserting some data: ****************
       object[] arr = new Object[3]{1, "test", "test description"};

       tblProjects.Rows.Add (arr);

       arr[0] = 2;
       arr[1] = "alt test";
       arr[2] = "alt description";
       tblProjects.Rows.Add (arr);
       //****************************************

       //to see inserted data as XML use:
       //Console.WriteLine(dts.GetXml ());

       //to see inserted data:

       foreach(DataRow row in tblProjects.Rows)
       {
          Console.WriteLine (String.Concat (row["ProjectName"], " - ", row["Description"]));
       }
      
          Console.ReadLine ();
      }
   }
}