1. 程式人生 > >Winform連線MySQL資料庫實現增刪查改功能

Winform連線MySQL資料庫實現增刪查改功能

       這個練手小專案是今年8月剛接觸C#的時候做的...當時對C#還不熟,邊查資料邊完成的,其實有很多需要優化的地方...資料庫是提前建立好的,使用者名稱密碼什麼的直接寫在點選連線按鈕事件中了,可能未來的某一天想起來這個小專案會回來完善一下

介面

程式碼

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;
using MySql.Data.MySqlClient;

namespace test
{
    public partial class Form1 : Form
    {
        MySqlConnection SQLCon;
        MySqlCommand SQLCmd;
        int s1;
        string s2 = "null", s3 = "null", s4 = "null", s5 = "null", s6 = "null";
        public Form1()
        {
            InitializeComponent();
        }

        //連線
        private void bt_CONNECT_Click(object sender, EventArgs e)
        {
            string connetStr = "server=localhost;user=root;password=admin;database=testdb;sslMode=none;"; //localhost不支援ssl連線時,最後一句一定要加!!!
            SQLCon = new MySqlConnection(connetStr);
            try
            {
                SQLCon.Open(); //連線資料庫
                //MessageBox.Show("資料庫連線成功", "提示", MessageBoxButtons.OK);
                string searchStr = "select * from student";   //student表中資料
                MySqlDataAdapter adapter = new MySqlDataAdapter(searchStr, SQLCon);
                DataTable a = new DataTable();
                adapter.Fill(a);
                this.dataGridView1.DataSource = a;
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK);     //顯示錯誤資訊
            }
        }


        //增加
        private void bt_ADD_Click(object sender, EventArgs e)
        {
            s1 = Convert.ToInt16(textBox1.Text);
            s2 = textBox2.Text;
            s3 = textBox3.Text;
            s4 = textBox4.Text;
            s5 = textBox5.Text;
            s6 = textBox6.Text;
            try
            {
                string searchStr = "insert into student values(" + s1 + ",'" + s2 + "','" + s3 + "','" + s4 + "','" + s5 + "','" + s6 + "')";
                SQLCmd = new MySqlCommand(searchStr, SQLCon);
                SQLCmd.ExecuteNonQuery();
                MessageBox.Show("插入成功", "提示", MessageBoxButtons.OK);
                bt_CONNECT_Click(sender, e);
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK);
            }
        }

        //刪除
        private void bt_DELETE_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() != "")
            {
                try
                {
                    s1 = Convert.ToInt16(textBox1.Text);
                    string searchStr = ("delete from student where id=" + s1);
                    SQLCmd = new MySqlCommand(searchStr, SQLCon);
                    SQLCmd.ExecuteNonQuery();
                    MessageBox.Show("刪除成功!", "提示", MessageBoxButtons.OK);
                    bt_CONNECT_Click(sender, e);
                }
                catch (MySqlException ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK);
                }
            }
            else
            {
                try
                {
                    for (int i = 0; i < 100; i++)//遍歷所有選中的行
                    {
                        if (dataGridView1.Rows[i].Selected == true)
                        {
                            string value = dataGridView1.Rows[i].Cells[0].Value.ToString();
                            int tmp = Convert.ToInt32(value);
                            string sql = ("delete from student where id=" + tmp);
                            SQLCmd = new MySqlCommand(sql, SQLCon);
                            SQLCmd.ExecuteNonQuery();
                            MessageBox.Show("刪除成功!", "提示", MessageBoxButtons.OK);
                            bt_CONNECT_Click(sender, e);
                            break;
                        }
                    }
                }
                catch (MySqlException ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK);
                }
            }
        }

        //查詢
        private void bt_SEARCH_Click(object sender, EventArgs e)
        {
            //s1 = Convert.ToInt16(textBox1.Text);
            string str = "";
            if (textBox1.Text.Trim() != "")
                str = "where id like " + textBox1.Text.Trim();
            try
            {
                string searchStr = ("select * from student " + str);
                MySqlDataAdapter adapter = new MySqlDataAdapter(searchStr, SQLCon);
                DataTable a = new DataTable();
                adapter.Fill(a);
                this.dataGridView1.DataSource = a;
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK);
            }
        }

        //修改
        private void bt_MODIFY_Click(object sender, EventArgs e)
        {
            s1 = Convert.ToInt16(textBox1.Text);
            try
            {
                string searchStr = ("update student set gender='123' where id=" + s1);
                SQLCmd = new MySqlCommand(searchStr, SQLCon);
                SQLCmd.ExecuteNonQuery();
                bt_CONNECT_Click(sender, e);
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK);
            }
        }

        //斷開
        private void bt_DISCONNECT_Click(object sender, EventArgs e)
        {
            SQLCon.Close();       //斷開連線
        }


    }
}