VS2019 C# MySQL student information addition, deletion, modification and query (1, addition and deletion)

VS2019 C# MySQL student information addition, deletion, modification and query

由于刚刚学习VS2019,入手的第一个例子是编写一个简单的增删改查。这里,我以学生信息为例,进行详细的过程描述以及代码实现。

1. Create a new Windows Forms application

1. Create a new project interface, as shown in the figure below.
insert image description here
2. Change the project name, of course you can not modify it. What I have here is "StudentInfoSystem"; the path of the project storage location can be changed according to your own needs; the check box in front of "Put the solution and the project in the same directory" can be selected or not; click "Create", as shown in the figure below Show.
insert image description here
2. Preparatory work

1. Change the "Form1" of the Text in the Form1 property to "Student Management System Homepage", and drag 4 Button buttons from the toolbox to the design interface, as shown in the figure below. Change the (Name) in the properties of "button1", "button2", "button3", and "button4" to "BtnZeng", "BtnShan", "BtnGai", and "BtnCha" respectively, and change the Text in their properties to For "add", "delete", "modify", "view".
insert image description here
insert image description here
2. (1) Select "StudentInfoSystem" with the mouse, right-click and select "Add", and find "Form (Windows Form)", as shown in the figure below.
insert image description here
(2) Add 4 Form design interfaces, respectively change (Name) in their properties to FormZengJia, FormShanChu, FormXiuGai, FormChaKan, and modify their properties Text to "Add", "Delete", "Modify", "View ",As shown below.
insert image description here
insert image description here
insert image description here
insert image description here
3. Double-click the four buttons of Add, Delete, Modify, and View respectively, connect the four design interfaces to the main interface, and add codes, as follows.

public partial class Form1 : Form
    {
    
    
        public Form1()
        {
    
    
            InitializeComponent();
        }

        private void BtnZeng_Click(object sender, EventArgs e)
        {
    
    
            FormZengJia zengJia = new FormZengJia();
            zengJia.Show();
        }
        private void BtnShan_Click(object sender, EventArgs e)
        {
    
    
            FormShanChu shanChu = new FormShanChu();
            shanChu.Show();
        }
        private void BtnGai_Click(object sender, EventArgs e)
        {
    
    
            FormXiuGai xiuGai = new FormXiuGai();
            xiuGai.Show();
        }
        private void BtnCha_Click(object sender, EventArgs e)
        {
    
    
            FormChaKan chaKan = new FormChaKan();
            chaKan.Show();
        }
    }

3. Establish a database

I use MySQL.

1. Create a new database, as shown in the figure below.
insert image description here
2. Database table design, as shown in the figure below.
insert image description here
Four, increase

1. Drag the control into the FormZengJia interface, as shown in the figure below.
insert image description here
2. Modify the corresponding properties in TextBox and Button, as shown in the figure below.
insert image description here
3. Double-click the "Add a record" button to enter the code interface. The first job is to import, right-click "Reference", select "Add Reference | Browse", find the "MySql.Data.dll" in the MySQL file you installed, as follows As shown in the figure.
insert image description here

insert image description here
4. Introduce "using MySql.Data.MySqlClient;" in the header file, as follows.

using MySql.Data.MySqlClient;

5. The code is as follows:

private void BtnZeng_Click(object sender, EventArgs e)
        {
    
    
            String connectStr = "server=localhost;database=studentinfosystem;user=root;password=123456;port=3306;charset=utf8";
            //server=127.0.0.1或localhost。database是我的数据库名,studentinfosystem是新建的数据库名。
            //我这里的user是root,密码是123456(安装MySQl时自己设置的密码)。端口号port一般都是3306。
            //charset字符集一定要加,不加的话,虽然连接成功,但如果含有中文,就显示不了中文内容。
            MySqlConnection conn = new MySqlConnection(connectStr);
            conn.Open();//打开通道,建立连接

            //下面主要就是条件判断
            if (tbno.Text == "")
            {
    
    
                MessageBox.Show("学号不能为空");
            }
            else if (tbname.Text == "")
            {
    
    
                MessageBox.Show("姓名不能为空");
            }
            else if (tbno.Text != "" && tbname.Text != "")
            {
    
    
                string str = "";
                if (tbdept.Text.Trim().Length == 0 && tbbirth.Text.Trim().Length == 0)
                {
    
    
                    str = "insert into info(学号,姓名) values('" + tbno.Text + "','" + tbname.Text + "')";//info是数据库表名
                }
                else if (tbdept.Text.Trim().Length == 0 && tbbirth.Text.Trim().Length > 0)
                {
    
    
                    str = "insert into info(学号,姓名,出生日期) values('" + tbno.Text + "','" + tbname.Text + "','" + tbbirth.Text + "')";
                }
                else if (tbdept.Text.Trim().Length > 0 && tbbirth.Text.Trim().Length == 0)
                {
    
    
                    str = "insert into info(学号,姓名,专业) values('" + tbno.Text + "','" + tbname.Text + "','" + tbdept.Text + "')";
                }
                else if (tbdept.Text.Trim().Length > 0 && tbbirth.Text.Trim().Length > 0)
                {
    
    
                    str = "insert into info(学号,姓名,专业,出生日期) values('" + tbno.Text + "','" + tbname.Text + "','" + tbdept.Text + "','" + tbbirth.Text + "')";
                }

                try
                {
    
    
                    MySqlCommand cmd = new MySqlCommand(str, conn);
                    cmd.ExecuteNonQuery();
                }
                catch (MySqlException ex)
                {
    
    
                    MessageBox.Show(ex.Message);
                }

                int s = 1;
                if (s == 1)
                {
    
    
                    MessageBox.Show("成功增加一条记录");
                    tbno.Clear();
                    tbname.Clear();
                    tbdept.Clear();
                    tbbirth.Clear();//清空TextBox中的数据
                }
            }
            conn.Close();
        }

6. Click "Start", add a record to see the effect, it will display "Add a record successfully", go back to the database table, refresh it, and find that the record just added has been successfully saved in the database table, as shown in the figure below.
insert image description here
insert image description here
Five, delete

1. Drag the control into the FormShanChu interface and change the corresponding properties, as shown in the figure below.
insert image description here
2. Double-click the "Delete a Record" button to enter the code interface, and import "using MySql.Data.MySqlClient;" into the header file.

3. The code is as follows:

private void BtnDelete_Click(object sender, EventArgs e)
        {
    
    
            String connectStr = "server=localhost;database=studentinfosystem;user=root;password=123456;port=3306;charset=utf8";
            //server=127.0.0.1或localhost。database是我的数据库名,studentinfosystem是新建的数据库名。
            //我这里的user是root,密码是123456(安装MySQl时自己设置的密码)。端口号port一般都是3306。
            //charset字符集一定要加,不加的话,虽然连接成功,但如果含有中文,就显示不了中文内容。
            MySqlConnection conn = new MySqlConnection(connectStr);
            conn.Open();//打开通道,建立连接

            //下面主要是条件判断
            string str = "";
            if (tbno.Text.Trim().Length == 0 && tbname.Text.Trim().Length == 0)
            {
    
    
                MessageBox.Show("请输入要删除的学号或姓名!");
            }
            else if (tbno.Text.Trim().Length > 0 && tbname.Text.Trim().Length == 0)
            {
    
    
                str = "delete from info where 学号='" + tbno.Text + "'";//按“学号”删除
            }
            else if (tbno.Text.Trim().Length == 0 && tbname.Text.Trim().Length > 0)
            {
    
    
                str = "delete from info where 姓名='" + tbname.Text + "'";//按“姓名”删除
            }
            else if (tbno.Text.Trim().Length > 0 && tbname.Text.Trim().Length > 0)
            {
    
    
                str = "delete from info where 学号='" + tbno.Text + "'and 姓名='" + tbname.Text + "'";//按“学号+姓名”删除
            }

            try
            {
    
    
                MySqlCommand cmd = new MySqlCommand(str, conn);
                cmd.ExecuteNonQuery();
            }
            catch (MySqlException ex)
            {
    
    
                MessageBox.Show(ex.Message);
            }

            int s = 1;
            if (s == 1)
            {
    
    
                MessageBox.Show("删除成功");
                tbno.Clear();
                tbname.Clear();//清空
            }
            conn.Close();
        }

4. Click "Start", delete a record to see the effect, and it will display "deleted successfully", go back to the database table, refresh it, and find that the record just deleted has been successfully deleted, as shown in the figure below.
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/qq_46649692/article/details/109254850