Experiment: student management system based on C#

catalogue

preface

1, Preliminary preparation

1. Create a new project and connect to the database

1.1 download MySQL

1.2 on the initial interface of vsial studio 2017, click file - > New - > project, find the Windows form application, modify the project name to xscj, and click save

  1.3 four main objects connected to the database

2.MySQL table creation

2.1 main tables

  2.2 student form

2.3 transcript

2.4 Curriculum

2.5 list of Teachers

2.6 student login form

2.7 create trigger

2, Form settings

3, Realization of some functions (with source code)

1 login interface

1.1 connecting to MySql database

1.2 realization of login function

1.3 student information display

2 student management main interface

2.1 modify student function realization

2.2 add student function realization

3. Achievement management main interface function realization

3.1 add grades

3.2 adding courses

https://pan.baidu.com/s/104uMaDW65VMTitiGYKMliw

summary





preface

This experiment uses Windows Forms application, based on. NET4.6.1. The "student information management system" is written and implemented in Visual C# programming language. The development tool uses Visual Studio 2017 and MySQL 8.0.26 as the background database.



1, Preliminary preparation

1. Create a new project and connect to the database

1.1 download MySQL

Official website: https://www.mysql.com/cn/

1.2 on the initial interface of vsial studio 2017, click file - > New - > project, find the Windows form application, modify the project name to xscj, and click save

 

  After completion, click the new project, find and click Project - > Add Reference - > extension at the top of the interface, find Mysql.Data in the extension and check it.

  1.3 four main objects connected to the database

Object nameeffect
ConnectionUsed to establish a connection to a specific database
CommdandEncapsulation of data source operation commands
DataReaderRead only data access to data from a specific data source
DataAdapterA data adapter used to connect a Connection object to a data source

2.MySQL table creation

2.1 main tables

Student form, grade sheet, course schedule, administrator form, student login form

Table creation can be performed visually in phpMyadmin or on the terminal. Please refer to the data types in this article.

  2.2 student form

Function: record students' student number, name, gender, time of birth, number of courses taken and photos.

 

2.3 transcript

Function: record students' course scores and query through student number.

 

2.4 Curriculum

Function: record the opened courses to facilitate the recording and modification of courses.

 

2.5 list of Teachers

Function: record the teacher's account, password and name.

 

2.6 student login form

Function: record the name and password of each student. The default password for each student is 1008611

2.7 create trigger

  2.7.1 CJ table and XS table

Function: when adding (deleting) grades in the grade table, modify the value of the corresponding number of courses in the student table.

 

2.7.2 XS table and USER_STU table

Function: when the teacher adds (deletes) a student, the corresponding student account is added (deleted) in the student login form.

 

 

2, Form settings

The form interface can be realized by adding controls and layout, and readers can set the form according to their needs.

As shown below


                                 In this experiment, you need to modify multiple properties in the DataGridView control


  In order to fully display the student photos in the PictureBox control: the property value of its property name SizeMode can be modified to: Stretchimage

Note: in addition to the bouton control, other controls must double-click the control!!!!!!!!!!!!!

In order to clearly distinguish each control, you can modify the value of its property (NAME).

3, Realization of some functions (with source code)

1 login interface

1.1 connecting to MySql database

static string config = "database=pxscj;" +
                        "datasource = 127.0.0.1;" +
                        "userid = root;" +
                        "port=3306;" +
                        "password=*******"; //Database connection fields
        MySqlConnection conn = new MySqlConnection(config); //Connect to database

1.2 realization of login function

code implementation

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;
    .......Partial code.......
        //  Login button event response
        private void Enter_button_Click(object sender, EventArgs e)
        {
            try
            { conn.Open(); }
            catch
            {
                MessageBox.Show("Database connection failed!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            if (name_textBox.Text== "")
            {
                MessageBox.Show("User name cannot be empty", "User Login ", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
     
            }
            else if(paw_textBox.Text == "")
            {
                MessageBox.Show("Password cannot be empty", "User Login ", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                string user_Id = name_textBox.Text;
                string password = paw_textBox.Text;
                string sql = "SELECT COUNT(*) FROM USER_STU WHERE ID='" + user_Id + "'AND PASSWORD='" + password + "';";
                string sql1 = "SELECT COUNT(*) FROM USER_ADM WHERE ID='" + user_Id + "'AND PASSWORD='" + password + "';";
                MySqlCommand cmd = new MySqlCommand(sql, conn);//query
                MySqlCommand cmd1 = new MySqlCommand(sql1, conn);//query
                if (Convert.ToInt32(cmd.ExecuteScalar()) == 1 ) // Transform lookup found as 1 
                {
                    //Enter the student interface
                    MessageBox.Show("Login successful", "Welcome", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    Stu stu = new Stu(user_Id);
                    stu.Show();
                    this.Hide();
                }
                else if(Convert.ToInt32(cmd1.ExecuteScalar()) == 1)
                {
                    // Enter the administrator interface
                    MessageBox.Show("Login successful", "Welcome", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    Adm adm = new Adm();
                    adm.Show();
                    this.Hide();

                }
                else //If the login is successful, you can directly enter the information interface
                {
                   MessageBox.Show("Account or password error, please re-enter", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                conn.Close();
            }
            return;

1.3 student information display

  code implementation

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;
using System.IO;
/*Partial code*/
namespace pxscj
{
    public partial class Stu : Form
    {
        static string config = "database=pxscj;" +
                        "datasource = 127.0.0.1;" +
                        "userid = root;" +
                        "port=3306;" +
                        "password=******"; //Database connection fields

        private static string path = ""; //Path to photo file
        MySqlConnection conn = new MySqlConnection(config);
        public Stu(string id)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            label1.Text = id;
            try
            { conn.Open(); }
            catch
            {
                MessageBox.Show("Database connection failed!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.Hide();
                return;
            }
        }
        //Show all
        private void Stu_Load(object sender, EventArgs e)
        {
            //Display personal information
            DataSet myDs = new DataSet();
            string mySqlStr = "select * from XS where ID=" + label1.Text.ToString() + ";";
            MySqlCommand cmd = new MySqlCommand(mySqlStr, conn); //sentence
            MySqlDataReader dr = cmd.ExecuteReader(); //Read data with DataReader
                if (dr.Read())
                {
                    label2.Text = dr.GetString(1).ToString();//Pass data into boxes
                    if (dr.GetString(2).ToString() == "1")// Judge gender
                        label3.Text = "male";
                    else
                        label3.Text = "female";
                    label4.Text = dr.GetString(3).ToString();
                    label5.Text = dr.GetString(4).ToString();
                
            }
            dr.Close();

            // Displays the course name
    
        }
    }
 

2 student management main interface

  code implementation

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 pxscj
{
    public partial class Choose : Form
    {
        static string conflg = "database=pxscj;" +
                       "datasource = 127.0.0.1;" +
                       "userid = root;" +
                       "port=3306;" +
                       "password=*******"; //Database connection fields
        MySqlConnection conn = new MySqlConnection(conflg);
        static string sno = "";//Receive student number text box
        public Choose()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            try
            { conn.Open(); }
            catch
            {
                MessageBox.Show("Database connection failed!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }

        }
        //View students
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox_sno.Text != "")
            {
                string str = "SELECT * FROM XS WHERE ID ='" + textBox_sno.Text.ToString() + "';";
                MySqlCommand pcmd = new MySqlCommand(str, conn);
                MySqlDataReader dr = pcmd.ExecuteReader(); //Read data with DataReader
                if (dr.Read()) //Determine whether there is the student
                {
                    sno = textBox_sno.Text.ToString();
                    Stu stu = new Stu(sno);
                    stu.Show();
                }
                else
                {
                    MessageBox.Show("No such student!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                dr.Close();
     
            }
            else
            {
                MessageBox.Show("Please enter the student ID you want to view", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

            }
          
        }
        // Add student
        private void button2_Click(object sender, EventArgs e)
        {
            Addstu add = new Addstu();
            add.Show();
            NEW_LOAD(null, null);
        
        }
        //Modify student
        private void button3_Click(object sender, EventArgs e)
        {
            
 
        }
        //Delete this student

        private void button4_Click(object sender, EventArgs e)
        {


        }
        //Show all students
        private void Choose_Load(object sender, EventArgs e)
        {
            NEW_LOAD(null, null);
        }
        public void NEW_LOAD(object sender,EventArgs e)
        {
            string kcm = "SELECT ID AS Student number, XM AS full name, CSSJ AS time of birth, KCS AS Number of courses FROM XS";

            MySqlDataAdapter da = new MySqlDataAdapter(kcm, conn);// Open data adapter (for reading data)
            DataSet ds = new DataSet();//Create data table
            da.Fill(ds);
            dataGridView_stu.DataSource = ds.Tables[0];// Combine dataset with dataGridView_xmcj data binding together
            textBox_sno.Text = "";
        }


        private void button5_Click_1(object sender, EventArgs e)
        {
            conn.Close();
            this.Hide();
            return;
        }
    }
}

2.1 modify student function realization

         code implementation

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 System.IO;
using MySql.Data.MySqlClient;
namespace pxscj
{
    public partial class Upstu : Form
    {
        static string config = "database=pxscj;" +
                        "datasource = 127.0.0.1;" +
                        "userid = root;" +
                        "port=3306;" +
                        "password=******"; //Database connection fields

        private static string path = ""; //Path to photo file
        MySqlConnection conn = new MySqlConnection(config); //Connect to database
        public Upstu(string id)
        {
            InitializeComponent();
            label2.Text = id;
            this.StartPosition = FormStartPosition.CenterScreen;
            try
            { conn.Open(); }
            catch
            {
                MessageBox.Show("Database connection failed!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
        }

        private void Upstu_Load(object sender, EventArgs e)
        {

        }
        // Modify information
        private void button_yes_Click(object sender, EventArgs e)
        {
            try
            {

                // Judge whether the information is complete
                if (textBox_name.Text == "")
                {
                    MessageBox.Show("Name cannot be empty", "User update", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else if (textBox_cssj.Text == "")
                {
                    MessageBox.Show("Birth time cannot be empty", "User update", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else if (!radioButton_male.Checked && !radioButton_female.Checked)
                {
                    MessageBox.Show("Gender cannot be empty", "User update", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else
                {
                    // Save field
                    string name = textBox_name.Text.ToString();
                    string cssj = textBox_cssj.Text.ToString();
                    int xb = 0;//The default gender is female
                    if (radioButton_male.Checked) xb = 1;
                    // Judge whether there is photo insertion (no photo by default)
                    string str = "UPDATE  XS SET XM='" + name + "',XB='" + xb + "',CSSJ='" + cssj + "'  WHERE ID='" + label2.Text + "'";
                    if (path != "")
                        str = "UPDATE XS SET XM='" + name + "',XB='" + xb + "',CSSJ='" + cssj + "',ZP=@Photo  WHERE ID='" + label2.Text + "'";
                    MySqlCommand cmd = new MySqlCommand(str, conn);
                    if (path != "")
                    {
                        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);//Save the picture as a file stream
                        byte[] fileBytes = new byte[fs.Length];//Create byte array
                        BinaryReader br = new BinaryReader(fs);
                        fileBytes = br.ReadBytes(Convert.ToInt32(fs.Length));
                        MySqlParameter mpar = new MySqlParameter("@Photo", SqlDbType.Image);//Create parameters for the command
                        mpar.MySqlDbType = MySqlDbType.VarBinary;
                        mpar.Value = fileBytes; //Assign values to parameters
                        cmd.Parameters.Add(mpar);// Add parameter
                    }
                    //Execute the statement in MYSQl
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Modification succeeded!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    conn.Close();
                    this.Hide();


                }
            }
            catch
            {
                MessageBox.Show("Modification failed,Please check the information", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }

            return;
        }
        //Select accept photos
        private void button_selectphoto_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            //Format accepted pictures
            openDialog.Filter = "bmp file(*.bmp)|bmp|gif file(*gif)|*.gif|jpeg file(*jpg)|*.jpg";
            openDialog.FilterIndex = 3;
            openDialog.Title = "Select photos";//Sets the name of the dialog box
            if (openDialog.ShowDialog() == DialogResult.OK)
                path = openDialog.FileName;//Get path
            pictureBox_stu.Image = Image.FromFile(path);
            MessageBox.Show(path);//Test whether to save the path
            return;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            conn.Close();
            this.Hide();
            return;
        }
    }
}

2.2 add student function realization

code implementation

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

namespace pxscj
{
    public partial class Addstu : Form
    {
        static string config = "database=pxscj;" +
                        "datasource = 127.0.0.1;" +
                        "userid = root;" +
                        "port=3306;" +
                        "password=*******"; //Database connection fields

        private static string path = ""; //Path to photo file
        public Addstu()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }
        // Add student
        private void button_yes_Click(object sender, EventArgs e)
        {
            try
            {
                MySqlConnection conn = new MySqlConnection(config); //Connect to database
                conn.Open(); //Open database
                             // Judge whether the information is complete
                if (textBox_id.Text == "")
                {
                    MessageBox.Show("Student number cannot be empty", "User Login ", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

                }
                else if (textBox_name.Text == "")
                {
                    MessageBox.Show("Name cannot be empty", "User Login ", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else if (textBox_cssj.Text == "")
                {
                    MessageBox.Show("Birth time cannot be empty", "User Login ", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else if (!radioButton_male.Checked && !radioButton_female.Checked)
                {
                    MessageBox.Show("Gender cannot be empty", "User Login ", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else
                {
                    // Save field
                    string id = textBox_id.Text.ToString();
                    string name = textBox_name.Text.ToString();
                    string cssj = textBox_cssj.Text.ToString();
                    int xb = 0;//The default gender is female
                    if (radioButton_male.Checked) xb = 1;
                    // Judge whether there is photo insertion (default is there is photo)
                    string str = "INSERT INTO XS VALUES('" + id + "','" + name + "','" + xb + "','" + cssj + "',0,NULL)";
                    if (path != "")
                        str = "INSERT INTO XS VALUES('" + id + "','" + name + "','" + xb + "','" + cssj + "',0,@Photo)";
                    MySqlCommand cmd = new MySqlCommand(str, conn);
                    if (path!="")
                    {
                        FileStream fs = new FileStream(path, FileMode.Open,FileAccess.Read);//Save the picture as a file stream
                        byte[] fileBytes = new byte[fs.Length];//Create byte array
                        BinaryReader br = new BinaryReader(fs);
                        fileBytes = br.ReadBytes(Convert.ToInt32(fs.Length));
                        MySqlParameter mpar = new MySqlParameter("@Photo", SqlDbType.Image);//Create parameters for the command
                        mpar.MySqlDbType = MySqlDbType.VarBinary;
                        mpar.Value = fileBytes; //Assign values to parameters
                        cmd.Parameters.Add(mpar);// Add parameter
                    }
                    //Execute the statement in MYSQl
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Successfully added!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    conn.Close();
                    this.Hide();
                    

                }
            }
            catch
            {
                MessageBox.Show("Add failed,Please check the information", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            return;
        }
        // Select Photo button event
        private void button_selectphoto_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            //Format accepted pictures
            openDialog.Filter = "bmp file(*.bmp)|bmp|gif file(*gif)|*.gif|jpeg file(*jpg)|*.jpg";
            openDialog.FilterIndex = 3;
            openDialog.Title = "Select photos";//Sets the name of the dialog box
            if (openDialog.ShowDialog() == DialogResult.OK)
                path = openDialog.FileName;//Get path
            pictureBox_stu.Image = Image.FromFile(path);
            //MessageBox.Show(path);// Test whether to save the path

        }
    }
}

3. Achievement management main interface function realization

code implementation

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 pxscj
{
    public partial class CJstu : Form
    {
        static string conflg = "database=pxscj;" +
                        "datasource = 127.0.0.1;" +
                        "userid = root;" +
                        "port=3306;" +
                        "password=*********"; //Database connection fields
        MySqlConnection conn = new MySqlConnection(conflg);
        public CJstu()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            //Database connection
            try
            { conn.Open(); }
            catch
            {
                MessageBox.Show("Database connection failed!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }

        }
        // Delete grade
        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox_Sno.Text != "")
            {
                string str = "SELECT * FROM XS WHERE ID ='" + textBox_Sno.Text.ToString() + "';";
                MySqlCommand pcmd = new MySqlCommand(str, conn);
                MySqlDataReader dr = pcmd.ExecuteReader(); //Read data with DataReader
                if (dr.Read()) //Determine whether there is the student
                {
                    DelCJ dj = new DelCJ(textBox_Sno.Text.ToString());
                    dj.Show();
                }
                else
                {
                    MessageBox.Show("No such student!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                dr.Close();
            }
            else
            {
                MessageBox.Show("Please enter the student ID you want to view", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            NEW_LOAD(null, null);
           
        }
        // Add grades
        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox_Sno.Text != "")
            {
                string str = "SELECT * FROM XS WHERE ID ='" + textBox_Sno.Text.ToString() + "';";
                MySqlCommand pcmd = new MySqlCommand(str, conn);
                MySqlDataReader dr = pcmd.ExecuteReader(); //Read data with DataReader
                if (dr.Read()) //Determine whether there is the student
                {
                    AddCJ ac = new AddCJ(textBox_Sno.Text.ToString());
                    ac.Show();
                }
                else
                {
                    MessageBox.Show("No such student!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                dr.Close();
            }
            else
            {
                MessageBox.Show("Please enter the student ID you want to view", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            NEW_LOAD(null, null);
   
        }
        // course management
        private void button3_Click(object sender, EventArgs e)
        {
            UpKC kc = new UpKC();
            kc.Show();
            NEW_LOAD(null, null);
            return;
        }
        //Page loading
        private void CJstu_Load(object sender, EventArgs e)
        {
            NEW_LOAD(null, null);
  
        }
        // Displays the courses learned by all students
        public void NEW_LOAD(object sender, EventArgs e)
        {
            string kcm = "SELECT XS.ID AS Student number, XS.XM AS full name,KC.KCM AS Courses taken,CJ.CJ AS achievement FROM XS,CJ,KC WHERE XS.ID=CJ.ID AND CJ.KCH=KC.KCH";

            MySqlDataAdapter da = new MySqlDataAdapter(kcm, conn);// Open data adapter (for reading data)
            DataSet ds = new DataSet();//Create data table
            da.Fill(ds);
            dataGridView_stu.DataSource = ds.Tables[0];// Combine dataset with dataGridView_xmcj data binding together
            textBox_Sno.Text = "";
          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            conn.Close();
            this.Hide();
            return;
        }
    }
}

3.1 add grades

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 pxscj
{
    public partial class AddCJ : Form
    {
        static string conflg = "database=pxscj;" +
                        "datasource = 127.0.0.1;" +
                        "userid = root;" +
                        "port=3306;" +
                        "password=******"; //Database connection fields
        MySqlConnection conn = new MySqlConnection(conflg);// Database connection
        public AddCJ(string id)
        {
            InitializeComponent();
            label2.Text = id;
            this.StartPosition = FormStartPosition.CenterScreen;
            try
            { conn.Open(); }
            catch
            {
                MessageBox.Show("Database connection failed!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
        }
        //Initialize loading course name
        private void AddCJ_Load(object sender, EventArgs e)
        {
            
            string str = "SELECT KCM FROM KC";
            MySqlDataAdapter pcmd = new MySqlDataAdapter(str, conn);
            DataSet da = new DataSet();
            comboBox_KC.Items.Add("Please select");
            pcmd.Fill(da, "KCM");
            for(int i = 0; i<da.Tables["KCM"].Rows.Count;i++)
            {
                comboBox_KC.Items.Add(da.Tables["KCM"].Rows[i][0].ToString());
            }
            comboBox_KC.SelectedIndex = 0;//Default initial display
            textBox_cj.Text = "";
            return;
        }

        private void comboBox_KC_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        // OK button event
        private void button_yes_Click(object sender, EventArgs e)
        {
            try
            {

                
                string cj = textBox_cj.Text.ToString();
                //Receive the selection result of combox
                int kc = comboBox_KC.SelectedIndex;
                string str = "INSERT INTO CJ VALUES('" + label2.Text + "','" + kc + "','" + cj + "')";
                MySqlCommand cmd = new MySqlCommand(str, conn);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Added successfully!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            catch
            {
                MessageBox.Show("Add failed!Please check the information!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            conn.Close();
            this.Hide();
      

        }
    }
}

3.2 adding courses

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 pxscj
{

    public partial class UpKC : Form
    {
        static string conflg = "database=pxscj;" +
                       "datasource = 127.0.0.1;" +
                       "userid = root;" +
                       "port=3306;" +
                       "password=55555"; //Database connection fields
        MySqlConnection conn = new MySqlConnection(conflg);
        static string id = ""; //Number of courses saved
       
        public UpKC()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            try
            { conn.Open(); }
            catch
            {
                MessageBox.Show("Database connection failed!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
        }

        // Add course
        private void button_yes_Click(object sender, EventArgs e)
        {
            if (textBox_kc.Text == "")
            {
                MessageBox.Show("Please enter the course name", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                try
                {
                    string kc_name = textBox_kc.Text.ToString();
                    string str = "INSERT INTO KC VALUES('" + id + "',  '" + kc_name + "',NULL);";// Insert statement
                    MySqlCommand kcmd = new MySqlCommand(str, conn);
                    kcmd.ExecuteNonQuery();
                    MessageBox.Show("Added successfully", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    NEW(null, null);
                }
                catch
                {
                    MessageBox.Show("Add failed!The course already exists", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
            return;
        }
        //Delete course
        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox_kc.Text == "")
            {
                MessageBox.Show("Please enter the course name", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                try
                {
                    string kc_name = textBox_kc.Text;
                    string str = "DELETE FROM KC WHERE KCM='" + kc_name + "';";
                    MySqlCommand cmd = new MySqlCommand(str, conn);
                    MessageBox.Show("Delete succeeded!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    cmd.ExecuteNonQuery();
                    NEW(null, null);
                }
                catch
                {
                    MessageBox.Show("Deletion failed!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
            return;
        }
        //Load all courses
        private void UpKC_Load(object sender, EventArgs e)
        {
            NEW(null, null);
            return;
        }
        private void NEW(object sender,EventArgs e)
        {
            string kcm = "SELECT  KCM AS Course name FROM KC ";
            MySqlDataAdapter da = new MySqlDataAdapter(kcm, conn);// Open data adapter (for reading data)
            DataSet ds = new DataSet();//Create data table
            da.Fill(ds);
            dataGridView_kc.DataSource = ds.Tables[0];// Combine dataset with dataGridView_xmcj data binding together
            id= dataGridView_kc.Rows.Count.ToString();//Get the total number of courses
            textBox_kc.Text = "";
            return;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            conn.Close();
            this.Hide();
            return;
        }
    }
}

Complete project source code link


https://pan.baidu.com/s/104uMaDW65VMTitiGYKMliw

Extraction code: KY65
 


summary

The newcomer may not write well for the first time. If the boss has any suggestions, he can leave your valuable suggestions in the comment area, which is really important to me!!!!!!!!!!!!

Tags: C# MySQL Back-end

Posted on Sat, 30 Oct 2021 10:33:31 -0400 by Maugrim_The_Reaper