C#写winform简单的增删改查,数据库mysql

1.创建项目,创建一个.net framework的winform

2.搭建页面

6个按钮,1个dataset存放数据,1个datagridview展示数据,dataset和datagridview怎么用,下面一步步介绍。

3.先将数据展示做好,在工具箱中找到Dataset选中将其拖到窗体中

4.选择非类型化数据集合

5.生成数据展示列表,将datagridview拖到窗体中

6.将dataset和datagridview绑定点击datagridview右上角的小箭头,选择数据源

7.datagridview展示的列,

添加三列。一列放表id,一列放用户名,一列放密码

7.绑定表字段,这步的操作目的是为了将这一列和数据库查出来的列绑定

找到数据这一属性,将值改为数据库表字段名,表什么的大家自己建吧,主键id自增,username,password

8.建好按钮

9.导入mysql数据库的引用,在资源管理器中,右击项目选择Nuget程序包

搜索mysql安装

10.建立数据库连接,双击点击,进行点击按钮后的代码编写

导入mysql的引用以及定义全局变量

这是连接按钮的代码,如果运行点击连接弹出连接成功,说明已经成功一半了。

private void button5_Click(object sender, EventArgs e)
        {
            string connStr = "server=127.0.0.1;port=3306;user=root;password=111111;database=demo_template;";
            con = new MySqlConnection(connStr);
            try
            {
                con.Open();
                MessageBox.Show("连接成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

11.查询展示功能,双击查询按钮

这是查询按钮代码

private void button1_Click(object sender, EventArgs e)
        {
            string queryUsers = "select * from sys_user";
            MySqlDataAdapter mda = new MySqlDataAdapter(queryUsers,con);
            dataSet1.Clear();
            mda.Fill(dataSet1,"users");
            dataGridView1.DataSource = dataSet1.Tables["users"];

        }

恭喜你,然后就可以运行试下,能不能展示出数据了

12.删除功能,点击删除按钮

由于datagridview是可以多选的,那咋就写出循环,这是删除按钮的代码

private void button4_Click(object sender, EventArgs e)
        {
            string detelteStr = "delete from sys_user where id = ";
            DataGridViewSelectedRowCollection list = dataGridView1.SelectedRows;
            if (list.Count < 1) {
                MessageBox.Show("未选中任何一行");
            }
            for (int i=0;i< list.Count;i++) {
                DataGridViewRow row = list[i];
                Object id = row.Cells[0].Value;
                MySqlCommand com = new MySqlCommand(detelteStr + id, con);
                com.ExecuteNonQuery();
            }
            button1_Click(sender, e);
            MessageBox.Show("删除成功");

        }

13.新增,编辑,新建个form窗体,第二个form中进行新增和编辑,后面只会列图和代码就不详细写步骤了。

Form2的引用以及全局变量

保存按钮代码:

private void button1_Click(object sender, EventArgs e)
        {
            if (id != null)
            {

                //编辑更新
                string uname = textBox1.Text;
                string pword = textBox2.Text;
                if (uname == "")
                {
                    MessageBox.Show("用户名为空");
                    return;
                }
                if (pword == "")
                {
                    MessageBox.Show("密码为空");
                    return;
                }
                string updateUser = "update sys_user set username = " + uname + ",password = " + pword + " where id = " + id + "";

                try
                {
                    MySqlCommand com = new MySqlCommand(updateUser, con);
                    com.ExecuteNonQuery();
                    MessageBox.Show("更新成功");
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
            else {

                //新增
                string uname = textBox1.Text;
                string pword = textBox2.Text;
                if (uname == "")
                {
                    MessageBox.Show("用户名为空");
                    return;
                }
                if (pword == "")
                {
                    MessageBox.Show("密码为空");
                    return;
                }
                string insertUser = "insert into sys_user(username,password) value(" + uname + "," + pword + ")";

                try
                {
                    MySqlCommand com = new MySqlCommand(insertUser, con);
                    com.ExecuteNonQuery();
                    MessageBox.Show("插入成功");
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

Form1新增按钮

  private void button2_Click_1(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.con = con;
            form2.Show();
        }

private void button3_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection list = dataGridView1.SelectedRows;
            if (list.Count < 1)
            {
                MessageBox.Show("未选中任何一行");
                return;
            }
            if (list.Count > 1)
            {
                MessageBox.Show("只能选中任何一行");
                return;
            }
            DataGridViewRow row = list[0];
            Object id = row.Cells[0].Value;
            Object username = row.Cells[1].Value;
            Object password = row.Cells[2].Value;

            Form2 form2 = new Form2();

            form2.textBox1.Text = (string)username;
            form2.textBox2.Text = (string)password;
            form2.con = con;
            form2.id = (int)id;
            form2.Show();
        }

这里要注意form2.textBox1和2是form2私有变量,所以点进去将textBox1和2改为public

发布了77 篇原创文章 · 获赞 21 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/f1370335844/article/details/101060908
今日推荐