แสดงบทความที่มีป้ายกำกับ C# แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ C# แสดงบทความทั้งหมด

วันจันทร์ที่ 15 ธันวาคม พ.ศ. 2551

การสร้างและใช้งาน Collection.Generic

Generic เป็นเครื่องมือที่ช่วยให้ผู้พัฒนา Collection Class สามารถนำ Class ที่สร้างไว้นำกลับมาใช้งานได้ง่ายขึ้นโดยไม่จำเป็นต้องคำนึงถึงชนิดของตัวแปรที่บรรจุอยู่ใน Collection Class เหล่านั้นในตอนออกแบบ แต่จะทำการกำหนดชนิดของตัวแปรให้กับ Class เมื่อจะนำ Class เหล่านั้นไปใช้งาน

ตัวอย่าง
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication9
{
     // กำหนดตัวแปรเป็นชนิด T ภายใต้เครื่องหมาย "< >"
    public class myCollection<T>
    {
         // นำชนิดตัวแปรนั้นมาใช้งานเช่นเดียวกับชนิดตัวแปรอื่นๆ
        T[] myStack = new T[50];

        public T this[int idx]
        {
            get
            {
                return myStack[idx];
            }
            set
            {
                myStack[idx] = value;
            }
        }

    }

    public class Student
    {
        private String _std;

        public Student(String name)
        {
            _std = name;
        }
        public override string ToString()
        {
            return _std;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // เมื่อนำมาใช้งาน ให้กำหนดชนิดของตัวแปรตามที่ต้องการ ในที่นี้ให้แทนด้วย Class Student ที่ได้กำหนดไว้ (อาจกำหนดเป็น String, Int, ... ก็ได้แล้วแต่จะใช้)
            myCollection<Student>             mCollec = new             myCollection<Student>();

            //หลังจากที่กำหนดเรียบร้อยแล้ว จะสามารถนำตัวแปรนั้นมาใช้งานได้ทันที โดยชนิดของตัวแปรนั้นจะเป็นไปตามที่กำหนดไว้ในขั้นตอนการประการตัวแปร
            mCollec[0] = new Student("Jor");
            mCollec[1] = new Student("Ann");
            mCollec[2] = new Student("Aae");

            for(int item = 0; item < 3; item++)
                Console.WriteLine(mCollec[item]);
            Console.ReadKey();
        }
    }
}

วันพุธที่ 29 ตุลาคม พ.ศ. 2551

เพิ่มเติมกับ IEnumerator, IEnumerable และ ICloneable

จากที่ผ่านมาได้พูดถึงภาพรวมของ Collection Interface ที่ .Net ได้เตรียมไว้ คราวนี้จะมาดูรายละเอียด ของ Interface 3 ตัวคือ IEnumerator, IEnumerable และ ICloneable (ที่จริงจะกล่าวเฉพาะ IEnumerator และ IEnumerable แต่ในตัวอย่างมีการใช้งาน ICloneable ด้วยก็เลยกล่าวเพิ่มไปซะเลย)

IEnumerator เป็น interface พื้นฐานเพื่อให้ class ที่อ้างถึง interface นี้สามารถใช้ foreach ได้ ภายในมีสมาชิกดังนี้
    Methods
  • MoveNext ระบุให้เลื่อน element ปัจจุบันไป 1 ช่อง
  • Reset สังให้ current กลับไปจุดเริ่มต้น

    Properties
  • Current ดึง element ปัจจุบันจาก collection

IEnumerable เป็น interface ที่จะทำการส่งคลาส IEnumerator ไปยังคำสั่ง foreach ภายในมีสมาชิกดังนี้
    Methods
  • GetEnumerator ส่งค่า enumerator ที่จะส่งไปยัง foreach

ICloneable เป็น interface ที่กำหนดให้คลาสที่ประกาศต้องมีความสามารถในการ clone ตัวมันเอง

    Methods
  • Clone ทำการ clone ตัวมันเองแล้วส่งค่านั้นกลับไป

ตัวอย่าง Code:
ตัวอย่างต่อไปนี้จะเป็นการสร้าง single linklist โดยไม่ใช้คลาสในกลุ่ม collection ของ .Net แต่จะกำหนดโครงสร้างทั้งหมดเองตามลักษณะดังนี้

|--------------- MyList -----------------|
|---Node---|   |---Node---|   |---Node---|
|Value|Next|-->|Value|Next|-->|Value|Next|

จากโครงสร้างดังกล่าวจะมีคลาสสองตัวคือ MyList และ Node โดย MyList จะเป็น collection ของ Node ภายใน Node จะมีสมาชิกภายในคือ Value เก็บค่าตัวเลขที่ต้องการบันทึก และ Next เก็บที่อยู่ของ Node ถัดไป


using System;
using System.Collections.Generic;
using System.Collections;

namespace LinkListDemo
{
    // ประกาศคลาส Node
    public class Node : ICloneable //ประกาศใช้ ICloneable
        public int Value;
        public Node Next;
 
        public Node()
        {
            _value = 0;
        }
 
        public Node(int nValue)
        {
            _value = nValue;
        }
 
        public override string ToString()
        {
            return _value.ToString();
        }
 
        #region ICloneable Members
        public object Clone()
        {
            return new Node(_value);         }
        #endregion
    }
 
    // ประกาศคลาส MyList
    public class MyList : IEnumerator, IEnumerable, ICloneable
    {
        private Node _firstNode;
        private Node _curNode;         private Node _lastNode;
 
        public MyList()
        {
            _firstNode = new Node(); //ตำแหน่ง 1.1
        }
 
        public bool MoveNext()
        {
            _curNode = _curNode.Next; //ตำแหน่ง 1.2
            return !(_curNode == null);
        }
 
        public void Reset()
        {
            _curNode = _firstNode; //ตำแหน่ง 1.3
        }
 
        public object Current //ตำแหน่ง 1.4
        {
            get
            {
                if (_curNode != null)
                {
                    return _curNode;
                }
                else                 {
                    throw new InvalidOperationException();
                }
            }
        }
 
        public void Add(Node newNode)
        {
            if (_curNode == null)
            {
                _firstNode.Next = newNode;
                _curNode = newNode;
                _lastNode = newNode;
            }
            else
            {
                _lastNode.Next = newNode;
                _lastNode = newNode;
            }
        }
 
        public void Add(int newValue)
        {
            Node newNode = new Node(newValue);
            this.Add(newNode);
        }
 
        #region IEnumerable Members
        public IEnumerator GetEnumerator() //ตำแหน่ง 2
        {
            return (IEnumerator)Clone();
        }
        #endregion
 
        #region ICloneable Members
        public object Clone()
        {
            Node tmpNode = _curNode;
            MyList newList = new MyList();
            _curNode = _firstNode.Next;
            while (_curNode != null)
            {
                newList.Add(_curNode.Value);
                _curNode = _curNode.Next;
            }
            return newList;
        }
        #endregion
    }
 
    class Program //โปรแกรมเรียกใช้ class MyList
    {
        static void Main(string[] args)
        {
            MyList myList = new MyList();
            for (int cnt = 0; cnt < 10; cnt++)
            {
                myList.Add(new Node(cnt));
            }
            foreach (Node node in myList)
            {
                Console.WriteLine(node);
            }
            Console.ReadKey();
        }
    }

ในการวนรอบของ foreach นั้นคำสั่ง foreach จะเรียก GetEnumerable() (ตำแหน่ง 2) เพื่อเรียก object collector ขึ้นมาโดยที่ object collector ที่คืนกลับนั้นคลาสจะต้องประกาศ IEnumerator ไว้ด้วย หลังจาก foreach ได้ object collector จะมีขั้นตอนการดำเนินการดังนี้
  1. เรียก MoveNext(ตำแหน่ง 1.2) เพื่อเลื่อนตำแหน่งของ element ไปยังจุดถัดไป ถ้าผลของการ MoveNext ส่งกลับมาเป็น true จะทำงานในข้อ 2 ต่อ ถ้าเป็น false จะหยุดการทำงาน
  2. ส่งค่าใน element ออกไปผ่าน property Current (ตำแหน่ง 1.4)
  3. วนกลับไปทำข้อ 1 ใหม่

จากขั้นตอนดังกล่าวจะเห็นว่าเมื่อเริ่มต้นนั้นการวนรอบของ foreach นั้นตำแหน่งของ Current นั้นจะต้องชี้ไปยังตำแหน่งก่อนหน้าค่าแรก (ในกรณีที่เป็น array ที่มีตำแหน่งเริ่มต้นที่ array[0] ตำแหน่งของ current จะต้องชี้ที่ตำแหน่ง -1) เนื่องจาก foreach จะเรียกใช้คำสั่ง MoveNext (ตำแหน่ง 1.3) ก่อนแล้วจึงส่ง element ใน collector ออกไป ดังนั้นในตอนที่สร้าง MyList ขึ้นมาจึงได้สร้าง node ใหม่ให้กับ _firstNode ในทันทีเพื่อป้องกันความผิดพลาดที่จะเกิดจากการอ่านข้อมูลจาก object ที่ไม่มีการอ้างถึง

วันอังคารที่ 28 ตุลาคม พ.ศ. 2551

ทำความรู้จัก Collection Interface ใน .Net

ใน .Net ได้จัดเตรียมเครื่องมือสำหรับสร้างคลาส Collector ไว้หลายชนิดด้วยกัน หนึ่งในนั้นคือกลุ่ม Interface เพื่อให้คลาสกลุ่ม Collector ที่สร้างขึ้นมานั้นสามารถเชื่อมต่อการทำงานกับภายนอกได้ Interface ที่ .Net ได้จัดเตรียมไว้ให้มีหลายตัว เช่น   
  • IEnumerator และ IEnumerable  ทำให้คลาสที่ inherit จาก interface นี้สามารถใช้คำสั่งวนรอบ foreach ได้
  • ICompare และ IComparable ทำให้คลาสที่ inherit จาก interface นี้สามารถทำการจัดเรียงข้อมูลภายในได้
  • ICollection  เป็นฐานของคลาสใน System.Collection
  • IList สืบทอดมาจาก ICollection อีกต่อหนึ่ง และเป็นฐานของ non-generic lists
  • IDictionary ทำให้คลาสที่ inherit สามารถทำงานได้ในลักษณะเดียวกับคลาส Dictionary
  • IDictionaryEnumerator เหมือนกับ Dictionary แต่สามารถใช้ร่วมกับคำสั่ง foreach ได้ (คลาส Dictionary ไม่สามารถใช้คำสั่ง foreach ได้)
  • IHash คลาสที่สืบทอดมาจะต้องมีคุณสมบัติการทำฟังก์ชั่น Hash

วันศุกร์ที่ 24 ตุลาคม พ.ศ. 2551

การใช้งาน Collections ใน .Net

การเขียนโปรแกรมส่วนใหญ่จะมีการใช้งาน Collections รวมด้วยเสมอเช่น การใช้งาน ListBox ก็จะมี Property Indexs หรือแม้แต่การใช้งาน DataSet ก็จะมี Tables, Columns, Rows ก็ล้วนเป็น Collections ด้วยกันทั้งสิ้น คราวนี้หากเราต้องการสร้าง Class ที่ต้องใช้ความสามารถดังกล่าว ตัวอย่าง การสร้างรายชื่อนักเรียนภายในห้อง เราก็จะสร้างคลาส ClassRoom ภายใน ClassRoom จะมี Property Students ซึ่งจะเก็บรายชื่อนักเรียนทั้งหมดไว้ ซึ่งจะมีโครงสร้างดังนี้

    Class จะมีทั้งหมด 3 class ด้วยกันคือ
  1. Student เป็นตัวแทนนักเรียนแต่ละคนในชั้นเรียน
  2. StudentList เป็นตัวแทนรายชื่อนักเรียนทั้งหมดในชั้นเรียน class นี้จะนำเอาเทคนิคการใช้ Collection มาใช้
  3. ClassRoom เป็นตัวแทนห้องเรียน แสดงการนำ Collection ที่ประการศไว้มาใช้ โดยนำคลาส StudentList มาเป็น Property หนึ่งของ ClassRoom

    ตัวอย่าง Code:

    public class Student
    {
        private int _id;
        private string _name;
        private string _surname;

        Student()
        {
            _id = 0;
        }
       
        public Student(int id, string name, string surname)
        {
            _id = id;
            _name = name;
            _surname = surname;
        }

        public int Id
        {
            get{ return _id; }
            set{ _id = id; }
        }

        public string Name
        {
            get{ return _name; }
            set{ _name = value;}
        }

        public string Surname
        {
            get{ return _surname; }
            set{ _surname = value; }
        }

        public override string ToString()
        {
            return _name + " " + _surname;
        }
    }

    จาก Student ข้างต้นจะมี attribute ที่เป็น private คือ _id, _name, _surname มี Properties 3 ตัวคือ Id, Name, Surname และมี Constructor 2 ตัว นอกจากนี้ยังมี method 1 ตัวคือ ToString() เพื่อให้สามารถแปลงเป็น string ได้เลย

    public class ClassRoom
    {
        public class StudentList : CollectionBase // ประกาศ Nested Class
        {
            public void Add(Student std)
            {
                if ((std.id == 0) || (std.Name == null) || (std.Surname == null))
                {
                    Console.WriteLine("Error: Student not initialize.");
                    return;
                }
                List.Add(std); //เพิ่ม std ลงใน list
            }

            public void Remove(Student std)
            {
                List.Remove(std); //ลบ std ออกจาก list
            }

            public Student this[int stdIndex] // ประกาศ property เพื่อให้สามารถมองในลักษณะ array
            {
                get { return (Student)List[stdIndex]; }
                set { List[stdIndex] = value; }
            }
        }

        private string _className;
        private string _Advisor;
        public StudentList Students;

        public ClassRoom()
        {
            Students = new StudentList(); // สร้างคลาส StudentList
        }

        public string ClassName
        {
            get { return _className; }
            set { _className = value; }
        }

        public string Advisor
        {
            get { return _Advisor; }
            set { _Advisor = value; }
        }

        public override string ToString()
        {
            StringBuilder str = new StringBuilder();
            str.Append("Class Room: " + _className + "\n");
            str.Append("Adviser: " + _Advisor + "\n");
            str.Append("Student: \n");
            int cnt = 1;
            foreach (Student std in Students)
            {
                str.Append("\t" + cnt++.ToString() + ") " + std + "\tid: " + std.id + "\n");
            }
            return str.ToString();
        }
    }

    จาก Code ข้างต้นภายในคลาส ClassRoom จะมีคลาสภายในคือ StudentList ซึ่งจะถือว่าเป็น Nested Class โดยที่ StduentList จะ Inherited Class มาจาก CollectionBase โดยภายใน CollectionBase จะมี property ที่สำคัญคือ List หลังจากที่ประกาศคลาสเรียบร้อยแล้ว ได้นำคลาส StudentList มาใช้กับ property Students

    public class Program
    {
        static void Main(string[] args)
        {
            ClassRoom c1 = new ClassRoom();
            c1.Advisor = "Tanapat Warahakit";
            c1.ClassName = "p1";
            c1.Students.Add(new Student(1001, "Tanapat", "Warahakit"));
            c1.Students.Add(new Student(1002, "Tanapong", "Warahakit"));
            c1.Students.Add(new Student(1003, "Jarutas", "Patthanapanchai"));
            Console.WriteLine(c1);
            Console.ReadKey();
        }
    }

    หลังจากที่ประกาศคลาส Student, StudentList และ ClassRoom เรียบร้อยแล้วก็ถึงเวลานำคลาสที่เตรียมไว้มาใช้ดังตัวอย่างข้างต้น

วันอังคารที่ 21 ตุลาคม พ.ศ. 2551

การติดต่อ XML โดยใช้ XmlDocument

ต่อจากตอนที่แล้ว ที่กล่าวถึงการติดต่อ XML แบบ SAX ตอนนี้จะกล่าวถึงการติดต่อแบบ DOM ซึ่งจะเป็นการโหลดข้อมูลทั้งหมดเข้าหน่วยความจำแล้วสร้างโครงสร้างข้อมูลแบบ tree ขึ้นมาในหน่วยความจำ จึงทำให้เราสามารถท่องไปยังส่วนต่างๆ ของข้อมูลได้โดยง่าย นอกจากนั้นยังสามารถจัดการข้อมูลได้โดยง่าย เช่น การเพิ่ม ลบ หรือแก้ไขข้อมูล แต่ข้อเสียของการดำเนินการแบบนี้คือหากข้อมูลมีขนาดใหญ่จะต้องมีหน่วยความจำจำนวนมากมารองรับด้วย และการดำเนินการทั้งหมดนี้จะดำเนินการได้หลังจากโหลดข้อมูลทั้งหมดขึ้นหน่วยความจำแล้ว

ตัวอย่าง code

วันอาทิตย์ที่ 19 ตุลาคม พ.ศ. 2551

การติดต่อไฟล์ xml ด้วย xmlTextReader

การติดต่อไฟล์ XML ด้วย XmlTextReader จะเป็นการติดต่อแบบ SAX (Simple API for XML) ซึ่งจะมีลักษณะการทำงานเป็นแบบ อ่านข้อมูลเข้ามาทีละชุด โดยข้อมูลที่เข้ามานั้นอาจจะเป็น element หรือ text หรือ attribute ก็ได้ ดังนั้นข้อดีของการติดต่อไฟล์ XML ด้วย SAX คือสามารถติดต่อไฟล์ XML ที่มีขนาดใหญ่มากๆ ได้ดีเนื่องจากจะเป็นการอ่านข้อมูลมาทีละชุดจึงทำให้ใช้เนื้อที่ในหน่วยความจำน้อย และสามารถทำงานได้รวดเร็วเนื่องจากทำการอ่านข้อมูลมา 1 ชุดแล้วก็ดำเนินการประมวลผลได้เลยไม่ต่อรอให้ระบบอ่านข้อมูลมาจนหมด ส่วนข้อเสียคือการเพิ่ม/ลดข้อมูล และ การท่องไปยังข้อมูลต่างๆ ในไฟล์ XML จะทำได้ยากเพราะการทำงานของ SAX นั้นจะเป็นการทำงานเรียงจาก element แรกเรื่อยไปจนกระทั่งถึง element สุดท้าย

ตัวอย่าง Code

วันจันทร์ที่ 15 กันยายน พ.ศ. 2551

วิธีเชื่อมข้อมูล MySql โดยใช้ BindingSource บน .Net

คำเตือน ข้อมูลในส่วนนี้สรุปเอาจากความเข้าใจ โดยการศึกษาและพยายามลองผิดลองถูกเอา อาจจะมีวิธีการอื่นที่ถูกต้องและรัดกุมกว่านี้

สิ่งที่ต้องเตรียม
1. ฐานข้อมูล MySql พร้อมข้อมูล ในที่นี้จะใช้ฐานข้อมูลชื่อ test ภายในฐานข้อมูลมีตาราง 1 ตารางชื่อ table1 ข้อมูลมีดังนี้
==== table1 ====
id name
================
1 abc
2 def
================

2. ติดตั้ง MySql Connector สำหรับ .net

ลงมือทำ

1. สร้าง Project ใหม่ใน Visual Studio

2. เพิ่ม Refence ของ MySql โดยเลือก Project -> Add Reference -> MySQL.Data ดังรูป














3. เพิ่ม using MySql ในหัวของ Form1 ดังนี้

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;


4. วาง component ดังรูป















พร้อมกับกำหนด properties ของ component แต่ละตัวดังนี้
- dataset1 ให้เลือก Tables แล้วในช่อง (Collection) จะแสดงปุ่ม '...' ให้กดปุ่มนี้ โปรแกรมจะแสดงหน้าต่างใหม่ ให้กดปุ่ม add จะได้ตารางใหม่ชื่อ Table1 ให้เปลี่ยนเป็นชื่อ table1 (เปลี่ยนตัว T เป็นตัวเล็ก) จากนั้นเลือก Columns ช่อง (Collection) จะแสดงปุ่ม '...' ให้กดปุ่มนี้ จากนั้นโปรแกรมจะแสดง หน้าต่างใหม่ ให้เพิ่ม 2 column แบบเดียวกับที่เพิ่ม table จากนั้นเปลี่ยนชื่อ ColumnName เป็น id และ Name
- bindingSource1 ให้กำหนด properties DataSource = dataSet1, DataMember = table1
- bindingNavigator1 ให้กำหนด BindingSource = bindingSource1
- dataGridView1 ให้กดปุ่มเล็กๆ ตรงมุมขวา (จะมีหน้าต่างๆ เล็กๆ ขึ้นมา) แล้วเลือก ข้อมูลในช่อง Choose Data Source เป็น bindingSource1
5. แก้ไข Source Code ให้เป็นดังนี้ (ขี้เกียจแสดงอธิบายรายละเอียดการใส่ code นะ)

public partial class Form1 : Form
{
private MySqlConnection conn;
private MySqlDataAdapter adp;

public
Form1()
{
InitializeComponent();
}
private void Form1_Load(object
sender, EventArgs e)
{
conn = new
MySqlConnection("database=test;server=localhost;uid=root;pwd=password");
adp = new MySqlDataAdapter();
adp.SelectCommand = new MySqlCommand("SELECT * FROM
table1", conn);
MySqlCommandBuilder cb = new
MySqlCommandBuilder(adp);
conn.Open();
adp.Fill(dataSet1,
"table1");

// ในกรณีที่ไม่ได้สร้าง table ภายใน dataSet1 ตามข้อ 4 ให้เพิ่ม  Code นี้ลงไป

DataColumn dc1 = new DataColumn("id", System.Type.GetType("System.Int32"));
DataColumn dc2 = new DataColumn("Name", System.Type.GetType("System.String"));
DataTable tb = new DataTable("NameList");
dataSet1.Tables.Add(tb);
bindingSource1.DataSource = dataSet1;
bindingSource1.DataMember = "table1";
dataGridView1.AutoGenerateColumns = true;

}


private void Form1_FormClosed(object sender, FormClosedEventArgs
e)
{
conn.Close();
}

private void dataGridView1_CellEndEdit(object sender,
DataGridViewCellEventArgs e)
{
adp.Update(dataSet1.Tables["table1"]);
}
private void
dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
adp.Update(dataSet1.Tables["table1"]);
}
}


เพิ่มเติม ในกรณีที่ต้องการเชื่อมต่อกับ control อื่นๆ เช่น textbox, combobox ให้ใช้ code ดังต่อไปนี้ในส่วนที่จะเชื่อมต่อกับฐานข้อมูล

ในกรณี textbox

textBox1.DataBindings.Add(new Binding("Text", bindingSource1, "id", true));

ในกรณี combobox

comboBox1.DataSource = dataSet1;
comboBox1.DisplayMember = "table1.Name";
comboBox1.DataBindings.Add(new Binding("Text", bindingSource1, "Name"));



วันพฤหัสบดีที่ 10 กรกฎาคม พ.ศ. 2551

ลำดับการเรียก Exception ใน C#

การเกิด Exception ใน C# กรณีที่เป็น Nested ให้พิจารณาตัวอย่างต่อไปนี้


using System;
using System.Text;

namespace DemoEsception
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Point A");
try
{
Console.WriteLine("Point B");
// Try to convert wrong format
Console.WriteLine(int.Parse("abc"));
Console.WriteLine("Point C");
}
catch (FormatException e)
{
Console.WriteLine("Catch exception:{0}",e);
}
Console.WriteLine("Point D");
}
catch (Exception e)
{
Console.WriteLine("Outer exception:{0}",e);
}
Console.WriteLine("Point E");
Console.ReadKey();
}
}
}


ผลลัพธ์ที่ได้
Point A
Point B

Catch exception:System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.Int32.Parse(String s) at DemoEsception.Program.Main(String[] args) in D:\Data\Visual Studio 2008\Projects\DemoEsception\DemoEsception\Program.cs:line 17
Point D
Point E


สังเกตว่าใน code นี้เกิดความผิดพลาดขึ้นเมื่อเกิดความพยายามเปลี่ยนตัวอักษร abc ให้เป็นตัวเลขโดยใช้คำสั่ง int.Parse("abc") ซึ่งจะทำให้เกิด FormatException ขึ้น และเมื่อเกิด Exception ขึ้นนั้นโปรแกรมจะข้าม Point C แล้วทำงานในส่วนของ exception แล้วกลับไปทำงานยัง Point D และ Point E จะเห็นว่าโปรแกรมจะไม่เข้าไปดำเนินการ exception ด้านนอกเนื่องจากได้ดำเนินการ FormatException ไปเรียบร้อยแล้ว

พิจาณา Code ต่อไปนี้เพิ่มเติม

using System;
using System.Text;

namespace DemoEsception
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Point A");
try
{
Console.WriteLine("Point B");
// Try to convert wrong format
Console.WriteLine(int.Parse("abc"));
Console.WriteLine("Point C");
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("Catch exception:{0}",e);
}
Console.WriteLine("Point D");
}
catch (Exception e)
{
Console.WriteLine("Outer exception:{0}",e);
}
Console.WriteLine("Point E");
Console.ReadKey();
}
}
}

ผลลัพธ์ที่ได้
Point A
Point B
Outer exception:System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.Int32.Parse(String s) at DemoEsception.Program.Main(String[] args) in D:\Data\Visual Studio 2008\Projects\DemoEsception\DemoEsception\Program.cs:line 17
Point E

จากตัวอย่างข้างต้นได้เปลี่ยนแปลง Exception เป็น IndexOutOfRangeException ซึ่งทำให้เกิด exception ที่ไม่ได้ดักจับไว้ ผลคือ โปรแกรมจะข้ามการดำเนินการ exception ภายในไปยัง exception ภายนอกซึ่งไม่ได้กำหนดชนิดของ exception ไว้จึงทำให้ การดำเนินการใน Point C และ Point D ถูกข้ามไป เมื่อทำงานในส่วน exception เสร็จแล้วโปรแกรมจึงกลับมาดำเนินการใน Point E ต่อไป

วันจันทร์ที่ 2 มิถุนายน พ.ศ. 2551

วิธีการติดต่อฐานข้อมูลโดยใช้ SharpDevelop

1. ก่อนอื่นต้อง Download MySql Connector มาก่อน
2. หลังจากติดตั้งเสร็จแล้ว ให้เรา Add Reference ลงใน Project ของเราโดยเลือก
Project->AddReference->GAC->MySQL->Select

3. เพิ่ม Code ลง "using Mysql.Data.MySqlClient;" ลงในส่วน using

ตัวอย่าง Code


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace TestDatabase
{
///
/// Description of MainForm.
///

public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void Button1Click(object sender, EventArgs e)
{
string MyConString = "SERVER=localhost;" +
"DATABASE=test;" +
"UID=root;" +
"PASSWORD=;";
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "select * from test";
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
string thisrow = "";
for(int i= 0;ithisrow+=Reader.GetValue(i).ToString() + ",";
listBox1.Items.Add(thisrow);
}
connection.Close();
}
}
}