【C#:学生信息管理系统-部分代码1】

创建项目Windows窗体应用程序:

创建一个DBhelp.cs类,连接数据库,每次只用调用这个类就行了:这样调用: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.点击项目添加项:Windows窗体,编写登录窗体:Login.cs:

首先:修改窗体的Test属性为登录、(Name)属性命名为frmLogin

添加一个图片控件PictureBox控件,(Name)命名为:pictureBox1, Image属性:导入图片,SizeMode属性:StretchImage来显示整张图片

添加一个Label控件 (Name)命名:lblUserName,Text:用户名 ,输入框控件TextBox,(Name):txtUserName

添加一个Label控件 (Name)命名:lblUserPwd ,Text:密码 ,输入框控件TextBox,(Name):txtUserPwd,如果想密码不显示设置属性PasswordChar:*

添加2个Button控件,前一个(Name):btnRegister Text:登录,后一个(Name):btnESC Text:取消

样式如下:

登录按钮里的方法:双击登录按钮:会在后台自动生成一个登录按钮的事件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();
            }
        }

取消按钮的方法,双击取消按钮:

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

2. 添加学院页面:frmAddCollege.cs

 更该窗体的Text属性:添加院系信息,(Name):属性:frmAddCollege

添加两个Lable控件,添加两个文本输入框控件TextBox,命名为:txtDepartmentID、txtDepartmentName,添加两个按钮控件Button  并为它们命令,更改需要该Text值

双击添加按钮,为添加按钮添加OnClick点击事件,代码如下:

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();
                }
            }
        }

为取消按钮添加点击事件,关闭此页面:

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

2.创建frmEditCollege.cs窗体

 

 修改窗体(Name)为:frmEditCollege,Text:院系信息系统

添加一个数据控件:dataGridView控件,点击上方的小三角,添加数据源-选择数据源-添加数据源-数据库-数据集-里面的表,就把数据源添加进来了

在添加两个Lable控件,Text命名为:院系编号:、院系名称,添加两个文本框,添加4个按钮

当想要店家数据源的数据在下边文本框内显示,双加数据控件,添加如下代码: 

​​ 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();
        }

为查询按钮,添加点击事件,代码如下:

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();
            }
        }

双击删除按钮,添加点击事件:

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();
            }
        }

双击修改按钮,添加点击事件:

 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();
            }
        }

点击取消按钮,添加点击事件:

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

设置页面初始时选中院系ID,需要设置页面加载frmCollege_Load代码:

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.创建添加学生页面,frmAddStudent.cs:

 添加Lable命名学号,输入框TextBox控件  姓名Label 输入框TextBox,添加一个Panel容器里面放两个单选按钮控件RadioButton命名男  、女 

添加Label控件命名出生日期,输入框TextBox控件

添加Label控件命名:班级,添加一个下拉框控件ComboBox控件,点击ComboBox控件上方的小三角,选择使用数据绑定项,绑定数据库中的表

... 

 添加两个按钮Button命名,添加、退出

双击添加按钮,添加点击事件:

 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();
            }
        }

双击退出按钮,添加点击事件:

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

后续代码见:学生信息管理系统-部分代码2

 

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>