Source code analysis of student information management system

Part of the core source code analysis, the project has been open source, see the complete code, the bottom of the article has a link.

Student information management system is divided into three parts

What is shown now is the result of the second development of the original system. Is version 2.0.

1, Interface design

1. New project

Select Windows Forms application when creating a new project

2. Use of toolbox

Using Windows Forms application, the code of the interface does not need to be written by yourself. Drag and match the controls in the toolbox directly. It's relatively simple.

3. Property settings for control

Each control has many properties, which can be set for different scenes.

All interfaces can be designed before logic programming

2, Code analysis

1. Link code of database

Because all data of the system is stored in the database, the database link is performed first. There are two ways to link:

(1) . write the database link statement directly in the trigger event (the database is stored in the Debug folder)

string wa;
OleDbConnection a1 = new OleDbConnection();
OleDbCommand a2 = new OleDbCommand();
//Database connection statement
a1.ConnectionString = "Provider=Microsoft.ace.OLEDB.12.0;Data Source= Student information management database.accdb";
a1.Open();

(2) . integrate the database connection, and directly call the file when using

Create a new class named data.cs to store database connection statements

data.cs file code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class data
    {
        //For database connection integration, you do not need to write database connection statements for each From
        public static string mystr = "provider =microsoft.ace.oledb.12.0;data source=Student information management database.accdb";
    }
}
Write as follows when calling:
string wa;
OleDbConnection a1 = new OleDbConnection();
OleDbCommand a2 = new OleDbCommand();
a1.ConnectionString = data.mystr; //Call the data file to get the database connection code
a1.Open();

2. Query (take login as an example)

It is also a query process for users to log in. The user enters the user name, password and role, and first queries whether the data is true. Then according to the role to judge whether to jump to the student side, the teacher side or the background management. The home code is as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /*
         *Event triggered by login button
         */
        private void button1_Click(object sender, EventArgs e)
        {
            string wa;
            OleDbConnection a1 = new OleDbConnection();
            OleDbCommand a2 = new OleDbCommand();
            //Database connection
            a1.ConnectionString = "Provider=Microsoft.ace.OLEDB.12.0;Data Source= Student information management database.accdb";
            a1.Open();
            //Find the corresponding user name and password in the database
            wa = "Select * from User table where user name='" + textBox1.Text.Trim() + "'and Password='" + textBox2.Text.Trim() + "'and role='" + comboBox1.Text.Trim() + "'";
            a2.CommandText = wa;
            a2.Connection = a1;
            a2.ExecuteScalar();
            //Obtain data to prove that the input account is correct
            if (null != a2.ExecuteScalar())
            {
                //Judge whether the account type is student
                if (comboBox1.Text == "Student")
                {
                    MessageBox.Show("Welcome to the student side of the information management system!", "Login succeeded!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Visible = false;
                    Form2 f2 = new Form2();   //Form2 is the main interface of student end
                    f2.ShowDialog();
                    this.Visible = true;
                }
                else
                {
                    //Judge whether the account type is a teacher
                    if (comboBox1.Text == "Teacher")
                    {
                        MessageBox.Show("Welcome to the information management system teachers!", "Login succeeded!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Visible = false;
                        Form4 f3 = new Form4();  //Form4 is the main interface of Teachers
                        f3.ShowDialog();
                        this.Visible = true;
                    }
                    else
                    {
                        //Judge whether the account type is administrator
                        MessageBox.Show("Welcome to background management mode!", "Login succeeded!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Visible = false;
                        Form16 f4 = new Form16();  //Form2 is the main background management interface
                        f4.ShowDialog();
                        this.Visible = true;

                    }
                }
            }
            else  //Wrong account or password
            {
                MessageBox.Show("Login failed! Please enter the correct user name, password and role!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
               
            }
        }

        /*
       *Registration button trigger event
       */
        private void button2_Click(object sender, EventArgs e)
        {
            Form32 f2 = new Form32();  //Form32 is the registration interface
            f2.ShowDialog();
        }

        /*
       *Cancel button trigger event
       */
        private void button3_Click(object sender, EventArgs e)
        {
            //wipe data 
            textBox1.Text = "";  
            textBox2.Text = "";
            comboBox1.Text = "";

        }

        /*
       *Bottom copyright hyperlink
       */
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
           linkLabel1.LinkVisited = true;
            //Open with IE browser
           System.Diagnostics.Process.Start("IExplore", "https://www.cztcms.cn");
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

3. Add operation (take registration as an example)

User registration involves querying and adding. First, query whether the user exists. If not, you can register. Registration interface code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form32 : Form
    {
        public Form32()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            { MessageBox.Show("Please enter the user name!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
            else
                if (textBox2.Text == "")
                { MessageBox.Show("Please enter the user password!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
                else
                    if (comboBox1.Text == "")
                    { MessageBox.Show("Please select user role!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); }

                    else
                        if (textBox2.Text == textBox3.Text)
                        {


                            string wa;
                            OleDbConnection a1 = new OleDbConnection();
                            OleDbCommand a2 = new OleDbCommand();
                            a1.ConnectionString = "Provider=Microsoft.ace.OLEDB.12.0;Data Source= Student information management database.accdb";
                            a1.Open();
                            wa = "Select * from User table where user name='" + textBox1.Text.Trim() + "'";
                            a2.CommandText = wa;
                            a2.Connection = a1;
                            a2.ExecuteScalar();
                            if (null != a2.ExecuteScalar())
                            {
                                MessageBox.Show("login has failed,The user name already exists!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                            }
                            else
                            {
                            //Add data to database
                                wa = "INSERT into User table values(" + "'" + textBox1.Text.Trim() + "','" + textBox2.Text.Trim() + "','" + comboBox1.Text.Trim() + "')";
                                a2.CommandText = wa;
                                a2.Connection = a1;
                                a2.ExecuteNonQuery();
                                MessageBox.Show("User registration successful,Please login!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                               Application.OpenForms["form32"].Close();
                              

                            }
                        }
                        else
                        {
                            MessageBox.Show("The two passwords are different!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                        }


        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void Form32_Load(object sender, EventArgs e)
        {

        }
    }
}

4. Modification (take password modification as an example)

  private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            { MessageBox.Show("Please enter the user name!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
            else
                if (textBox2.Text == "")
                { MessageBox.Show("Please enter the user password!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
                else
                    if (textBox3.Text == "")
                    { MessageBox.Show("Please input the password again!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
                    else

                        if (textBox2.Text == textBox3.Text)
                        {
                            string wa;
                            OleDbConnection a1 = new OleDbConnection();
                            OleDbCommand a2 = new OleDbCommand();
                            a1.ConnectionString = "Provider=Microsoft.ace.OLEDB.12.0;Data Source= Student information management database.accdb";
                            a1.Open();


                            string sql = "select * from User table where user name='" + textBox1.Text.Trim() + "'";
                            a2.CommandText = sql;
                            a2.Connection = a1;
                            OleDbDataReader a3 = a2.ExecuteReader();
                            if (a3.Read() == false)
                            { MessageBox.Show("This information does not exist in the system!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
                            else
                            {
                                MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
                                DialogResult dr = MessageBox.Show("Are you sure you want to modify this information!", "Modify information", messButton, MessageBoxIcon.Information);
                                if (dr == DialogResult.OK)
                                {
                                    a3.Close();
                                    wa = "update User table set Password='" + textBox2.Text.Trim() + "' where user name='"+textBox1 .Text .Trim ()+"'";
                                    a2.CommandText = wa;
                                    a2.Connection = a1;
                                    a2.ExecuteNonQuery();
                                    MessageBox.Show("Information modified successfully!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }

                            }
                        }
else
                            {
                                MessageBox.Show("Two different passwords!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }


                        }

5. Delete operation

  private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            { MessageBox.Show("Please enter the user name!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
            else
                if (textBox2.Text == "")
                { MessageBox.Show("Please query user information first!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
                else

                    if (comboBox1.Text == "")
                    { MessageBox.Show("Please query user information first!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); }

                    else
                    {
                        OleDbConnection a1 = new OleDbConnection();
                        OleDbCommand a2 = new OleDbCommand();
                        a1.ConnectionString = data.mystr;
                        a1.Open();
                        MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
                        DialogResult dr = MessageBox.Show("Are you sure you want to delete this user!", "delete user", messButton, MessageBoxIcon.Information);
                        if (dr == DialogResult.OK)
                        {
                            string wa;
                            wa = "delete * from User table where user name='" + textBox1.Text.Trim() + "' and Password='" + textBox2.Text.Trim() + "'and role='" + comboBox1.Text.Trim() + "' ";
                            a2.CommandText = wa;
                            a2.Connection = a1;
                            a2.ExecuteNonQuery();

                            { MessageBox.Show("User deleted successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); }
                        }
                    }
        }

6. Add picture

There are two difficulties in adding personal photos when adding personal information. One is to get the local file, the other is to get the format of the uploaded image. First, add the following code to the Main class (Program). Note: it is not the entry class Main
 //Define member variables for adding pictures
 public static string Imagefile;
 public static string FileName;
Then write the following code in the control of adding pictures:
OpenFileDialog dr = new OpenFileDialog();
dr.ShowDialog();
dr.Filter = "image file (*.bmp;*.jpg)|*.bmp;*.jpg";
string imagefile = dr.FileName;
pictureBox1.ImageLocation = imagefile;
Program.Imagefile = imagefile;

7. Display the data of the database on the dataGridView control

The dataGridView control can display the data in the form of tables, which is very good for displaying course information, grades and so on. The main codes are as follows:
//Query the contents of the entire table
string sql1 = "select * from  Student information form ";
OleDbDataAdapter ad1 = new OleDbDataAdapter(sql1, a1);
DataSet ds1 = new DataSet();
ad1.Fill(ds1);
//Display data as a list
dataGridView1.DataSource = ds1.Tables[0];
MessageBox.Show("Retrieved successfully!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
In fact, the function of the whole system is to add, delete, check and modify the data. There are many vulnerabilities in the whole system. For example, the data restrictions during login and registration, especially the security and role permissions. A lot of holes. But at that time, it was pretty good to write such a system on the premise that there was little programming ability. This time, I just recorded my experience of C programming. As for the system, it will not be updated and maintained.

Please look forward to the next project packaging and installation!

Project open source address (for learning reference only)

               

Tags: Programming Database Windows SQL

Posted on Fri, 08 May 2020 21:59:37 -0400 by steelaz