Friday 14 October 2016

c# dataset


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DatasetDemo
{
    public partial class Form1 : Form
    {
        MedicalManager Manager { get; set; }

        public Form1()
        {
            InitializeComponent();

         
            Manager = new MedicalManager();

            uxData.DataSource = Manager.DataSet.Tables["Patient"];
            uxAppointments.DataSource = Manager.DataSet.Tables["Patient"];
            uxAppointments.DataMember = "FK_PatientAppointment";

            //format columns
            uxData.Columns["Phone"].Width = 150;
            uxData.Columns["HealthNumber"].Width = 150;
            uxData.Columns["HealthNumber"].HeaderText = "Health Number";
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Manager.SaveChanges();
        }
    }
}
----------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.IO;

namespace DatasetDemo
{
    public class MedicalManager
    {
        const string XmlDataFile = "MedicalClinic.xml";
        const string XmlSchemaFile = "MedicalClinic.xsd";

        public DataSet DataSet { get; private set; }

        public MedicalManager()
        {
            DataSet = new DataSet("MedicalClinic");

            //later I want to test if xml files exist...and if not create them
            if (File.Exists(XmlDataFile))
            {
                //read in the XSD file first then read in the xml file
                DataSet.ReadXmlSchema(XmlSchemaFile);
                DataSet.ReadXml(XmlDataFile);
            }
            else
            {
                CreateTables();
            }        
        }

        private void CreateTables()
        {
            CreatePatientTable();
            CreateAppointmentTable();
        }

        private void CreateAppointmentTable()
        {
            //create the Appointment table using the add method
            DataSet.Tables.Add("Appointment");

            //add the columns to this table
            DataSet.Tables["Appointment"].Columns.Add("Id", typeof(int));
            DataSet.Tables["Appointment"].Columns["Id"].AutoIncrement = true;
            DataSet.Tables["Appointment"].Columns["Id"].AutoIncrementSeed = 1;
            DataSet.Tables["Appointment"].Columns.Add("AppointmentDate", typeof(DateTime));
            DataSet.Tables["Appointment"].Columns.Add("Doctor", typeof(string));
            DataSet.Tables["Appointment"].Columns.Add("PatientId", typeof(int));
            DataSet.Tables["Appointment"].PrimaryKey =
                new DataColumn[] { DataSet.Tables["Appointment"].Columns["Id"] };

            //create the datarelation that defines the foreirn key constraint
            DataSet.Relations.Add("FK_PatientAppointment", 
                DataSet.Tables["Patient"].Columns["Id"],
                DataSet.Tables["Appointment"].Columns["PatientId"]);
            //nest appointment elements inside patient elements in the xml data file
            DataSet.Relations["FK_PatientAppointment"].Nested = true;

            //add test data
            DataSet.Tables["Appointment"].Rows.Add(new object[] { null, new DateTime(2016, 10, 2, 10, 15, 0), "Dr Jones", 1 });
            DataSet.Tables["Appointment"].Rows.Add(new object[] { null, new DateTime(2016, 10, 2, 10, 45, 0), "Dr Kennedy", 2 });
            DataSet.Tables["Appointment"].Rows.Add(new object[] { null, new DateTime(2016, 10, 3, 10, 15, 0), "Dr Jones", 3 });
            DataSet.Tables["Appointment"].Rows.Add(new object[] { null, new DateTime(2016, 10, 10, 8, 0, 0), "Dr Jones", 1 });
            DataSet.Tables["Appointment"].Rows.Add(new object[] { null, new DateTime(2016, 10, 10, 14, 30, 0), "Dr Kennedy", 2 });
        }

        private void CreatePatientTable()
        {
            //create the Patients table
            var table = new DataTable("Patient");

            //add the table to the dataset
            DataSet.Tables.Add(table);

            //create and add columns to the datatable
            var column = new DataColumn("Id", typeof(int));
            column.AutoIncrement = true;
            column.AutoIncrementSeed = 1;
            table.Columns.Add(column);

            column = new DataColumn("FirstName", typeof(string));
            column.MaxLength = 30;
            column.Caption = "First Name";
            table.Columns.Add(column);

            column = new DataColumn("LastName", typeof(string));
            column.MaxLength = 30;
            table.Columns.Add(column);

            column = new DataColumn("Phone", typeof(string));
            column.MaxLength = 14;
            table.Columns.Add(column);

            column = new DataColumn("HealthNumber", typeof(string));
            column.MaxLength = 10;
            column.Unique = true;
            table.Columns.Add(column);

            //set the primary key for the table
            table.PrimaryKey = new DataColumn[] { table.Columns["Id"] };

            //add test data
            var row = table.NewRow();
            row["FirstName"] = "John";
            row["LastName"] = "Doe";
            row["Phone"] = "1-403-555-6780";
            row["HealthNumber"] = "AB12345678";
            table.Rows.Add(row);

            row = table.NewRow();
            row["FirstName"] = "Jane";
            row["LastName"] = "Smith";
            row["Phone"] = "1-403-555-4152";
            row["HealthNumber"] = "AB12398745";
            table.Rows.Add(row);

            row = table.NewRow();
            row["FirstName"] = "Ken";
            row["LastName"] = "Hunter";
            row["Phone"] = "1-403-555-7800";
            row["HealthNumber"] = "AB98765432";
            table.Rows.Add(row);
        }

        public void SaveChanges()
        {
            DataSet.WriteXml(XmlDataFile);
            DataSet.WriteXmlSchema(XmlSchemaFile);
        }
    }
}

------------------------------------------------------
//MedicalClinic.xml


<?xml version="1.0" standalone="yes"?>
<MedicalClinic>
  <Patient>
    <Id>1</Id>
    <FirstName>John</FirstName>
    <LastName>Doe</LastName>
    <Phone>1-403-555-6780</Phone>
    <HealthNumber>AB12345678</HealthNumber>
    <Appointment>
      <Id>1</Id>
      <AppointmentDate>2016-10-02T10:15:00-06:00</AppointmentDate>
      <Doctor>Dr Jones</Doctor>
      <PatientId>1</PatientId>
    </Appointment>
    <Appointment>
      <Id>4</Id>
      <AppointmentDate>2016-10-10T08:00:00-06:00</AppointmentDate>
      <Doctor>Dr Jones</Doctor>
      <PatientId>1</PatientId>
    </Appointment>
  </Patient>
  <Patient>
    <Id>2</Id>
    <FirstName>Jane</FirstName>
    <LastName>Smith</LastName>
    <Phone>1-403-555-4152</Phone>
    <HealthNumber>AB12398745</HealthNumber>
    <Appointment>
      <Id>2</Id>
      <AppointmentDate>2016-10-02T10:45:00-06:00</AppointmentDate>
      <Doctor>Dr Kennedy</Doctor>
      <PatientId>2</PatientId>
    </Appointment>
    <Appointment>
      <Id>5</Id>
      <AppointmentDate>2016-10-10T14:30:00-06:00</AppointmentDate>
      <Doctor>Dr Kennedy</Doctor>
      <PatientId>2</PatientId>
    </Appointment>
  </Patient>
  <Patient>
    <Id>3</Id>
    <FirstName>Ken</FirstName>
    <LastName>Hunter</LastName>
    <Phone>1-403-555-7800</Phone>
    <HealthNumber>AB98765432</HealthNumber>
    <Appointment>
      <Id>3</Id>
      <AppointmentDate>2016-10-03T10:15:00-06:00</AppointmentDate>
      <Doctor>Dr Jones</Doctor>
      <PatientId>3</PatientId>
    </Appointment>
  </Patient>
  <Patient>
    <Id>4</Id>
    <FirstName>Sarah</FirstName>
    <LastName>Dunne</LastName>
    <Phone>1-403-555-8799</Phone>
    <HealthNumber>AB64578412</HealthNumber>
    <Appointment>
      <Id>7</Id>
      <AppointmentDate>2016-10-12T10:00:00-06:00</AppointmentDate>
      <Doctor>Dr Kennedy</Doctor>
      <PatientId>4</PatientId>
    </Appointment>
  </Patient>
</MedicalClinic>

------------------------------------------------------
//MedicalClinic xml schema


<?xml version="1.0" standalone="yes"?>
<xs:schema id="MedicalClinic" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="MedicalClinic" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Patient">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Id" msdata:AutoIncrement="true" msdata:AutoIncrementSeed="1" type="xs:int" />
              <xs:element name="FirstName" msdata:Caption="First Name" minOccurs="0">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="30" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="LastName" minOccurs="0">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="30" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="Phone" minOccurs="0">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="14" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="HealthNumber" minOccurs="0">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="10" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="Appointment" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Id" msdata:AutoIncrement="true" msdata:AutoIncrementSeed="1" type="xs:int" />
                    <xs:element name="AppointmentDate" type="xs:dateTime" minOccurs="0" />
                    <xs:element name="Doctor" type="xs:string" minOccurs="0" />
                    <xs:element name="PatientId" type="xs:int" minOccurs="0" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
    <xs:unique name="Appointment_Constraint1" msdata:ConstraintName="Constraint1" msdata:PrimaryKey="true">
      <xs:selector xpath=".//Appointment" />
      <xs:field xpath="Id" />
    </xs:unique>
    <xs:unique name="Constraint1">
      <xs:selector xpath=".//Patient" />
      <xs:field xpath="HealthNumber" />
    </xs:unique>
    <xs:unique name="Constraint2" msdata:PrimaryKey="true">
      <xs:selector xpath=".//Patient" />
      <xs:field xpath="Id" />
    </xs:unique>
    <xs:keyref name="FK_PatientAppointment" refer="Constraint2" msdata:IsNested="true">
      <xs:selector xpath=".//Appointment" />
      <xs:field xpath="PatientId" />
    </xs:keyref>
  </xs:element>
</xs:schema>

No comments:

Post a Comment