VS2019 C# MySQL student information addition, deletion, modification and query (2. Change and query (continued from the previous section))

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

Six, change

1. Drag the control into the FormXiuGai interface and change the corresponding properties, as shown in the figure below.
Idea: Search first, then display the found data on the TextBox, and then modify it. I set the student number here to be non-modifiable. Click the TextBox text box after the student number, find Enabled in the property bar, and set it to False (unmodifiable), so that the user can only modify the content other than the student number.
insert image description here
2. Double-click the "Query" button, code, if found, it will be displayed in the text box on the right; if not found, a prompt box will pop up, the code is as follows.
(Remember to introduce in the header file: using MySql.Data.MySqlClient;)

private void BtnChaXun_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 = "select *from info where 学号=" + tbxuehao.Text;
            MySqlCommand command = new MySqlCommand(str, conn);
            MySqlDataReader mySqlDataReader = command.ExecuteReader();
            if (mySqlDataReader != null)
            {
    
    
                MessageBox.Show("查询成功");
                mySqlDataReader.Read();

                tbno.Text = mySqlDataReader[1].ToString(); //将查询结果显示在文本框
                tbname.Text = mySqlDataReader[2].ToString();
                tbdept.Text = mySqlDataReader[3].ToString();
                tbbirth.Text = mySqlDataReader[4].ToString();
            }
            else
            {
    
    
                MessageBox.Show("请重新输入");
            }
        }

3. Double-click the "Modify" button, code, the code is as follows.

private void BtnUpdate_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 = "update info set 姓名='" + tbname.Text + "',专业='" + tbdept.Text + "',出生日期='" + tbbirth.Text + "'where 学号='" + tbxuehao.Text + "'";
            MySqlCommand command = new MySqlCommand(str, conn);
            command.ExecuteNonQuery();
            MessageBox.Show("修改成功");
        }

4. Click "Start" to query a record to see the effect, and it will display "query successful"; then modify the information, click the "Modify" button, and it will prompt "modification successful". Go back to the database table, refresh it, and find that the record just modified has been successfully modified, as shown in the figure below.
insert image description here
insert image description here
insert image description here
insert image description here
Seven, check

1. Drag the control into the FormChaKan interface and change the corresponding properties, as shown in the figure below.
insert image description here
2. Double-click the "OK" button to encode, the code is as follows.
(Remember to introduce in the header file: using MySql.Data.MySqlClient;)

private void BtnOK_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 = "select *from info ";
            if (tbno.Text.Trim().Length > 0)
            {
    
    
                str += "where 学号='" + Convert.ToInt32(tbno.Text) + "'";
            }
            if (tbname.Text.Trim().Length > 0)
            {
    
    
                if (str.Contains("where"))
                {
    
    
                    str += "and 姓名='" + tbname.Text + "'";
                }
                else
                {
    
    
                    str += "where 姓名='" + tbname.Text + "'";
                }
            }
            if (tbdept.Text.Trim().Length > 0)
            {
    
    
                if (str.Contains("where"))
                {
    
    
                    str += "and 专业='" + Convert.ToInt32(tbdept.Text) + "'";
                }
                else
                {
    
    
                    str += "where 专业='" + Convert.ToInt32(tbdept.Text) + "'";
                }
            }
            if (tbbirth.Text.Trim().Length > 0)
            {
    
    
                if (str.Contains("where"))
                {
    
    
                    str += "and 出生日期='" + Convert.ToInt32(tbbirth.Text) + "'";
                }
                else
                {
    
    
                    str += "where 出生日期='" + Convert.ToInt32(tbbirth.Text) + "'";
                }
            }
            MySqlCommand comm = new MySqlCommand(str, conn);
            comm.ExecuteNonQuery();
            MySqlDataAdapter mysd = new MySqlDataAdapter(comm);
            DataSet ds = new DataSet();
            mysd.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            conn.Close();
        }

3. Click "Start" to view a record, as shown in the figure below.
insert image description here

到这儿,简单学生管理系统的增删改查操作就全部完成。文中如有发现错误,欢迎指正!

Guess you like

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