因應有些人說我的blog都沒有新增加一些技術的東西,所以這幾天剛好實驗有用到PDA去控制MySQL資料庫,因此寫這個範例來提供給各位,希望都各位在程式上有所幫助,這樣我就值得了,呵呵

      好累啊! 寫完這篇花了一小時,再加上今天整天都在整理與找這類的程式,希望大家不要失望!

要寫這支程式的前提:
      (1) 請先去MySQL官方網站下載Connector/Net
      (2) 先加入「參考」,也就是「MySql.Data.CF.dll」檔案
      (3) 程式再加入「using MySql.Data.MySqlClient;」

注意:
(1) myConnection.Open();會出現Runtime Error,例外為IndexOutOfRangeException,有可能是您的Connector/Net 版本有問題! 像是就有人遇到自己的MySQL為5.0.9版的,但卻使用Connector/Net 6.x 版的,而造成myConnection.Open();會出現例外IndexOutOfRangeException,而改用了Connector/Net 5.2 後就可以成功連結到MySQL。
(2)  若myConnection.Open();會出現Runtime Error,例外為MissingManifestResourceException,有可能是PDA或模擬器沒連上網路。
(3)  如上第(2)點另一個可能,只要換PDA去跑程式,結果就不會有這個例外了! 但為什麼會這樣,我也不知道......>"<

PDA上C#搜尋MySQL資料的範例:

        private void Button_Click(object sender, EventArgs e)
        {
            //設定MySQL資料庫IP位址,此為localhost
            string DataSource = "localhost";
            //設定MySQL資料庫名稱,此為database_name
            string Database = "database_name";
            //設定連結MySQL資料庫使用者ID,此為admin
            string UserID = "admin";
            //設定連結MySQL資料庫使用者Password,此為1234567890
            string Password = "1234567890";

            string myConnectionString = "Data Source=" + DataSource +
                                  ";Database=" + Database +
                                  ";User ID=" + UserID +
                                  ";Password=" + Password;

            MySqlConnection myConnection = new MySqlConnection(myConnectionString);

            //設定要搜尋MySQL資料庫的語法
            //此為SELECT * FROM table
            //table為資料表名稱
            MySqlCommand myCommand = new MySqlCommand("SELECT * FROM table", myConnection);

            myConnection.Open();
            MySqlDataReader myReader;
            myReader = myCommand.ExecuteReader();
            try
            {
                searchTextBox.Text = string.Empty;
               
                while (myReader.Read())
                {
                    //myReader.GetString(0)為資料表裡的第一個欄位的資料
                    //myReader.GetString(1)為資料表裡的第二個欄位的資料
                    searchTextBox.Text += myReader.GetString(0) + " : " + myReader.GetString(1) + "\r\n";
                }
            }
            finally
            {
                myReader.Close();
                myConnection.Close();
            }
        }

 

PDA上C#「新增」、「修改」、「刪除」MySQL資料的範例:

        private void Button_Click(object sender, EventArgs e)
        {
            //設定MySQL資料庫IP位址,此為localhost
            string DataSource = "localhost";
            //設定MySQL資料庫名稱,此為database_name
            string Database = "database_name";
            //設定連結MySQL資料庫使用者ID,此為admin
            string UserID = "admin";
            //設定連結MySQL資料庫使用者Password,此為1234567890
            string Password = "1234567890";

            string myConnectionString = "Data Source=" + DataSource +
                                  ";Database=" + Database +
                                  ";User ID=" + UserID +
                                  ";Password=" + Password;

            MySqlConnection myConnection = new MySqlConnection(myConnectionString);

            //若要使用此刪除功能,請先將下面第二列「//」拿掉
            //以下這列語法為新增一筆資料進MySQL資料庫
            //string sql = "INSERT INTO table (username, phone) VALUES(dreamtails, 0921888888)";

            //若要使用此刪除功能,請先將下面第二列「//」拿掉
            //以下這列語法為修改username為dreamtails的電話成0921168168到MySQL資料庫
            //string sql = "UPDATE table SET phone=0921168168 WHERE username='dreamtails'";

            //若要使用此刪除功能,請先將下面第二列「//」拿掉
            //以下這列語法為刪除username為dreamtails的資料
            //string sql = "DELETE FROM table WHERE username='dreamtails'";


            MySqlCommand myCommand = new MySqlCommand(sql);
            myCommand.Connection = myConnection;
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myCommand.Connection.Close();
        }

 

MySQL Connectors檔案下載位址:
(1)
首頁 - http://dev.mysql.com/downloads/connector/
(2) Connector/Net - http://dev.mysql.com/downloads/connector/net/

參考資料來源:
(1)
Connector/NET Examples and Usage Guide:Using MySqlCommand - http://dev.mysql.com/doc/refman/5.0/en/connector-net-examples-mysqlcommand.html
(2) Connector/NET Examples and Usage Guide - http://dev.mysql.com/doc/refman/5.0/en/connector-net-examples.html
(3) MySQL 官方網站 - http://www.mysql.com/

以上範例若有錯誤,請各位告知或糾正,謝謝~~

arrow
arrow
    全站熱搜

    dreamtails 發表在 痞客邦 留言(43) 人氣()