(C# Coding) DataSet & XML, Database Programming

C# & .Net Framework 

 

What to do ?

 

To save the information as below, create a table with C# code and save it as a file (Xml file). 

 

[Student]

 ID

 Name 

 Phone Number

 

 0

 ABC

 000-111-2222

 

 1

 DEF

 000-111-3333

 

 2

 GHI

 000-111-4444

 

 

 

 

 

 

 

 

 

______________________________________________________________________________________________

1. Creating Table in Memory and Configuring Fields

  

Create the table below using C# code and save it as an XML file

 

field name

 

Caption

in XML File

ID

PK, Auto Increment

 

Attribute

Name

String

Student Name

Attribute

HP

String

Phone Number

 

 

 

 

 

 

 

 

First, declare the DataSet and DataTable variables.

 

 

 

public partial class Form1 : Form

{

 

    DataSet mDataSet = new DataSet ("DataSet");

    DataTable mStudentTable = new DataTable ("Student"); 

 

 

 

 

Above, the name of the DataSet is "DataSet" and the name of the student table is "Student".

 

-> is DataSet.Name,  DataTable.Name property value

 

 

 

In the constructor, a DataTable is added to the DataSet and a DataColumn is configured.

 

 

 

public partial class Form1 : Form

{

 

   DataSet mDataSet = new DataSet ("DataSet");

   DataTable mStudentTable = new DataTable ("Student");

 

 

    public Form1 ()

    {

 

      // Add DataTable to DataSet

   →    mDataSet.Tables.Add (mStudentTable);                                

 

 

       DataColumn column;

 

       // ID (Attribute)

   →    column = mStudentTable.Columns.Add ("ID", typeof(int));            

       →    column.ColumnMapping = MappingType.Attribute;               

 

       // Name (Attribute, "Student Name")

    →    column = mStudentTable.Columns.Add ("Name", typeof(string));       

    →    column.ColumnMapping = MappingType.Attribute;         

    →    column.Caption = "Student Name";                                                

 

        // HP ("Phone Number")

    →    column = mStudentTable.Columns.Add ("HP", typeof(string));     

        column.Caption = "Phone Number";                               

 

 

 

 

When creating a column above, the field to be mapped as an attribute in the XML file and the

.Caption property were also specified.

 

Just because the .Caption property is specified, when mapping to DataGrid, etc., the .Caption value is

not automatically applied to the column text value of the DataGrid, etc., and it is more meaningful

that the value to be used as the display string for the column is stored in the .Caption property.

 

Now the table has been created in memory and the columns are configured.

 

In addition, it specifies more detailed properties required for the required column.

( Also includes specifying the Primary Key field (Column) )

   

 

 

public partial class Form1 : Form

{

 

   DataSet mDataSet = new DataSet ("DataSet");

   DataTable mStudentTable = new DataTable ("Student");

 

 

    public Form1 ()

    {

 

      // Add DataTable to DataSet

      mDataSet.Tables.Add (mStudentTable);                                

 

 

       DataColumn column;

 

       // ID (Attribute)

       column = mStudentTable.Columns.Add ("ID", typeof(int));            

         column.ColumnMapping = MappingType.Attribute;               

 

       // Name (Attribute, " Student Name")

       column = mStudentTable.Columns.Add ("Name", typeof(string));       

       column.ColumnMapping = MappingType.Attribute;         

       column.Caption = " Student Name";                                                

 

        // HP ("Phone Number ")

       column = mStudentTable.Columns.Add ("HP", typeof(string));     

       column.Caption = "Phone Number";         

 

 

       // ID  ( AutoNumber, Key field )

      mStudentTable.Columns["ID"].Unique = true;        

      mStudentTable.Columns["ID"].ReadOnly = true;    

      mStudentTable.Columns["ID"].AllowDBNull = false;   

 

      mStudentTable.Columns["ID"].AutoIncrement = true;  

      mStudentTable.Columns["ID"].AutoIncrementSeed = 1 

      mStudentTable.Columns["ID"].AutoIncrementStep = 1;

 

       // Name : NN

      mStudentTable.Columns["Name"].AllowDBNull = false;   

 

       // Specify the Primary Key field (column)

       // ------------------------------------------------------------------------

      mStudentTable.PrimaryKey = new DataColumn[]

       {

          mStudentTable.Columns[0]

       };

 

 

 

 

 

Specify the Primary Key field

The method of specifying the Primary Key field is the process of specifying the value of the .PrimaryKey

property of the DataTable as in the code above, and the Type of the PrimaryKey property is an array of

DataColumn.

 

That is, in the case of a multi-key table, you can write the fields to be used as the key in order, and in

the case of a single key, you only need to write one field to be used as the key field. 

  

 

 

Data table structure created using C# code is,

 

 Field name

 Filed Type

 Caption

 in XML File

 ID

 PK, Auto Increment

 

 Attribute

 Name

 String

 Student Name

 Attribute

 HP

 String

 Phone Number

 

 

 

 

 

 

 

 

 

 

______________________________________________________________________________________________

2-1. Connect to Grid, input data and save as XML file

 

 

DataGridView is used to input data to a table implemented in memory.

 

 

 

public partial class Form1 : Form

{

 

    DataSet mDataSet = new DataSet ("DataSet");

    DataTable mStudentTable = new DataTable ("Student");

 

 

    public Form1 ()

    {

 

        // Add DataTable to DataSet

        mDataSet.Tables.Add (mStudentTable);                                

 

 

        DataColumn column;

 

        // ID (Attribute)

         column = mStudentTable.Columns.Add ("ID", typeof(int));            

          column.ColumnMapping = MappingType.Attribute;               

 

        // Name (Attribute, "Student Name")

          column = mStudentTable.Columns.Add ("Name", typeof(string));       

        column.ColumnMapping = MappingType.Attribute;         

        column.Caption = "Student Name";                                                

 

        // HP ("Phone Number")

          column = mStudentTable.Columns.Add ("HP", typeof(string));     

        column.Caption = "Phone Number";         

 

 

        // ID  ( AutoNumber, Key field )

        mStudentTable.Columns["ID"].Unique = true;        

        mStudentTable.Columns["ID"].ReadOnly = true;    

        mStudentTable.Columns["ID"].AllowDBNull = false;   

 

        mStudentTable.Columns["ID"].AutoIncrement = true;  

        mStudentTable.Columns["ID"].AutoIncrementSeed = 1 

        mStudentTable.Columns["ID"].AutoIncrementStep = 1;

 

        // Name : NN

        mStudentTable.Columns["Name"].AllowDBNull = false;   

 

 

        // Specify the Primary Key field (column)

        // ------------------------------------------------------------------------

        mStudentTable.PrimaryKey = new DataColumn[] { mStudentTable.Columns[0] };

 

 

        // Connect to DataGridView

         this.dgvStudent.DataSource = mStudentTable.DefaultView;

 

        // Grid Rows Height +=

        // Using .RowTemplate.Height has the effect of specifying all future Row.Height

        // values.

            this.dgvStudent.RowTemplate.Height += 5;

 

         // Specify Grid Column Caption

           foreach (DataGridViewColumn dgvc in dgvStudent.Columns)

         {

            dgvc.HeaderText = mDataSet.Tables["Student"].Columns[dgvc.Index].Caption;

         }

 

 

         // Specify width for each column

         // Check the screen for each column and designate

       dgvStudent.Columns[1].Width += 10;

       dgvStudent.Columns[3].Width += 70;

 

 

 

 

After connecting to the DataGridView,

 

Increase the default Row Height of DataGrdView slightly

The height of the Grid Row that is assigned by default is just the right height for the font height,

so increase it a little.

Using .RowTemplate.Height has the effect of specifying all future Row.Height values.

 

this.dgvStudent.RowTemplate.Height += 5;

 

( Since the specified value of 5 above is a pixel value, it is necessary to recalculate the DPI. )

 

 

Specify Text value of Column Header of DataGridView

Specify the Column Header Text value of the DataGridView using the value specified in the Column

Caption property of the DataTable.

( DataColumn.Caption,  DataGridViewColumn.HeaderText )

 

 

Also, adjusted the width of each column appropriately.

Adjust while checking on the screen.

 

 

Now, if you run the program, input data, and use the Save as XML file function supported by DataSet,

you can save it as an XML file.

 

DataTable also provides save and read functions as XML files, so each table can be saved as an XML file.

 

 

 

private void btnSaveToXmlFile_Click (object sender, EventArgs e)

{

→   mDataSet.WriteXml (@"c:\work\xml database.xml");

    MessageBox.Show ("Done");

}

 

 

 

Schema can also be saved as follows.

 

 

 

private void btnSaveToXmlFile_Click (object sender, EventArgs e)

{

→   mDataSet.WriteXml (@"c:\work\xml database.xml", XmlWriteMode.WriteSchema);

    MessageBox.Show ("Done");

}

 

 

 

 

Result

 

______________________________________________________________________________________________________

c:\work\xml database.xml

 

<?xml version="1.0" standalone="yes"?>

<DataSet>

  <Student ID="1" Name="ABC">

    <HP>000-1111-2222</HP>

  </Student>

  <Student ID="3" Name="DEF">

    <HP>000-2222-2222</HP>

  </Student>

</DataSet>

 

______________________________________________________________________________________________________

 

 

 

If you save the schema together,

 

______________________________________________________________________________________________________

c:\work\xml database.xml

 

<?xml version="1.0" standalone="yes"?>

<DataSet>

  <xs:schema id="DataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

    <xs:element name="DataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">

      <xs:complexType>

        <xs:choice minOccurs="0" maxOccurs="unbounded">

          <xs:element name="Student">

            <xs:complexType>

              <xs:sequence>

                <xs:element name="HP" type="xs:string" minOccurs="0" msdata:Ordinal="3" />

              </xs:sequence>

              <xs:attribute name="ID" msdata:ReadOnly="true" msdata:AutoIncrement="true" msdata:AutoIncrementSeed="1" type="xs:int" use="required" />

              <xs:attribute name="Name" msdata:Caption="Student Name" type="xs:string" use="required" />

            </xs:complexType>

          </xs:element>

        </xs:choice>

      </xs:complexType>

      <xs:unique name="Constraint1">

        <xs:selector xpath=".//Student" />

        <xs:field xpath="@ID" />

      </xs:unique>

    </xs:element>

  </xs:schema>

  <Student ID="1" Name="ABC">

    <HP>000-1111-2222</HP>

  </Student>

  <Student ID="3" Name="DEF">

    <HP>000-2222-2222</HP>

  </Student>

</DataSet>

 

______________________________________________________________________________________________________

 

<xs:schema >  </xs:schema> is added to the front of the file.

 

 

______________________________________________________________________________________________________

 

The skills of handling DataSet and saving them as XML files become the basic skills for handling

and storing information in any program.

 

 

 

 

 

 

____________________________________________________________________________________________________________

2-2. Instead of using Grid Control for data entry, you can also do it

   using C# code.

 

 

public partial class Form1 : Form

{

 

   DataSet mDataSet = new DataSet ("DataSet");

   DataTable mStudentTable = new DataTable ("Student");

 

 

   public Form1 ()

   {

 

      // Add DataTable to DataSet

      mDataSet.Tables.Add (mStudentTable);                                

 

 

      DataColumn column;

 

      // ID (Attribute)

       column = mStudentTable.Columns.Add ("ID", typeof(int));            

      column.ColumnMapping = MappingType.Attribute;               

 

      // Name (Attribute, "Student Name")

      column = mStudentTable.Columns.Add ("Name", typeof(string));       

      column.ColumnMapping = MappingType.Attribute;         

      column.Caption = "Student Name";                                                

 

      // HP ("Phone Number")

      column = mStudentTable.Columns.Add ("HP", typeof(string));     

      column.Caption = "Phone Number";         

 

 

      // ID  ( AutoNumber, Key field )

      mStudentTable.Columns["ID"].Unique = true;        

      mStudentTable.Columns["ID"].ReadOnly = true;    

      mStudentTable.Columns["ID"].AllowDBNull = false;   

 

      mStudentTable.Columns["ID"].AutoIncrement = true;  

      mStudentTable.Columns["ID"].AutoIncrementSeed = 1 

      mStudentTable.Columns["ID"].AutoIncrementStep = 1;

 

      // Name : NN

      mStudentTable.Columns["Name"].AllowDBNull = false;   

 

 

      // Specify the Primary Key field (column)

      // ------------------------------------------------------------------------

      mStudentTable.PrimaryKey = new DataColumn[] { mStudentTable.Columns[0] };

 

 

      // Add data using C# code

      DataRow nrow = mStudentTable.NewRow();

      nrow["Name"] = "ABC";

      nrow["HP"] = "000-111-2222";

      mStudentTable.Rows.Add(nrow);

 

 

 

 

 

 

 

 

______________________________________________________________________________

 

System Internals

System Internals (developer edition)

 

 

 

 

Popular posts from this blog

CPU Architecture & Authority Control

Known DLLs

lua command (EnableLUA)