[C#: Student Information Management System-Part Code 1]

Create the project Windows Forms Application:

Create a DBhelp.cs class, connect to the database, just call this class every time: call this: DBhelp.conn.Open();

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

namespace Win9_28
{
    public static class DBhelp
    {
        static string  strconn = "Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
        public static SqlConnection conn = new SqlConnection(strconn);
        public static SqlCommand comm = new SqlCommand();
        //public static SqlDataReader read = comm.ExecuteReader();
    }
}

1. Click the item to add item: Windows Form, write the login form: Login.cs:

First: Modify the Test property of the form to log in, and the (Name) property to frmLogin

Add a picture control PictureBox control, (Name) named: pictureBox1, Image property: import picture, SizeMode property: StretchImage to display the whole picture

Add a Label control (Name) named: lblUserName, Text: user name, input box control TextBox, (Name): txtUserName

Add a Label control (Name) named: lblUserPwd, Text: password, input box control TextBox, (Name): txtUserPwd, if you want the password not displayed, set the property PasswordChar: *

Add 2 Button controls, the former (Name): btnRegister Text: Login, the latter (Name): btnESC Text: Cancel

The style is as follows:

The method in the login button: double-click the login button: a login button event will be automatically generated in the background qu

private void btnRegister_Click(object sender, EventArgs e)
        {
            string strconn = "Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
            SqlConnection conn = new SqlConnection(strconn);//创建SqlConnection对象,连接数据库
            string name = txtUserName.Text;//获取姓名文本框的内容
            string pwd = txtUserPwd.Text;//获取密码文本框的内容
            try
            {
                conn.Open();
                string sqlstr=string.Format("select * from tb_User where UserName='{0}' and UserPasswd='{1}'",name,pwd);
                SqlCommand comm = new SqlCommand();//创建sqlCommand对象,执行SQL语句
                comm.Connection = conn;//执行SqlCommand对象的Connection属性,设置Command对象的Connection对象
                comm.CommandText = sqlstr;//执行SqlCommand对象的的CommandText属性,设置Command对象的sql语句

                /*
                if (name == string.Empty || pwd == string.Empty)
                {
                    MessageBox.Show("用户名或密码不能为空", "系统提示");
                }*/


                if (comm.ExecuteScalar() == null)//ExecuteScalar()返回查询结果集中的第一行第一列,没有返回null
                {
                    MessageBox.Show("用户名与密码不匹配,登录失败");//提示信息
                    this.txtUserName.Clear();
                    this.txtUserPwd.Clear();
                    this.txtUserName.Focus();

                }
                else 
                {
                    //this.Hide();//此页面隐藏
                    frmMain frmmain = new frmMain();//创建新的页面
                    frmmain.Show();//打开新的页面
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
             conn.Close();
            }
        }

The method of canceling the button, double-click the cancel button:

private void btnESC_Click(object sender, EventArgs e)
        {
            Application.Exit();//退出程序,结束运行
        }

2. Add college page: frmAddCollege.cs

 Change the Text property of the form: add department information, (Name): property: frmAddCollege

Add two Lable controls, add two text input box controls TextBox, named: txtDepartmentID, txtDepartmentName, add two button controls Button and command for them, change the Text value required

Double-click the Add button to add an OnClick event for the Add button. The code is as follows:

private void btnAdd_Click(object sender, EventArgs e)
        {
            string conn = "Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
            SqlConnection a = new SqlConnection(conn);//创建Connection对象

            if (txtDepartmentID.Text == "" || txtDepartmentName.Text == "")
            {
                MessageBox.Show("院系编号或院系名称不能为空!");
            }
            else
            {
                try
                {
                    a.Open();
                    string sqlstr = string.Format("insert into tb_College values('{0}','{1}')", txtDepartmentID.Text, txtDepartmentName.Text);

                    SqlCommand comm = new SqlCommand();//创建Command 对象执行SQL语句
                    comm.Connection = a;
                    comm.CommandText = sqlstr;
                    //int result = comm.ExecuteNonQuery();

                    if (comm.ExecuteNonQuery() > 0)
                    {
                        MessageBox.Show("插入成功");
                    }
                    else
                    {
                        MessageBox.Show("插入失败");
                    }
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    a.Close();
                }
            }
        }

Add a click event for the cancel button to close this page:

private void btnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

2. Create frmEditCollege.cs form

 

 Modify the form (Name) as: frmEditCollege, Text: Faculty Information System

Add a data control: dataGridView control, click the small triangle above, add data source - select data source - add data source - database - data set - table inside, add the data source

In adding two Label controls, the Text is named: Department Number:, Department Name, add two text boxes, and add 4 buttons

When you want the data of the store data source to be displayed in the lower text box, double add the data control and add the following code: 

​​ private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtCollegeNo.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            txtCollegeName.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
        }

For the query button, add a click event, the code is as follows:

private void btnSelect_Click(object sender, EventArgs e)
        {
            string conn = "Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
            SqlConnection a = new SqlConnection(conn);

            try
            {
                a.Open();
                string sqlstr = string.Format("select * from tb_College where DepartmentID='{0}'", txtCollegeNo.Text);//Format转化为字符串
                SqlCommand comm = new SqlCommand();
                comm.Connection = a;
                comm.CommandText = sqlstr;

                SqlDataReader read = comm.ExecuteReader();//创建DataReader对象,在数据库中读取数据
                if (read.Read())
                {
                    txtCollegeName.Text = read[1].ToString();
                }
                else
                {
                    MessageBox.Show("查询结果不存在!");
                    txtCollegeName.Text = "";
                }


            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                a.Close();
            }
        }

Double-click the delete button to add a click event:

private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                DBhelp.conn.Open();
                string sqlstr = string.Format("delete from tb_College where DepartmentID='{0}'and DepartmentName='{1}' ", txtCollegeNo.Text, txtCollegeName.Text);
                DBhelp.comm.CommandText = sqlstr;
                DBhelp.comm.Connection = DBhelp.conn;

                if ((int)DBhelp.comm.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show("删除成功");

                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBhelp.conn.Close();
            }
        }

Double-click the Modify button to add a click event:

 private void btnCollege_Click(object sender, EventArgs e)
        {
            /*string conn = "Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
            SqlConnection a = new SqlConnection(conn);

            if (txtCollegeNo.Text == "" || txtCollegeName.Text == "")
            {
                MessageBox.Show("院系编号或院系名称不能为空!");
            }
            else
            {
                try
                {
                    a.Open();
                    string sqlstr = string.Format("insert into tb_College values('{0}','{1}')", txtCollegeNo.Text, txtCollegeName.Text);

                    SqlCommand comm = new SqlCommand();
                    comm.Connection = a;
                    comm.CommandText = sqlstr;
                    int result = comm.ExecuteNonQuery();

                    if (result != 0)
                    {
                        MessageBox.Show("插入成功");
                    }
                    else
                    {
                        MessageBox.Show("插入失败");
                    }
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    a.Close();
                }
            }*/
            //上面代码无用
            try
            {
                DBhelp.conn.Open();
                string sqlstr = string.Format("update tb_College set DepartmentName='{0}' where DepartmentID='{1}'",txtCollegeName.Text,txtCollegeNo.Text);
                DBhelp.comm.CommandText = sqlstr;
                DBhelp.comm.Connection = DBhelp.conn;

                if ((int)DBhelp.comm.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show("修改成功");

                }
                else
                {
                    MessageBox.Show("修改失败");
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBhelp.conn.Close();
            }
        }

Click the Cancel button and add a click event:

 private void btnCollegeClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

When the setting page is initially selected the department ID, you need to set the page to load frmCollege_Load code:

private void frmCollege_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“studentInfDataSet3.tb_College”中。您可以根据需要移动或删除它。
            this.tb_CollegeTableAdapter.Fill(this.studentInfDataSet3.tb_College);
            string conn = "Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
            SqlConnection a = new SqlConnection(conn);

            try
            {
                a.Open();
                string sqlstr = string.Format("select * from tb_College where DepartmentName='医学院'");
                SqlCommand comm = new SqlCommand();
                comm.Connection = a;
                comm.CommandText = sqlstr;

                SqlDataReader read = comm.ExecuteReader();
                if (read.Read())
                {
                    txtCollegeNo.Text = read[0].ToString();
                }

                
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                a.Close();
            }



            try
            {
                DBhelp.conn.Open();
                string sqlstr = "select * from tb_College";
                SqlDataAdapter datadapter = new SqlDataAdapter(sqlstr, DBhelp.conn);
                DataSet set = new System.Data.DataSet();
                datadapter.Fill(set);
                dataGridView1.DataSource = set.Tables[0];

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBhelp.conn.Close();
            }
        }

3. Create an add student page, frmAddStudent.cs:

 Add Label named student number, input box TextBox control Name Label input box TextBox, add a Panel container and put two radio button controls RadioButton named male and female 

Add a Label control to name the date of birth, and an input box TextBox control

Add a Label control named: class, add a drop-down box control ComboBox control, click the small triangle above the ComboBox control, select the use data binding item, bind the table in the database

... 

 Add two buttons Button named, add, exit

Double-click the Add button to add a click event:

 private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DBhelp.conn.Open();
                string sqlstr = string.Format("insert into tb_Student values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", txtstudentID.Text, txtStudentName.Text, radmale.Checked ? radFamale.Text : radmale.Text, txtBirthday.Text, cmbClassID.Text, txtMobilePhone.Text, txtAddress.Text);
                DBhelp.comm.CommandText = sqlstr;
                DBhelp.comm.Connection = DBhelp.conn;

                if ((int)DBhelp.comm.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show("插入成功");

                }
                else
                {
                    MessageBox.Show("插入失败!");
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBhelp.conn.Close();
            }
        }

Double-click the exit button and add a click event:

 private void btnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

See the follow-up code: Student Information Management System - Section Code 2

 

Guess you like

Origin blog.csdn.net/dengfengling999/article/details/123717619