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();       //断开连接
        }


    }
}

 

展开阅读全文

没有更多推荐了,返回首页