学生信息管理系统 C# + SQL

开发工具:Microsoft SQL Serever 2017 / Microsoft Visual studio 2019
开发语言:SQL / C#

一、需求分析

根据对学生信息管理的需要,设计并实现“学生信息管理系统”,该程序主要有由注册、学生登录和管理员登录组成,在登录之前应先注册,学生登录可以完成查询“个人信息”、“成绩信息”、“选课信息”、“修改密码”的操作,管理员登录可以完成对“学生”、“课程”、“学生-课程”、“登录日志”、“个人信息”、“修改密码”的操作。
注册登录:注册时需要补充相应的信息如:用户名、密码、学号、工号、联系方式、出生日期、身份、照片。注册完可直接登录,登录时需要输入用户名、密码、验证码(可以更换验证码)。

学生登录:主要对用户的个人信息、成绩信息和选课信息进行查询。
管理员登录:主要对学生的个人信息、课程基本信息、学生成绩信息进行增、删、改、查。

二、总体设计和详细设计

2.1 总体功能设计
学生信息管理系统 C# + SQL_第1张图片

图1 总体功能设计

根据图1的总体功能设计,各模块功能需求如下:
1、注册:注册学生身份或管理员身份,需要填写一些信息:用户名、密码(加密存储)、学号、工号、联系方式、出生日期、身份、照片。注册完可直接登录。
2、学生登录:可以查询个人信息、成绩信息、选课信息,还可以根据自己的用户名修改登录时的密码。
个人信息包括学号、姓名、性别、联系方式、出生日期、年龄、专业和照片。
成绩信息可以查看个人的所有课程的成绩以及平均成绩和不及格成绩的信息。
3、管理员登录:完成对学生表、课程表和成绩的增删改查操作,查看登录日志、个人信息,根据用户名修改登陆密码。
其中,学生表的增加要求姓名唯一,对性别和年龄加以限制,性别必须输入为“男”或“女”,年龄必须为大于零的整数,还要求学生的学号不可重复;删除时要求选择一整行进行删除,如果该学生已经选课,则提示该学而生无法删除;修改时可以根据学号修改姓名、性别、年龄、系别;查找时可以根据学号和姓名单独查询,也可以根据性别、系别、年龄的任意组合进行查询。
课程表的增加要求课程号唯一、先行课的选择必须是已经存在的课程号;删除时选择一整行删除,如果已经有学生选课,则提示无法删除;修改时可以根据课程号修课程名、先行课和学分,也可以根据课程名修改先行课和学分;查找时可以根据课程号查找,也可以根据课程名、先行课和学分的任意组合查找。
学生-课程表完成对学生成绩的操作,增加时学号和课程号必须是已经存在的;删除时选中一整行删除;修改时根据学号和课程号修改成绩;查找时可以根据学号、课程号、成绩的任意组合纪念性查找,也可以查找某门课程在某个分数段的学生成绩信息。
登录日志可以查看学生和管理员的登录和注册的时间以及操作记录。
个人信息包括:工号、姓名、性别、联系方式、出生日期、工资、职称和照片的信息。

2.2 概念模型:E-R图
通过需求分析,可以得到学生、课程、管理员、注册、日志实体,对实体之间的分析可得到如下的E-R图。
1、系统总体E-R图
学生信息管理系统 C# + SQL_第2张图片

图2-1 系统总体E-R图

2、实体图
学生实体属性包括学号、姓名、性别、年龄、系别。学生实体图如图2-2所示。
学生信息管理系统 C# + SQL_第3张图片

图2-2 学生实体图

管理员实体属性包括工号、姓名、性别、工资、职称。管理员实体图如图2-3所示。
学生信息管理系统 C# + SQL_第4张图片

图2-3 管理员实体图

课程实体属性包括课程号、课程名、先行课、学分。课程实体图如图2-4所示。
学生信息管理系统 C# + SQL_第5张图片

图2-4 课程实体图

注册实体属性包括用户名、密码、学号/工号、联系方式、出生日期、身份、照片。注册实体图如图2-5所示。
学生信息管理系统 C# + SQL_第6张图片

图2-5 注册实体图

日志实体属性包括用户名、时间、操作。日志实体图如图2-6所示。

学生信息管理系统 C# + SQL_第7张图片

图2-6 日志实体图

2.3 逻辑模型:基本表
以下为各数据表的属性字段说明:
1、学生表(Student),记录学生的基本信息。通过分析表中各属性字段得到的具体数据类型如表3-1.

表3-1 学生表
列名 说明 数据类型 约束
Sno 学号 CHAR(9) 主码
Sname 姓名 CHAR(20) 唯一UNIQUE
Ssex 性别 CHAR(2) NOT NULL,取“男”或“女”
Sage 年龄 SMALLINT NOT NULL,大于0
Sdept 系别 CHAR(20)

2、课程表(Course),记录课程信息。通过分析表中各属性字段得到的具体数据类型如表3-2.

表3-2 课程表
列名 说明 数据类型 约束
Cno 课程号 CHAR(4) 主码
Cname 课程名 CHAR(40) NOT NULL
Cpno 先行课 CHAR(4) Course表的外码
Ccredit 学分 SMALLINT NOT NULL

3、学生-课程表(SC),记录学生成绩信息。通过分析表中各属性字段得到的具体数据类型如表3-3.

表3-3 学生-课程表
列名 说明 数据类型 约束
Sno 学号 CHAR(9) Student表的外码
Cno 课程号 CHAR(4) Course表的外码
Grade 成绩 SMALLINT
Sno和Cno共同构成主码
4、管理员表(Admin),记录管理员的个人信息。通过分析表中各属性字段得到的具体数据类型如表3-4.
表3-4 管理员表
列名 说明 数据类型 约束
Ano 工号 CHAR(9) 主码
Aname 姓名 CHAR(20) NOT NULL,唯一 UNIQUE
Asex 性别 CHAR(2) NOT NULL,取“男”或“女”
Asalary 工资 CHAR(10) NOT NULL
Atitle 职称 CHAR(10)
5、用户注册表(SysUser),记录用户登录时的信息。通过分析表中各属性字段得到的具体数据类型如表3-5.
表3-5 用户注册表
列名 说明 数据类型 约束
UserID 用户名 NCHAR(20) 主码
UserPassWord 密码 NCHAR(32) NOT NULL
UserSchoolID 学号/工号 NCHAR(20) NOT NULL
UserMobile 联系方式 NCHAR(11) NOT NULL
UserBirthday 出生日期 datatime
UserIdentity 身份 NCHAR(20) NOT NULL
UserPhoto 照片 image
6、用户登录记录表(SysLog),记录用户的登录和注册日志。通过分析表中各属性字段得到的具体数据类型如表3-6.
表3-6 用户登录记录表
列名 说明 数据类型 约束
UserID 用户名 NCHAR(20)
DataAndTime 时间 datatime
UserOperation 操作 NCHAR(200)

三、系统实现

数据库建表准备:


CREATE TABLE SysUser          
 (	
 UserID NCHAR(20) PRIMARY KEY,                          
 UserPassWord NCHAR(32) NOT NULL,             
 UserSchoolID NCHAR(20) NOT NULL,
 UserMobile NCHAR(11) NOT NULL,
 UserBirthday datetime,
 UserIdentity NCHAR(20) NOT NULL,
 UserPhoto image
 ); 


 CREATE TABLE SysLog          
 (	
 UserID NCHAR(20) ,                          
 DateAndTime datetime,
 UserOperation NCHAR(200)
 ); 

CREATE TABLE Student          
 (	
 Sno CHAR(9) PRIMARY KEY,        /* 列级完整性约束条件,Sno是主码*/                  
 Sname CHAR(20) UNIQUE,        
 Ssex CHAR(2) NOT NULL,check (Ssex in ('男','女')),
 Sage SMALLINT NOT NULL,check (Sage>0),
 Sdept CHAR(20)
 ); 

CREATE TABLE  Course
 (	
 Cno CHAR(4) PRIMARY KEY,
 Cname CHAR(40) NOT NULL,            
 Cpno CHAR(4),               	                      
 Ccredit SMALLINT NOT NULL,
 FOREIGN KEY (Cpno) REFERENCES  Course(Cno) 
 ); 

CREATE TABLE  SC
 (
 Sno CHAR(9), 
 Cno CHAR(4),  
 Grade SMALLINT,
 PRIMARY KEY (Sno,Cno),                     /* 主码由两个属性构成,必须作为表级完整性进行定义*/
 FOREIGN KEY (Sno) REFERENCES Student(Sno),  /* 表级完整性约束条件,Sno是外码,被参照表是Student */
 FOREIGN KEY (Cno)REFERENCES Course(Cno)     /* 表级完整性约束条件, Cno是外码,被参照表是Course*/
 ); 

INSERT  INTO  SysUser VALUES ('admin','123','000','13812345678',1999-1-1,'0',NULL);

INSERT  INTO  Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215121','李勇','男','CS',20);
INSERT  INTO  Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215122','刘晨','女','CS',19);
INSERT  INTO  Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215123','王敏','女','MA',18);
INSERT  INTO  Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215125','张立','男','IS',19);
INSERT  INTO  Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215128','陈冬','男','IS',20);
INSERT  INTO  Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215129','李丽','女','IS',20);
INSERT  INTO  Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215126','陈霞','男','MA',19);

SELECT * FROM Student

INSERT  INTO Course(Cno,Cname,Cpno,Ccredit)	VALUES ('1','数据库',NULL,4);
INSERT  INTO Course(Cno,Cname,Cpno,Ccredit)	VALUES ('2','数学',NULL,4);
INSERT  INTO Course(Cno,Cname,Cpno,Ccredit)	VALUES ('3','信息系统',NULL,4);
INSERT  INTO Course(Cno,Cname,Cpno,Ccredit)	VALUES ('4','操作系统',NULL,4);
INSERT  INTO Course(Cno,Cname,Cpno,Ccredit)	VALUES ('5','数据结构',NULL,4);
INSERT  INTO Course(Cno,Cname,Cpno,Ccredit)	VALUES ('6','数据处理',NULL,4);
INSERT  INTO Course(Cno,Cname,Cpno,Ccredit)	VALUES ('7','Pascal语言',NULL,4);

UPDATE Course SET Cpno = '5' WHERE Cno = '1' 
UPDATE Course SET Cpno = '1' WHERE Cno = '3' 
UPDATE Course SET Cpno = '6' WHERE Cno = '4' 
UPDATE Course SET Cpno = '7' WHERE Cno = '5' 
UPDATE Course SET Cpno = '6' WHERE Cno = '7' 

SELECT * FROM Course

INSERT  INTO SC(Sno,Cno,Grade) VALUES ('201215121 ','1',92);
INSERT  INTO SC(Sno,Cno,Grade) VALUES ('201215121 ','2',85);
INSERT  INTO SC(Sno,Cno,Grade) VALUES ('201215121 ','3',88);
INSERT  INTO SC(Sno,Cno,Grade) VALUES ('201215122 ','2',90);
INSERT  INTO SC(Sno,Cno,Grade) VALUES ('201215122 ','3',80);
INSERT  INTO SC(Sno,Cno,Grade) VALUES ('201215121 ','4',56);
INSERT  INTO SC(Sno,Cno,Grade) VALUES ('201215125 ','1',85);
SELECT * FROM SC

CREATE TABLE Admin(Ano char (9) PRIMARY KEY,
		  Aname char(20) NOT NULL,
		  Asex char (2) NOT NULL,check (Asex in ('男','女')),
		  Asalary char(10) NOT NULL,
		  Atitle char(10));

INSERT  INTO Admin(Ano,Aname,Asex,Asalary,Atitle) VALUES ('1','王琦','女',5000,'副教授');
INSERT  INTO Admin(Ano,Aname,Asex,Asalary,Atitle) VALUES ('2','陈宇','男',5500,'教授');
INSERT  INTO Admin(Ano,Aname,Asex,Asalary,Atitle) VALUES ('3','刘宁','女',4500,'讲师');
INSERT  INTO Admin(Ano,Aname,Asex,Asalary,Atitle) VALUES ('4','王晨','女',6000,'教授');
INSERT  INTO Admin(Ano,Aname,Asex,Asalary,Atitle) VALUES ('5','韩新','女',4000,'讲师');
INSERT  INTO Admin(Ano,Aname,Asex,Asalary,Atitle) VALUES ('6','王韩','女',5400,'教学秘书');

select *
from Admin

建立触发器:

IF(OBJECT_ID('regist_recorder') is not null)        -- 判断名为 regist_recorder 的触发器是否存在
DROP TRIGGER regist_recorder        -- 删除触发器
GO

CREATE TRIGGER regist_recorder
ON SysUser  	         
AFTER
INSERT
AS 
	declare @UserName    nchar(20)
	declare @DateTime    datetime
	declare @UserOperation nchar(200)

	select @UserName = system_user
	select @DateTime = CONVERT(datetime,GETDATE(),120) 

	declare @op varchar(10)
	select @op=case when exists(select 1 from inserted) and exists(select 1 from deleted)
                   then 'Update'
                   when exists(select 1 from inserted) and not exists(select 1 from deleted)
                   then 'Insert'
                   when not exists(select 1 from inserted) and exists(select 1 from deleted)
                   then 'Delete' end
                   
	
	select @UserOperation = @op
	

	INSERT INTO SysLog(UserID,DateAndTime,UserOperation)
	VALUES (@UserName,@DateTime,@UserOperation)

  • 注册界面:输入信息选择身份注册。
    用户名和密码必须为“至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符!”,否则会提示。
    学生信息管理系统 C# + SQL_第8张图片
    MD5加密
 public Byte[] mybyte = new byte[0];

        public static string EncryptWithMD5(string source)  //MD5加密
        {
            byte[] sor = Encoding.UTF8.GetBytes(source);
            MD5 md5 = MD5.Create();
            byte[] result = md5.ComputeHash(sor);
            StringBuilder strbul = new StringBuilder(40);
            for (int i = 0; i < result.Length; i++)
            {
                strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
            }
            return strbul.ToString();
        }

上传照片

 private void buttonphoto_Click(object sender, EventArgs e)
        {
            //打开浏览图片对话框
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.ShowDialog();
            string picturePath = openFileDialog.FileName;//获取图片路径
            //文件的名称,每次必须更换图片的名称,这里很为不便
            //创建FileStream对象
            FileStream fs = new FileStream(picturePath, FileMode.Open, FileAccess.Read);
            //声明Byte数组
            mybyte = new byte[fs.Length];
            //读取数据
            fs.Read(mybyte, 0, mybyte.Length);
            pictureBox1.Image = Image.FromStream(fs);
            fs.Close();
        }

确定按钮

private void buttonok_Click(object sender, EventArgs e)
        {
            try
            {
                string connString = "Data Source =.; Initial Catalog = School;  Persist Security Info = True;User ID = sa; Password = 123";//数据库连接字符串
                SqlConnection connection = new SqlConnection(connString);//创建connection对象
                string sql = "insert into SysUser (UserID,   UserPassWord ,   UserSchoolID, UserMobile, UserBirthday , UserIdentity , UserPhoto ) " +
                                                        "values (@userid, @userpassword,@userschoolid,@usermobile,@userbirthday,@useridentity,@userphoto)";
                SqlCommand command = new SqlCommand(sql, connection);

                SqlParameter sqlParameter = new SqlParameter("@userid", textBoxusername.Text);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@userpassword", EncryptWithMD5(textBoxpassword.Text));
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@userschoolid", textBoxid.Text);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@usermobile", textBoxmobile.Text);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@userbirthday", dateTimePickerbirth.Value);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@useridentity", comboBoxidentity.Text);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@userphoto", SqlDbType.VarBinary, mybyte.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, mybyte);
                command.Parameters.Add(sqlParameter);

                //打开数据库连接
                connection.Open();
                command.ExecuteNonQuery();
                connection.Close();
                MessageBox.Show("注册成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            this.Close();
        }

用户名和密码的格式限制。点击用户名和密码的文本框,点击属性/事件里面的Leave。

  private void textBoxusername_Leave(object sender, EventArgs e)
        {
            if (textBoxusername.Text.Trim() != "")
            {
                //使用regex(正则表达式)进行格式设置 至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符。
                Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");

                if (regex.IsMatch(textBoxusername.Text))//判断格式是否符合要求
                {
                    //MessageBox.Show("输入用户名格式正确!");
                }
                else
                {
                    MessageBox.Show("至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符!");
                    textBoxusername.Focus();
                }
            }
            else
            {
                MessageBox.Show("用户名不能为空!");
            }
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void textBoxpassword_Leave_1(object sender, EventArgs e)
        {
            if (textBoxpassword.Text.Trim() != "")
            {
                //使用regex(正则表达式)进行格式设置 至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符。
                Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");

                if (regex.IsMatch(textBoxpassword.Text))//判断格式是否符合要求
                {
                    //MessageBox.Show("输入密码格式正确!");
                }
                else
                {
                    MessageBox.Show("至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符!");
                    textBoxpassword.Focus();
                }
            }
            else
            {
                MessageBox.Show("密码不能为空!");
            }
        }
  • 登录界面:输入用户名、密码、验证码(可以更改)和选择正确的身份即可登录。

学生登录
可以通过点击窗口的,将StartPosition改为CenterScreen,,将窗体显示在屏幕中间。学生信息管理系统 C# + SQL_第9张图片
获取验证码:

 public void ShowCode()   //验证码取值
        {
            labelcode.Text = "";
            //随机实例化 
            Random ran = new Random();
            int number;
            char code1;
            //取五个数 
            for (int i = 0; i < 5; i++)
            {
                number = ran.Next();
                if (number % 2 == 0)
                    code1 = (char)('1' + (char)(number % 9));
                //由于O与0经常混淆,所以数字从1-9,大写英文字母A-Z
                else
                    code1 = (char)('A' + (char)(number % 26)); //转化为字符 
                this.code += code1.ToString();
            }
            labelcode.Text = code;
        }

        public string code;

        private void Form1_Load(object sender, EventArgs e)
        {
            ShowCode();
        }
          private void linkLabelchange_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)//更改验证码
        {
            this.code = "";
            ShowCode();
        }

登录按钮

 static String name;
 string identity;
        public string Get()
        {
            return name;
        }
        private void buttonlogin_Click(object sender, EventArgs e)
        {
            string username = textBoxusername.Text.Trim();  //取出账号
            string password = EncryptWithMD5(textBoxpassword.Text.Trim());  //取出密码并加密        
            name = username;          
            if (radioButtonadmin.Checked)
            {
                identity = radioButtonadmin.Text;
            }
            else if (radioButtonstudent.Checked)
            {
                identity = radioButtonstudent.Text;
            }
            string myConnString = "Data Source =.; Initial Catalog = School;  Persist Security Info = True;User ID = sa; Password = 123";
            SqlConnection sqlConnection = new SqlConnection(myConnString);  //实例化连接对象
            sqlConnection.Open();         
            string sql = "select UserID,UserPassword,UserIdentity  from SysUser where UserID = '" + username + "' and UserPassword = '" + password + "' and UserIdentity='"+identity+"'";                                            //编写SQL命令
            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);

            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

            if (sqlDataReader.HasRows && textBoxcode.Text == code && radioButtonstudent.Checked)//学生成功登录
            {            
                    MessageBox.Show("欢迎使用!");             //登录成功
                    this.Hide();
                    StudentMain studentmain = new StudentMain();
                    studentmain.ShowDialog();                                                                  
            }
            if (sqlDataReader.HasRows && textBoxcode.Text == code && radioButtonadmin.Checked)//管理员成功登录
            {
                MessageBox.Show("欢迎使用!");  
                this.Hide();           //登录成功
                AdminMain adminmain = new AdminMain();
                adminmain.ShowDialog();               
            }                  
            if (textBoxcode.Text != code)       //验证码输入错误,登录失败
            {
                MessageBox.Show("验证码错误!");
                return;
            }
            if (!sqlDataReader.HasRows)         //用户名或密码错误,登录失败
            {
                MessageBox.Show("密码错误或该用户不存在!");
                return;
            }
            if (!radioButtonstudent.Checked && !radioButtonadmin.Checked)        //未选择身份
            {
                MessageBox.Show("请选择登录身份!");
                return;
            }
            sqlDataReader.Close();
            sql = "insert into SysLog values ( '" + username + "' , '" + DateTime.Now + "' , '" + "Login" + "')";                                            //编写SQL命令
            sqlCommand = new SqlCommand(sql, sqlConnection);
            sqlCommand.ExecuteNonQuery();
            sqlConnection.Close();
        }

MD5加密

 public static string EncryptWithMD5(string source)      //MD5加密
        {
            byte[] sor = Encoding.UTF8.GetBytes(source);
            MD5 md5 = MD5.Create();
            byte[] result = md5.ComputeHash(sor);
            StringBuilder strbul = new StringBuilder(40);
            for (int i = 0; i < result.Length; i++)
            {
                strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
            }
            return strbul.ToString();
        }

进入下一个窗体,退出按钮

 private void linkLabelregister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Hide();
            Register register = new Register();
            register.ShowDialog();           
        }
     
        private void buttonclose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
  • 选择要进行的操作:
    学生信息管理系统 C# + SQL_第10张图片
    点击显示下一个窗体
 private void buttonclose_Click(object sender, EventArgs e)//关闭
        {
            this.Close();
            Form1 m = new Form1();
            m.Show();
        }

        private void buttonpassword_Click(object sender, EventArgs e)//修改密码
        {
            this.Hide();
            Student_password spw = new Student_password();
            spw.ShowDialog(); 
        }

        private void buttoninfo_Click(object sender, EventArgs e)//个人信息
        { 
            this.Hide();
            Student_info studentinfo = new Student_info();
            studentinfo.ShowDialog();          
        }

        private void buttongrade_Click(object sender, EventArgs e)//成绩
        {  
            this.Hide();
            Student_grade studentgrade = new Student_grade();
            studentgrade.ShowDialog();         
        }

        private void buttoncourse_Click(object sender, EventArgs e)//课程
        {
            this.Hide();
            Student_Course student_course = new Student_Course();
            student_course.ShowDialog();           
        }

        private void button1_Click(object sender, EventArgs e)//退出
        {
            Application.Exit();            
        }

个人信息显示:
学生信息管理系统 C# + SQL_第11张图片
返回上一个窗体,退出

 private void buttonselect_Click(object sender, EventArgs e)
        {
            this.Close();
            StudentMain s = new StudentMain();
            s.Show();
        }
private void button1_Click(object sender, EventArgs e)
      {
            Application.Exit();
      }

直接显示个人信息

  private void Student_info_Load(object sender, EventArgs e)
        {
            try
            {
                Form1 m = new Form1();
                string select_name = m.Get();
                string connString = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=123";//数据库连接字符串
                SqlConnection connection = new SqlConnection(connString);//创建connection对象              
                connection.Open(); //打开数据库连接

                //显示信息
                //在显示出生日期,注意DataTime格式的转换,以及完成查询后的标签内容的转换                
                string select_a = "select Sno,Sname,Ssex,UserMobile,UserBirthday,Sage,Sdept from Student,SysUser where Student.Sno=SysUser.UserSchoolID and UserID='" + select_name + "'";
                SqlCommand cmd = new SqlCommand(select_a, connection); 
                SqlDataReader dr = cmd.ExecuteReader();//读取数据
                dr.Read();
                if (dr.HasRows)
                {
                    labelsno.Text=dr[0].ToString();
                    labelsname.Text = dr[1].ToString();
                    labelssex.Text = dr[2].ToString();
                    labelsphone.Text = dr[3].ToString();
                  //  labelsbirth.Text = dr[4].ToString();
                    labelsbirth.Text = Convert.ToString(dr.GetDateTime(4).ToShortDateString());
                    labelsage.Text = dr[5].ToString();
                    labelsdept.Text = dr[6].ToString();                   
                }
                else { MessageBox.Show("您的信息还未录入!"); }
                dr.Close();

                //显示图片,如果图片显示不全,将pictureBox的SizeMode改为Zoom,图片的长宽比例不变
                string sql = "select UserPhoto from SysUser where UserID = '" + select_name + "'";
                SqlCommand command = new SqlCommand(sql, connection);//创建SqlCommand对象
                //创建DataAdapter对象
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                //创建DataSet对象
                DataSet dataSet = new DataSet();
                dataAdapter.Fill(dataSet, "SysUser");
                int c = dataSet.Tables["SysUser"].Rows.Count;
                if (c > 0)
                {
                    Byte[] mybyte = new byte[0];
                    mybyte = (Byte[])(dataSet.Tables["SysUser"].Rows[c - 1]["UserPhoto"]);
                    MemoryStream ms = new MemoryStream(mybyte);
                    pictureBox1.Image = Image.FromStream(ms);
                }
                else
                    pictureBox1.Image = null;
                connection.Close();
            }
            catch
            {
                MessageBox.Show("显示信息失败!");
            }
        }

成绩信息显示:
学生信息管理系统 C# + SQL_第12张图片

窗口直接显示成绩

 String conn = "Data Source =.; Initial Catalog = School;  Persist Security Info = True;User ID = sa; Password = 123";
        static Form1 m = new Form1();
        static string select_name = m.Get();
        private void Student_grade_Load(object sender, EventArgs e)
        {
            SqlConnection sqlconnection = new SqlConnection(conn);//实例化连接对象
            try
            {
                sqlconnection.Open();
                string select_by_sno = "select SC.Sno,Sname,SC.Cno,Cname,Grade from Student,SC,Course,SysUser where Student.Sno=SC.Sno and Course.Cno=SC.Cno and SC.Sno=SysUser.UserSchoolID and UserID='" + select_name + "'";
                SqlCommand sqlcommand = new SqlCommand(select_by_sno, sqlconnection);
                SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                BindingSource bindingsource = new BindingSource();
                bindingsource.DataSource = sqldatareader;
                dataGridView1.DataSource = bindingsource;
            }
            catch
            {
                MessageBox.Show("查询语句有误,请认真检查SQL语句");
            }
            finally
            {
                sqlconnection.Close();
            }
            // TODO: 这行代码将数据加载到表“schoolDataSet5.SC”中。您可以根据需要移动或删除它。
            this.sCTableAdapter.Fill(this.schoolDataSet5.SC);
        }
     

平均成绩
学生信息管理系统 C# + SQL_第13张图片
查询平均成绩

 private void buttonavegrade_Click(object sender, EventArgs e)//查询平均成绩
        {       
            SqlConnection sqlconnection = new SqlConnection(conn);//实例化连接对象
            try
            {
                sqlconnection.Open();
                string select_avggrade = "select AVG(Grade) from SC,SysUser where  SC.Sno=SysUser.UserSchoolID and UserID='" + select_name + "'";               
                SqlCommand sqlcommand = new SqlCommand(select_avggrade, sqlconnection);
                SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                BindingSource bindingsource = new BindingSource();
                bindingsource.DataSource = sqldatareader;
                dataGridView1.DataSource = bindingsource;
            }
            catch
            {
                MessageBox.Show("查询语句有误,请认真检查SQL语句");
            }
            finally
            {
                sqlconnection.Close();
            }
        }

不及格成绩
学生信息管理系统 C# + SQL_第14张图片
查询不及格成绩

 private void buttonnograde_Click(object sender, EventArgs e)//查询不及格成绩信息
        {
            SqlConnection sqlconnection = new SqlConnection(conn);//实例化连接对象
            try
            {
                sqlconnection.Open();
                string select_nograde =" select Sno,SC.Cno,Cname,Grade from SC,Course,SysUser where SC.Cno=Course.Cno and SC.Sno = SysUser.UserSchoolID and Grade<60 and UserID = '" + select_name + "'";
                SqlCommand sqlcommand = new SqlCommand(select_nograde, sqlconnection);
                SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                BindingSource bindingsource = new BindingSource();
                bindingsource.DataSource = sqldatareader;
                dataGridView1.DataSource = bindingsource;
                if (dataGridView1.DataSource is null)
                {
                    string nullgrade = "wiekof";
                    SqlCommand sqlcommand1 = new SqlCommand(nullgrade, sqlconnection);
                    SqlDataReader sqldatareader1 = sqlcommand.ExecuteReader();
                    BindingSource bindingsource1= new BindingSource();
                    bindingsource.DataSource = sqldatareader1;
                    dataGridView1.DataSource = bindingsource1;
                }
            }
            catch
            {             
                MessageBox.Show("查询语句有误,请认真检查SQL语句");
            }
            finally
            {
                sqlconnection.Close();
            }
            this.sCTableAdapter.Fill(this.schoolDataSet5.SC);
        }

选课信息显示
学生信息管理系统 C# + SQL_第15张图片
窗口直接显示选课信息

string studentsno;
        private void Student_Course_Load(object sender, EventArgs e)
        {
            Form1 m = new Form1();
            string select_name = m.Get();
            
            String conn = "Data Source =.; Initial Catalog = School;  Persist Security Info = True;User ID = sa; Password = 123";
            SqlConnection sqlconnection = new SqlConnection(conn);//实例化连接对象
            try
            {
                sqlconnection.Open();         
                String select_by_sno = "select UserSchoolID from SysUser where UserID='" + select_name + "'";
                SqlCommand cmd = new SqlCommand(select_by_sno, sqlconnection);
                SqlDataReader dr = cmd.ExecuteReader();//读取数据
                dr.Read();
                if (dr.HasRows)
                    studentsno = dr[0].ToString();
                dr.Close();
                String select_sno = "select SC.Sno,Course.Cno,Cname,Ccredit from Course,SC where Course.Cno = SC.Cno and SC.Sno = '" + studentsno + "'";
                SqlCommand sqlcommand = new SqlCommand(select_sno, sqlconnection);
                SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                BindingSource bindingsource = new BindingSource();
                bindingsource.DataSource = sqldatareader;
                dataGridView1.DataSource = bindingsource;
            }
            catch
            {
                MessageBox.Show("查询语句有误,请认真检查SQL语句");
            }
            finally
            {
                sqlconnection.Close();
            }

            // TODO: 这行代码将数据加载到表“schoolDataSet6.Course”中。您可以根据需要移动或删除它。
            this.courseTableAdapter.Fill(this.schoolDataSet6.Course);
        }

修改密码
学生信息管理系统 C# + SQL_第16张图片
MD5加密

 public static string EncryptWithMD5(string source)      //MD5加密
        {
            byte[] sor = Encoding.UTF8.GetBytes(source);
            MD5 md5 = MD5.Create();
            byte[] result = md5.ComputeHash(sor);
            StringBuilder strbul = new StringBuilder(40);
            for (int i = 0; i < result.Length; i++)
            {
                strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
            }
            return strbul.ToString();
        }

输入用户名、新密码、确认新密码,输入错误会提示

  private void buttonok_Click(object sender, EventArgs e)
        {
            try
            {
                Form1 m = new Form1();
                string select_name = m.Get();
                string newpassword = textBoxnewpassword.Text.Trim();
                string newpassword1 = textBoxnewpassword1.Text.Trim();
                string connString = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=123";//数据库连接字符串
                SqlConnection connection = new SqlConnection(connString);//创建connection对象              
                connection.Open(); //打开数据库连接

                if (textBoxusername.Text.Trim() == select_name)
                {
                    if (newpassword == newpassword1)//密码与确认密码的内容相同
                    {
                        string sql = "update SysUser set UserPassWord = @upw where UserID = @id";
                        SqlCommand cmd = new SqlCommand(sql, connection);

                        SqlParameter sqlParameter = new SqlParameter("@upw", EncryptWithMD5(textBoxnewpassword.Text.Trim()));
                        cmd.Parameters.Add(sqlParameter);
                        SqlParameter sqlParameter1 = new SqlParameter("@id", textBoxusername.Text.Trim());
                        cmd.Parameters.Add(sqlParameter1);

                        cmd.ExecuteNonQuery();
                        connection.Close();
                        MessageBox.Show("密码修改成功!");
                    }
                    else
                    {
                        MessageBox.Show("请再次检查输入的密码是否正确!");
                    }
                }
                else
                {
                    MessageBox.Show("请输入正确的用户名!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }         
        }

密码和输入新密码的格式限制

private void textBoxnewpassword_Leave(object sender, EventArgs e)//密码
        {
            if (textBoxnewpassword.Text.Trim() != "")
            {
                //使用regex(正则表达式)进行格式设置 至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符。
                Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");
                if (regex.IsMatch(textBoxnewpassword.Text))//判断格式是否符合要求
                {
                    //MessageBox.Show("输入密码格式正确!");
                }
                else
                {
                    MessageBox.Show("至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符!");
                    textBoxnewpassword.Focus();
                }
            }
            else
            {
                MessageBox.Show("密码不能为空!");
            }
        }

        private void textBoxnewpassword1_Leave(object sender, EventArgs e)//确认新密码
        {
            if (textBoxnewpassword1.Text.Trim() != "")
            {
                //使用regex(正则表达式)进行格式设置 至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符。
                Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");

                if (regex.IsMatch(textBoxnewpassword1.Text))//判断格式是否符合要求
                {
                    //MessageBox.Show("输入密码格式正确!");
                }
                else
                {
                    MessageBox.Show("至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符!");
                    textBoxnewpassword1.Focus();
                }
            }
            else
            {
                MessageBox.Show("确定密码不能为空!");
            }
        }
  • 管理员登录
    选择要进行的操作
    学生信息管理系统 C# + SQL_第17张图片
 private void buttonstudent_Click(object sender, EventArgs e)
        {
            this.Hide();
            Admin_Student ads = new Admin_Student();
            ads.ShowDialog();
            
        }

        private void buttonclose_Click(object sender, EventArgs e)
        {
            this.Close();
            Form1 f = new Form1();
            f.Show();
        }

        private void buttoncourse_Click(object sender, EventArgs e)
        {
            this.Hide();
            Admin_Course adc = new Admin_Course();
            adc.ShowDialog();
            
        }

        private void buttoninfo_Click(object sender, EventArgs e)//个人信息
        { 
            this.Hide();
            Admin_info adin = new Admin_info();
            adin.ShowDialog();
           
        }

        private void buttonlogin_Click(object sender, EventArgs e)//登录日志
        { 
            this.Hide();
            Admin_login adlogin = new Admin_login();
            adlogin.ShowDialog();
           
        }

        private void buttonsc_Click(object sender, EventArgs e)
        {
            this.Hide();
            Admin_SC adsc = new Admin_SC();
            adsc.ShowDialog();
            
        }

        private void buttonpassword_Click(object sender, EventArgs e)
        {
            this.Hide();
            Admin_password adpw = new Admin_password();
            adpw.ShowDialog();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

学生表
学生信息管理系统 C# + SQL_第18张图片
增加:性别、年龄限制

        SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = School;  Persist Security Info = True;User ID = sa; Password = 123"); //连接数据库
         private void buttonadd_Click(object sender, EventArgs e)    //增加
        {
            string StuSno = textBoxsno.Text.Trim();
            string StuSname = textBoxsname.Text.Trim();
            string StuSsex = textBoxssex.Text.Trim();
            string StuSage = textBoxsage.Text.Trim();
            string StuSdept = textBoxsdept.Text.Trim();
           
             try
             {
                if (StuSsex != "男" && StuSsex != "女")
                {
                    MessageBox.Show("性别必须输入为“男”或“女”");
                }
                
                con.Open();     //打开数据库
                 string insertStr = "INSERT INTO Student(Sno,Sname,Ssex,Sage,Sdept)" + "VALUES('" + StuSno + "','" + StuSname + "','" + StuSsex + "'," + StuSage + ",'" + StuSdept + "')";
                 SqlCommand cmd = new SqlCommand(insertStr, con);
                 cmd.ExecuteNonQuery();      //将增加后的信息直接出来
             }
             catch
             {
                MessageBox.Show("输入数据违反要求,请按照要求填写信息!");
            }
             finally
             {
                con.Close();      //关闭数据库
               //  con.Close();
             }
             this.studentTableAdapter.Fill(this.schoolDataSet.Student);          
        }

删除:选择整行删除,已经选课的学生不可删除

 private void buttondelete_Click(object sender, EventArgs e)     //删除
        {
            try
            {
                con.Open();     //打开数据库
                string select_Sno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是Sno
                string delete_by_Sno = "DELETE FROM Student WHERE Sno='" + select_Sno + "'";//sql删除语句
                SqlCommand cmd = new SqlCommand(delete_by_Sno, con);
                cmd.ExecuteNonQuery();
            }
            catch
            {
                MessageBox.Show("该学生已选课,无法删除!");
            }
            finally
            {
                // con.Dispose();      //关闭数据库
                con.Close();
            }
            this.studentTableAdapter.Fill(this.schoolDataSet.Student);
        }

修改:根据学号修改姓名、性别、年龄、专业

private void buttonchange_Click(object sender, EventArgs e)     //修改,根据学号修改姓名、性别、年龄、专业
        {
            string StuSno = textBoxsno.Text.Trim();
            string StuSname = textBoxsname.Text.Trim();
            string StuSsex = textBoxssex.Text.Trim();
            string StuSage = textBoxsage.Text.Trim();
            string StuSdept = textBoxsdept.Text.Trim();
            try
            {
                con.Open();     //打开数据库
                if (StuSno == "")//输入的学号为空
                {
                    MessageBox.Show("学号不能为空!");
                }
                if (StuSname != "") //在Sname的文本框输入内容,修改Sname
                {
                    string update_sname = "UPDATE Student SET Sname='" + StuSname + "'WHERE Sno='" + StuSno + "'";
                    SqlCommand cmd1 = new SqlCommand(update_sname, con);
                    cmd1.ExecuteNonQuery();
                }
                if (StuSsex != "")  //修改Ssex
                {
                    string update_sex = "UPDATE Student SET Ssex='" + StuSsex + "' WHERE Sno='" + StuSno + "'";
                    SqlCommand cmd2 = new SqlCommand(update_sex, con);
                    cmd2.ExecuteNonQuery();
                }
                if (StuSage != "")//修改Sage
                {                
                     string update_age = "UPDATE Student SET Sage='" + StuSage + " 'WHERE Sno='" + StuSno + "'";
                     SqlCommand cmd3 = new SqlCommand(update_age, con);
                    cmd3.ExecuteNonQuery();
                }
                if (StuSdept != "")//修改Sdept
                {
                    string update_sdept = "UPDATE Student SET Sdept='" + StuSdept + "' WHERE Sno='" + StuSno + "'";
                    SqlCommand cmd4 = new SqlCommand(update_sdept, con);
                    cmd4.ExecuteNonQuery();
                }               
            }
            catch
            {
                MessageBox.Show("输入数据违反要求!");
            }
            finally
            {
                //con.Dispose();      //关闭数据库
                con.Close();
            }
            this.studentTableAdapter.Fill(this.schoolDataSet.Student);
        }

查找

 private void buttonselect_Click(object sender, EventArgs e) //查找
        {
            string StuSno = textBoxsno.Text.Trim();
            string StuSname = textBoxsname.Text.Trim();
            string StuSsex = textBoxssex.Text.Trim();
            string StuSage = textBoxsage.Text.Trim();
            string StuSdept = textBoxsdept.Text.Trim();
            String conn = "Data Source =.; Initial Catalog = School;  Persist Security Info = True;User ID = sa; Password = 123";
            SqlConnection sqlconnection = new SqlConnection(conn);//实例化连接对象
            
            try
            {
                sqlconnection.Open();
                
                if (StuSno != "")//按照学号查找,只有一个
                {
                    String select_by_sno = "select * from Student where Sno='" + StuSno + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_sno, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                    //将读出来的值赋给数据源,再将数据源给dataGridView
                }
                if (StuSname != ""&&StuSsex==""&&StuSage==""&&StuSdept=="")//只按照姓名查找
                {
                    String select_by_sname = "select * from Student where Sname='" + StuSname + "'";
                   SqlCommand  sqlcommand = new SqlCommand(select_by_sname, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                    //将读出来的值赋给数据源,再将数据源给dataGridView
                }
                if (StuSsex!=""&&StuSname==""&& StuSdept==""&&StuSage=="") //只按照性别查找
                {
                    String select_by_sex = "select * from Student where Ssex='" + StuSsex + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_sex, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                    //将读出来的值赋给数据源,再将数据源给dataGridView
                }
                if (StuSage != "" && StuSname == "" && StuSsex == "" && StuSdept == "")//只按照年龄查找
                {
                    String select_by_age = "select * from Student where Sage='" + StuSage + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_age, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                    //将读出来的值赋给数据源,再将数据源给dataGridView
                }
                if (StuSdept != "" && StuSname == "" && StuSsex == "" && StuSage == "")//只按照系别查找
                {
                    String select_by_sdept = "select * from Student where Sdept='" + StuSdept + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_sdept, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                    //将读出来的值赋给数据源,再将数据源给dataGridView
                }
                 if(StuSsex!=""&&StuSage!=""&&StuSdept=="")//按照性别和年龄查找
                {
                    String select_by_sexage = "select * from Student where Ssex='" + StuSsex + "' and Sage='"+StuSage+"'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_sexage, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if (StuSsex != "" && StuSdept != "" && StuSage == "")//按照性别和系别查找
                {
                    String select_by_sexdept = "select * from Student where Ssex='" + StuSsex + "' and Sdept='" + StuSdept + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_sexdept, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if (StuSage != "" && StuSdept != "" && StuSsex == "")//按照年龄和系别查找
                {
                    String select_by_agedept = "select * from Student where Sage='" + StuSage + "' and Sdept='" + StuSdept + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_agedept, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if (StuSage != "" && StuSdept != "" && StuSsex != "")//按照年龄、系别、性别查找
                {
                    String select_by_agedeptsex = "select * from Student where Sage='" + StuSage + "' and Sdept='" + StuSdept + "' and Ssex='"+StuSsex+"'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_agedeptsex, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
            }
            catch
            {
                MessageBox.Show("查询语句有误,请认真检查SQL语句");
            }
            finally
            {
                sqlconnection.Close();              
            }
        }

清除文本框内容

  public void clear()//将文本框的内容全部清除
        {
            textBoxsno.Text = "";
            textBoxsname.Text = "";
            textBoxssex.Text = "";
            textBoxsage.Text = "";
            textBoxsdept.Text = "";
        }

课程表
学生信息管理系统 C# + SQL_第19张图片
增加:

SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = School;  Persist Security Info = True;User ID = sa; Password = 123"); //连接数据库

       
        private void buttonadd_Click(object sender, EventArgs e)//增加
        {
            string CouCno = textBoxcno.Text.Trim();
            string CouCname = textBoxcname.Text.Trim();
            string CouCpno =textBoxcpno.Text.Trim();
            string CouCcredit = textBoxcredit.Text.Trim();
            try
            {
                con.Open();     //打开数据库
                string insertCou = "INSERT INTO Course(Cno,Cname,Cpno,Ccredit)" + "VALUES('" + CouCno + "','" + CouCname + "','" + CouCpno + "'," + CouCcredit+ ")";
                SqlCommand cmd = new SqlCommand(insertCou, con);
                cmd.ExecuteNonQuery();      //将增加后的信息直接出来
            }
            catch
            {
                MessageBox.Show("输入数据违反要求!");
            }
            finally 
            {
                con.Close();
            }
            this.courseTableAdapter.Fill(this.schoolDataSet1.Course);
        }

删除:已经被学生选课的课程无法删除

 private void buttondelete_Click(object sender, EventArgs e)//删除
        {
            try
            {
                con.Open();     //打开数据库
                string select_Cno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是Cno
                string delete_by_Cno = "DELETE FROM Course WHERE Cno='" + select_Cno + "'";//sql删除语句
                SqlCommand cmd = new SqlCommand(delete_by_Cno, con);
                cmd.ExecuteNonQuery();
            }
            catch
            {
                MessageBox.Show("已有学生选课或作为先行课,无法删除!");
            }
            finally
            {
                // con.Dispose();      //关闭数据库
                con.Close();
            }
            this.courseTableAdapter.Fill(this.schoolDataSet1.Course);
        }

修改:根据课程号修改或者根据课程名修改

 private void buttonchange_Click(object sender, EventArgs e)//修改
        {
            string CouCno = textBoxcno.Text.Trim();
            string CouCname = textBoxcname.Text.Trim();
            string CouCpno = textBoxcpno.Text.Trim();
            string CouCcredit = textBoxcredit.Text.Trim();
            try
            {
                con.Open();     //打开数据库
                
                if (CouCname != "") //修改课程名称,只能根据课程号修改
                {
                    
                    string update_cname = "UPDATE Course SET Cname='" + CouCname + "'WHERE Cno='" + CouCno + "'";
                    SqlCommand cmd = new SqlCommand(update_cname, con);
                    cmd.ExecuteNonQuery();
                }
                 if ( CouCpno != "" &&CouCno != "" || CouCname != ""&& CouCpno != "")  //修改Cpno,通过课程号修改一门课的或者通过课程名修改多门课的
                 {
                     string update_cpno = "UPDATE Course SET Cpno='" + CouCpno + "' WHERE Cno='" + CouCno + "' OR Cname='"+CouCname+"'";
                     SqlCommand cmd2 = new SqlCommand(update_cpno, con);
                     cmd2.ExecuteNonQuery();
                 }

                 if (CouCcredit != "" && CouCno != "" || CouCname != "" && CouCcredit != "")//修改Credit,通过课程号修改一门课的或者通过课程名修改多门课的
                 {
                     string update_credit = "UPDATE Course SET Ccredit='" + CouCcredit + " 'WHERE Cno='" + CouCno + "' OR Cname='"+CouCname+"'";
                     SqlCommand cmd3 = new SqlCommand(update_credit, con);
                     cmd3.ExecuteNonQuery();
                 }
              
            }
            catch
            {
                MessageBox.Show("输入数据违反要求!");
            }
            finally
            {
                //con.Dispose();      //关闭数据库
                con.Close();
            }
            this.courseTableAdapter.Fill(this.schoolDataSet1.Course);
        }

查找

private void buttonselect_Click(object sender, EventArgs e)//查找
        {
            string CouCno = textBoxcno.Text.Trim();
            string CouCname = textBoxcname.Text.Trim();
            string CouCpno = textBoxcpno.Text.Trim();
            string CouCcredit = textBoxcredit.Text.Trim();
            String conn = "Data Source =.; Initial Catalog = School;  Persist Security Info = True;User ID = sa; Password = 123";
            SqlConnection sqlconnection = new SqlConnection(conn);//实例化连接对象

            try
            {
                sqlconnection.Open();

                if (CouCno != "")//按照课序号查找,只有一个
                {
                    String select_by_cno = "select * from Course where Cno='" + CouCno + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_cno, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                    //将读出来的值赋给数据源,再将数据源给dataGridView
                }
                if (CouCname != "" && CouCpno == "" && CouCcredit == "")//按照课程名查找
                {
                    String select_by_cname = "select * from Course where Cname='" + CouCname + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_cname, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if (CouCpno != "" && CouCname == "" && CouCcredit == "")//按照Cpno查找
                {
                    String select_by_cpno = "select * from Course where Cpno='" + CouCpno + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_cpno, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if (CouCcredit != "" && CouCname == "" && CouCpno == "")//按照Credit查找
                {
                    String select_by_credit = "select * from Course where Ccredit='" + CouCcredit + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_credit, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if (CouCname != "" && CouCpno != "" && CouCcredit == "")//按照Cname和Cpno查找
                {
                    String select_by_namepno = "select * from Course where Cname='" + CouCname + "' AND Cpno='"+CouCpno+"'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_namepno, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if (CouCname != "" && CouCcredit != "" && CouCpno == "")//按照Cname和Ccredit查找
                {
                    String select_by_namecredit = "select * from Course where Cname='" + CouCname + "' AND Ccredit='" + CouCcredit + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_namecredit, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if (CouCpno != "" && CouCcredit != "" && CouCname == "")//按照Cpno和Ccredit查找
                {
                    String select_by_pnocredit = "select * from Course where Cpno='" + CouCpno + "' AND Ccredit='" + CouCcredit + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_pnocredit, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if (CouCname != "" && CouCpno != "" && CouCcredit != "")//按照Cname和Cpno和Ccredit查找
                {
                    String select_by_pnocreditname = "select * from Course where Cpno='" + CouCpno + "' AND Ccredit='" + CouCcredit + "' AND Cname='"+CouCname+"'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_pnocreditname, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
            }
            catch
            {
                MessageBox.Show("查询语句有误,请认真检查SQL语句");
            }
            finally
            {
                sqlconnection.Close();
            }
        }

学生-课程表
学生信息管理系统 C# + SQL_第20张图片
增加

  private void buttonadd_Click(object sender, EventArgs e)//增加
        {
            string SCsno = textBoxsno.Text.Trim();
            string SCcno = textBoxcno.Text.Trim();
            string SCgrade = textBoxgrade.Text.Trim();
            try
            {
                con.Open();     //打开数据库
                string insertsc = "INSERT INTO SC(Sno,Cno,Grade)" + "VALUES('" + SCsno + "','" + SCcno + "'," +SCgrade +")";
                SqlCommand cmd = new SqlCommand(insertsc, con);
                cmd.ExecuteNonQuery();      //将增加后的信息直接出来
            }
            catch
            {
                MessageBox.Show("输入数据违反要求!");
            }
            finally
            {
                con.Close();      //关闭数据库                                
            }
            this.sCTableAdapter.Fill(this.schoolDataSet3.SC);
        }

删除

 private void buttondelete_Click(object sender, EventArgs e)//删除
        {
            try
            {         
               con.Open(); //打开数据库
                string select_Sno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是Sno
                string select_Cno = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();//选择当前行第2列的值,也就是Cno
                string delete_by_SnoCno = "delete from SC where Sno='" + select_Sno + "' and Cno='" + select_Cno + "'";//sql删除语句
                SqlCommand cmd = new SqlCommand(delete_by_SnoCno, con);
                cmd.ExecuteNonQuery();             
            }
            catch
            {
                MessageBox.Show("请选择正确行!");
            }
            finally
            {                 
                con.Close(); //关闭数据库
            }
            this.sCTableAdapter.Fill(this.schoolDataSet3.SC);
        }

修改:修改成绩

 private void buttonchange_Click(object sender, EventArgs e)//修改
        {
            string SCsno = textBoxsno.Text.Trim();
            string SCcno = textBoxcno.Text.Trim();
            string SCgrade = textBoxgrade.Text.Trim();

            try
            {
                con.Open();     //打开数据库

                if (SCsno == "" || SCcno == "")
                {
                    MessageBox.Show("Sno和Cno不能为空!");
                }
                if (SCgrade != "")//根据Sno和Cno修改Grade
                {
                    string update_grade = "UPDATE SC SET Grade='" + SCgrade + "' WHERE Sno='"+SCsno+"' AND Cno='"+SCcno+"'";
                    SqlCommand cmd = new SqlCommand(update_grade,con);
                    cmd.ExecuteNonQuery();
                }               
            }
            catch
            {
                MessageBox.Show("输入数据违反要求!");
            }
            finally
            {
                   //关闭数据库
                con.Close();
            }
            this.sCTableAdapter.Fill(this.schoolDataSet3.SC);
        }

查找

 private void buttonselect_Click(object sender, EventArgs e)//查找
        {
            this.Hide();
            Admin_SC_grade adsc = new Admin_SC_grade();
            adsc.ShowDialog();        
        }

查找
学生信息管理系统 C# + SQL_第21张图片
查找,Sno、Cno、Grade查找

 private void buttonselect_Click(object sender, EventArgs e)
        {
            string SCsno = textBoxsno.Text.Trim();
            string SCcno = textBoxcno.Text.Trim();
            string SCgrade = textBoxgrade.Text.Trim();          
            SqlConnection sqlconnection = new SqlConnection(conn);//实例化连接对象
            try
            {
                sqlconnection.Open();

                if (SCcno != "" && SCsno != "")//按照Sno和Cno查询
                {
                    String select_by_snocno = "select * from SC where Sno='" + SCsno + "' AND Cno='" + SCcno + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_snocno, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                    //将读出来的值赋给数据源,再将数据源给dataGridView
                }
                if (SCsno != "" && SCcno == "" && SCgrade == "")//按照学号查询
                {
                    String select_by_sno = "select * from SC where Sno='" + SCsno + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_sno, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if (SCcno != "" && SCsno == "" && SCgrade == "")//按照Cno查找
                {
                    String select_by_cno = "select * from SC where Cno='" + SCcno + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_cno, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if (SCgrade != "" && SCsno == "" && SCcno == "")//按照Grade查询
                {
                    String select_by_grade = "select * from SC where Grade='" + SCgrade + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_grade, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
               
                if (SCcno != "" && SCgrade != "" && SCsno == "")//按照Cno和Grade查询
                {
                    String select_by_cnograde = "select * from SC where Cno='" + SCcno + "' and Grade='" + SCgrade + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_cnograde, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if (SCsno != "" && SCgrade != "" && SCcno == "")//按照Sno和Grade查询
                {
                    String select_by_snograde = "select * from SC where Sno='" + SCsno + "' and Grade='" + SCgrade + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_snograde, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
            }
            catch
            {
                MessageBox.Show("查询语句有误,请认真检查SQL语句");
            }
            finally
            {
                sqlconnection.Close();
            }
        }

查找不及格成绩

 private void buttonnograde_Click(object sender, EventArgs e)//不及格成绩
        {
            string SCsno = textBoxsno.Text.Trim();
            string SCcno = textBoxcno.Text.Trim();          
            SqlConnection sqlconnection = new SqlConnection(conn);//实例化连接对象

            try
            {
                sqlconnection.Open();

                if (SCsno != "" && SCcno == "")//查询某个学生的不及格成绩
                {
                    string select_nograde = "select * from SC where Grade<60 and Sno='" + SCsno + "'";
                       
                    SqlCommand sqlcommand = new SqlCommand(select_nograde, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if (SCcno != "" && SCsno == "")//查询某个课程的不及格成绩
                {
                    string select_nograde = "select * from SC where Grade<60 and Cno='" + SCcno + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_nograde, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
            }
            catch
            {
                MessageBox.Show("查询语句有误,请认真检查SQL语句");
            }
            finally
            {
                sqlconnection.Close();
            }
        }

按分数段查询

 private void button3_Click(object sender, EventArgs e)//分数段查询
        {
            string SCcno = textBoxcno.Text.Trim();
            string SCmin = textBoxmin.Text.Trim();
            string SCmax = textBoxmax.Text.Trim();
            String conn = "Data Source =.; Initial Catalog = School;  Persist Security Info = True;User ID = sa; Password = 123";
            SqlConnection sqlconnection = new SqlConnection(conn);//实例化连接对象           
            try
            {
                sqlconnection.Open();
                if (SCmin != "" && SCmax != "" && SCcno != "")//查询在特定分数段的学生
                {
                    String select_by_minmax = "select * from SC where Cno='" + SCcno + "' and Grade between '" + SCmin + "' and '" + SCmax + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_minmax, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;                   
                } 
                if (SCcno != "" && SCmin != "" && SCmax == "")//查询在特定成绩之上的学生     
                {
                    String select_by_cnograde = "select * from SC where Cno='" + SCcno + "' and Grade>'" + SCmin + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_cnograde, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                 if (SCcno != "" && SCmin == "" && SCmax != "")//查询在特定成绩之下的学生
                {
                    String select_by_cnograde = "select * from SC where Cno='" + SCcno + "' and Grade<'" + SCmax + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_cnograde, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
            }
            catch
            {
                MessageBox.Show("查询语句有误,请认真检查SQL语句");
            }
            finally
            {
                sqlconnection.Close();

            }
        }

登录日志
学生信息管理系统 C# + SQL_第22张图片
刷新按钮

 private void buttonchange_Click(object sender, EventArgs e)//刷新
        {
            this.sysLogTableAdapter.Fill(this.schoolDataSet2.SysLog);
        }

个人信息
学生信息管理系统 C# + SQL_第23张图片
显示个人信息

 private void Admin_info_Load(object sender, EventArgs e)
        {
            try
            {
                Form1 m = new Form1();
                string select_name = m.Get();
                string connString = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=123";//数据库连接字符串
                SqlConnection connection = new SqlConnection(connString);//创建connection对象              
                connection.Open(); //打开数据库连接

                //显示信息
                //在显示出生日期,注意DataTime格式的转换,以及完成查询后的标签内容的转换                
                string select_a = "select Ano,Aname,Asex,UserMobile,UserBirthday,Asalary,Atitle from Admin,SysUser where Admin.Ano=SysUser.UserSchoolID and UserID='" + select_name + "'";
                SqlCommand cmd = new SqlCommand(select_a, connection);
                SqlDataReader dr = cmd.ExecuteReader();//读取数据
                dr.Read();
                if (dr.HasRows)
                {
                    labelno.Text = dr[0].ToString();
                    labelname.Text = dr[1].ToString();
                    labelsex.Text = dr[2].ToString();
                    labelphone.Text = dr[3].ToString();                 
                     labelbirth.Text = Convert.ToString(dr.GetDateTime(4).ToShortDateString());
                    //出生日期只显示年月日
                    labelsalary.Text = dr[5].ToString();
                    labeltitle.Text = dr[6].ToString();
                }
                else { MessageBox.Show("您的信息还未录入!"); }
                dr.Close();

                //显示图片
                string sql = "select UserPhoto from SysUser where UserID = '" + select_name + "'";                
                SqlCommand command = new SqlCommand(sql, connection);//创建SqlCommand对象
                //创建DataAdapter对象
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                //创建DataSet对象
                DataSet dataSet = new DataSet();
                dataAdapter.Fill(dataSet, "SysUser");
                int c = dataSet.Tables["SysUser"].Rows.Count;
                if (c > 0)
                {
                    Byte[] mybyte = new byte[0];
                    mybyte = (Byte[])(dataSet.Tables["SysUser"].Rows[c - 1]["UserPhoto"]);
                    MemoryStream ms = new MemoryStream(mybyte);
                    pictureBox1.Image = Image.FromStream(ms);
                }
                else
                    pictureBox1.Image = null;
                connection.Close();
                }
           
            catch
            {
                MessageBox.Show("显示信息失败!");
            }
            
        }

修改密码
学生信息管理系统 C# + SQL_第24张图片
MD5加密

  public static string EncryptWithMD5(string source)      //MD5加密
        {
            byte[] sor = Encoding.UTF8.GetBytes(source);
            MD5 md5 = MD5.Create();
            byte[] result = md5.ComputeHash(sor);
            StringBuilder strbul = new StringBuilder(40);
            for (int i = 0; i < result.Length; i++)
            {
                strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
            }
            return strbul.ToString();
        }

点击确定按钮,密码修改成功

  private void buttonok_Click(object sender, EventArgs e)
        {
            try
            {
                Form1 m = new Form1();
                string select_name = m.Get();
                string newpassword = textBoxnewpassword.Text.Trim();
                string newpassword1 = textBoxnewpassword1.Text.Trim();

                string connString = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=123";//数据库连接字符串
                SqlConnection connection = new SqlConnection(connString);//创建connection对象              
                connection.Open(); //打开数据库连接

                if (textBoxusername.Text.Trim() == select_name)
                {
                    if (newpassword == newpassword1)//密码与确认密码的内容相同
                    {
                        string sql = "update SysUser set UserPassWord = @upw where UserID = @id";
                        SqlCommand cmd = new SqlCommand(sql, connection);

                        SqlParameter sqlParameter = new SqlParameter("@upw", EncryptWithMD5(textBoxnewpassword.Text.Trim()));
                        cmd.Parameters.Add(sqlParameter);
                        SqlParameter sqlParameter1 = new SqlParameter("@id", textBoxusername.Text.Trim());
                        cmd.Parameters.Add(sqlParameter1);

                        cmd.ExecuteNonQuery();
                        connection.Close();
                        MessageBox.Show("密码修改成功!");
                    }
                    else
                    {
                        MessageBox.Show("请再次检查输入的密码是否正确!");
                    }
                }
                else
                {
                    MessageBox.Show("请输入正确的用户名!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           // this.Close();
        }

新密码和确认新密码的格式限制

 private void textBoxnewpassword_Leave(object sender, EventArgs e)
        {
            if (textBoxnewpassword.Text.Trim() != "")
            {
                //使用regex(正则表达式)进行格式设置 至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符。
                Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");

                if (regex.IsMatch(textBoxnewpassword.Text))//判断格式是否符合要求
                {
                    //MessageBox.Show("输入密码格式正确!");
                }
                else
                {
                    MessageBox.Show("至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符!");
                    textBoxnewpassword.Focus();
                }
            }
            else
            {
                MessageBox.Show("密码不能为空!");
            }
        }

        private void textBoxnewpassword1_Leave(object sender, EventArgs e)
        {
            if (textBoxnewpassword1.Text.Trim() != "")
            {
                //使用regex(正则表达式)进行格式设置 至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符。
                Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");

                if (regex.IsMatch(textBoxnewpassword1.Text))//判断格式是否符合要求
                {
                    //MessageBox.Show("输入密码格式正确!");
                }
                else
                {
                    MessageBox.Show("至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符!");
                    textBoxnewpassword1.Focus();
                }
            }
            else
            {
                MessageBox.Show("确定密码不能为空!");
            }
        }

视频讲解链接:视频讲解
代码下载:代码

你可能感兴趣的:(数据库,c#,sql)