数据库课程设计——学生信息管理系统C#,SQL Sever

Posted Beth Harmon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库课程设计——学生信息管理系统C#,SQL Sever相关的知识,希望对你有一定的参考价值。

目录

利用SQL Sever和 VS C#实现

一、程序流程图

二、具体实现:利用SQL Sever和 VS实现,使用C#连接数据库

1、新建一个名为MySchool的数据库​​​​​​​

2、C#连接数据库,并实现对MySchool数据库的增、删、改、查操作

(1)主界面 

(2)学生登录页面

(3)个人主页

(4)注册页面

(5)管理员登录页面

(6)管理员主页 

(7)登录日志页面 

(8)学生信息页面

(9)课程信息页面

(10)学生成绩信息页面

(11)成绩统计页面

(12)在主界面点击右上角的“×”即退出系统。


利用SQL Sever和 VS C#实现

一、程序流程图

 图1:程序流程图

二、具体实现:利用SQL Sever和 VS实现,使用C#连接数据库

1、新建一个名为MySchool的数据库

(1)首先,连接数据库,一定一定要选择“SQL Sever身份验证”方式建立连接,不懂怎么用这种方式连接的可以自行搜索一下,很简单。

图2:数据库连接 

 (2)新建一个名为“MySchool”的数据库,其中含有的表如下:

图3:MySchool数据库 

对应的SQL语句如下:

USE MySchool;

DROP TABLE IF EXISTS SC       /*成绩*/
DROP TABLE IF EXISTS Student  /*学生信息*/
DROP TABLE IF EXISTS Course   /*课程*/
DROP TABLE IF EXISTS StudentUser  /*学生用户信息*/
DROP TABLE IF EXISTS Administrator  /*管理员用户信息*/
DROP TABLE IF EXISTS SysLog   /*注册日志*/
DROP TABLE IF EXISTS SysLog1   /*登陆日志*/
DROP TABLE IF EXISTS AVG1   /*登陆日志*/

CREATE TABLE StudentUser          
 (	
 ID NCHAR(20) PRIMARY KEY,               /*学号*/  
 PassWord NCHAR(32) ,					/*密码*/
 Sex CHAR(2) ,							/*性别*/
 Birthday datetime,						/*生日*/
 UserMobile NCHAR(11),					/*电话号码*/
 ); 
 CREATE TABLE Administrator          
 (	
 ID NCHAR(20) PRIMARY KEY,               /*工号*/  
 PassWord NCHAR(32) ,             /*密码*/
 Sex CHAR(2),							/*性别*/
 Birthday datetime,				/*生日*/
 UserMobile NCHAR(11),					/*电话号码*/
 );

 CREATE TABLE SysLog          
 (	[UserMobile]
 UserID NCHAR(20) ,  /*id*/
 dentity CHAR(20),  /*学生或管理员*/
 DateAndTime datetime,  /*注册时间*/
 UserOperation NCHAR(200)  /*操作方式*/
 ); 
CREATE TABLE SysLog1          
 (	
 UserID NCHAR(20) ,  /*id*/
 dentity CHAR(20),  /*学生或管理员*/
 DateAndTime datetime,  /*登陆时间*/
 UserOperation NCHAR(200)  /*登陆操作方式*/
 );

CREATE TABLE Student          
 (	
 Sno CHAR(9) PRIMARY KEY,        /* 列级完整性约束条件,Sno是主码*/                  
 Sname CHAR(20) UNIQUE,             /* Sname取唯一值*/
 Ssex CHAR(2),					/*性别*/
 Sage SMALLINT,					/*年龄*/
 Sdept CHAR(20)					/*专业*/
 ); 

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

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  StudentUser VALUES ('20181101111','123456','女',1999-1-1,'13812345678',NULL);
INSERT  INTO  Administrator VALUES ('2018110',substring(sys.fn_sqlvarbasetostr(HashBytes('MD5','123456')),3,32),'女',1989-1-1,'13812345687',NULL);
INSERT  INTO  Administrator VALUES ('2018111',substring(sys.fn_sqlvarbasetostr(HashBytes('MD5','123456')),3,32),'女',1989-2-1,'13812655687',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);

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

SELECT * FROM SC

--当学生用户信息更新,触发器启动,将更新的内容存至注册日志
IF(OBJECT_ID('regist_recorder1') is not null)        -- 判断名为 regist_recorder 的触发器是否存在
DROP TRIGGER regist_recorder1        -- 删除触发器
GO
CREATE TRIGGER regist_recorder1
ON StudentUser  	         
AFTER
INSERT
AS 
	declare @UserName    nchar(20)
	declare @DateTime    datetime
	declare @UserOperation nchar(200)
	declare @dentity CHAR(20)

	select @UserName = ID FROM StudentUser
	select @DateTime = CONVERT(datetime,GETDATE(),120) 
	select @dentity ='StudentUser'

	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,dentity,DateAndTime,UserOperation)
	VALUES (@UserName,@dentity,@DateTime,@UserOperation)

--当管理员信息更新,触发器启动,将更新的内容存至注册日志
IF(OBJECT_ID('regist_recorder2') is not null)        -- 判断名为 regist_recorder 的触发器是否存在
DROP TRIGGER regist_recorder2        -- 删除触发器
GO
CREATE TRIGGER regist_recorder2
ON Administrator 	         
AFTER
INSERT
AS 
	declare @UserName    nchar(20)
	declare @DateTime    datetime
	declare @UserOperation nchar(200)
	declare @dentity CHAR(20)

	select @UserName = ID FROM Administrator
	select @DateTime = CONVERT(datetime,GETDATE(),120) 
	select @dentity ='Administrator'

	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,dentity,DateAndTime,UserOperation)
	VALUES (@UserName,@dentity,@DateTime,@UserOperation)




--建一个表存储各科平均分
--建一个表存储各科平均分
CREATE TABLE AVG1
	(
		Cname CHAR(10),   /* 科目*/	
		avg1 INT
	);
INSERT  INTO AVG1(Cname,avg1)	VALUES ('数据库',NULL);
INSERT  INTO AVG1(Cname,avg1)	VALUES ('数学',NULL);
INSERT  INTO AVG1(Cname,avg1)	VALUES ('信息系统',NULL);
INSERT  INTO AVG1(Cname,avg1)	VALUES ('操作系统',NULL);
INSERT  INTO AVG1(Cname,avg1)	VALUES ('数据结构',NULL);
INSERT  INTO AVG1(Cname,avg1)	VALUES ('数据处理',NULL);
INSERT  INTO AVG1(Cname,avg1)	VALUES ('Pascal语言',NULL);

--将成绩信息通过下列存储过程算出各科平均分并存储至AVG表
IF (exists (select * from sys.objects where name = 'COURSE_AVG1'))
	DROP PROCEDURE COURSE_AVG1
	GO
	CREATE  PROCEDURE COURSE_AVG1
	AS
	BEGIN TRANSACTION TRANS  
	DECLARE 
	    @SX INT,    /* 数学总分 */
	    @XXXT INT,    /* 信息系统总分 */
	    @CZXT INT,    /* 操作系统总分 */
		@SJJG INT,    /* 数据结构总分 */
		@SJK_C INT,   /* 数据库总分 */
		@SJCL INT,   /*数据处理总分*/
		@P INT       /*Pascal语言*/
	SELECT @SX=AVG(Grade) FROM SC
						WHERE  Cno='2 '

	SELECT @XXXT=AVG(Grade) FROM SC
						WHERE  Cno='3'

	SELECT @CZXT=AVG(Grade) FROM SC
						WHERE  Cno='4'

	SELECT @SJJG=AVG(Grade) FROM SC
						WHERE  Cno='5'

	SELECT @SJK_C=AVG(Grade) FROM SC
						WHERE  Cno='1'
	SELECT @SJCL=AVG(Grade) FROM SC
						WHERE  Cno='6'
	SELECT @P=AVG(Grade) FROM SC
						WHERE  Cno='7'
	
	BEGIN 
	UPDATE AVG1 SET avg1=@SJK_C WHERE Cname='数据库'
	UPDATE AVG1 SET avg1=@SX WHERE Cname='数学'
	UPDATE AVG1 SET avg1=@XXXT WHERE Cname='信息系统'
	UPDATE AVG1 SET avg1=@CZXT WHERE Cname='操作系统'
	UPDATE AVG1 SET avg1=@SJJG WHERE Cname='数据结构'
	UPDATE AVG1 SET avg1=@SJCL WHERE Cname='数据处理'
	UPDATE AVG1 SET avg1=@P WHERE Cname='Pascal语言'
	COMMIT TRANSACTION TRANS; 
	END

2、C#连接数据库,并实现对MySchool数据库的增、删、改、查操作

(1)主界面:主界面包含欢迎文本和两个可选项,如下图3所示分别为“学生登录”和“管理员登录”,选择不同的选项则分别进入不同的页面。

 图4:主界面

对应C#实现如下: 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SchoolManage

    public partial class FormLogin : Form
    
        public FormLogin()
        
            InitializeComponent();
        

        private void buttonStuLogin_Click(object sender, EventArgs e)
        
            FormStuLogin FormStuLogin = new FormStuLogin(); //学生登录窗体
            FormStuLogin.Show();//跳转至学生登录窗体
            this.Hide();  //隐藏原窗体
        

        private void buttonManagerLogin_Click(object sender, EventArgs e)
        
            FormManagerLogin FormManagerLogin = new FormManagerLogin(); //管理员登录窗体
            FormManagerLogin.Show();//跳转至管理员登录窗体
            this.Hide();
        

        private void FormLogin_FormClosing_1(object sender, FormClosingEventArgs e)
        
            
        

        private void FormLogin_FormClosed(object sender, FormClosedEventArgs e)
        
            while (MessageBox.Show("即将退出系统,您确认退出吗?", "提示!", MessageBoxButtons.YesNo)== DialogResult.Yes)
            
                System.Environment.Exit(System.Environment.ExitCode);
            
            


        


    

(2)学生登录页面:在主界面选择学生登录按钮则进入学生登录页面,页面功能如下图所示。若用户有该系统的账号,则可选择直接登录:先输入用户名,密码以及验证码,三者均正确才能登陆成功。

图5:学生登录页面

图6:学生登录成功

若其中一项不正确则会弹窗提示你重新输入,如下图所示:

图7:学生登录失败

对应C#实现如下(连接数据库的代码实现部分大家根据自身情况自行修改,下同):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SchoolManage

    public partial class FormStuLogin : Form
    
        public FormStuLogin()
        
            InitializeComponent();
        
        
        

        public string code;


        private 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 buttonLogin_Click(object sender, EventArgs e)
        
            string username = tBUserName.Text.Trim();  //取出账号
            string password = EncryptWithMD5(tBPassword.Text.Trim());  //取出密码并加密

            string myConnString = "Data Source=.;Initial Catalog=MySchool;Persist Security Info=True;User ID=sa;Password=yourpassword";

            SqlConnection sqlConnection = new SqlConnection(myConnString);  //实例化连接对象
            sqlConnection.Open();

            string sql = "select ID,PassWord from StudentUser where ID = '" + username + "' and PassWord = '" + password + "'";                                            //编写SQL命令
            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);

            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();//读取数据

            if (sqlDataReader.HasRows && tBVerifyCode.Text == code)//验证是否有该用户及密码且验证码正确
            
                MessageBox.Show("欢迎使用!");             //登录成功
                StuMain stuMain = new StuMain();
                stuMain.GetId(username);
                stuMain.Show();//显示下一界面
                this.Hide();
            
            else if (sqlDataReader.HasRows && tBVerifyCode.Text != code)
            
                MessageBox.Show("验证码错误,登录失败!");
                return;
            
            else
            
                MessageBox.Show("账号密码有误,登录失败!");
                return;
            
            sqlDataReader.Close();
            sql = "insert into SysLog1 values ( '" + username + "' , 'Student','" + DateTime.Now + "' , '" + "Login" + "')";                                            //编写SQL命令,把登陆信息存入登录日志
            SqlCommand sqlCommand1 = new SqlCommand(sql, sqlConnection);
            sqlCommand1.ExecuteNonQuery();
            sqlConnection.Close();

        
        

        private void buttonReg_Click(object sender, EventArgs e)
        
            this.Hide();
            RegisterForm registerForm = new RegisterForm();
            registerForm.Show();
        

        private void FormStuLogin_Load_1(object sender, EventArgs e)
        
            //随机实例化 
            Random ran = new Random();
            int number;
            char code1;
            //取五个数 
            for (int i = 0; i < 5; i++)
            
                number = ran.Next();
                if (number % 2 == 0)
                    code1 = (char)('0' + (char)(number % 10));
                else
                    code1 = (char)('A' + (char)(number % 26)); //转化为字符 

                this.code += code1.ToString();
            

            lbVerifyCode.Text = code;
        

        private void lbVerifyCode_Click(object sender, EventArgs e)
        

        

        private void buttonCancel_Click_1(object sender, EventArgs e)
        
            this.Close();
        

        private void FormStuLogin_FormClosing_1(object sender, FormClosingEventArgs e)
        
            FormLogin m2 = new FormLogin();
            m2.Show();
        

        private void lbPassword_Click(object sender, EventArgs e)
        

        
    

(3)个人主页:学生用户若成功登录则进入个人主页,个人主页共包含“个人基本信息”和“成绩单”两个分页,所展示的信息如下图所示,学生能够看到自己的个人信息和个人成绩,以及各门课程的详细信息。

图8:个人主页——个人信息页

图9:个人主页——成绩单页

对应C#实现如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace SchoolManage

    public partial class StuMain : Form
    
        public StuMain()
        
            InitializeComponent();
        

        private void buttonExit_Click(object sender, EventArgs e)
        
            this.Close();
        


        string Id;
        public void GetId(string id)//上个窗体调用此函数,将id值传过来
        
            lbStuId.Text = id;
            lbStuId.Refresh();
            Id = id;
        



        private void StuMain_Load_1(object sender, EventArgs e)
        
            // TODO: 这行代码将数据加载到表“mySchoolDataSet.Course”中。您可以根据需要移动或移除它。
            this.courseTableAdapter.Fill(this.mySchoolDataSet.Course);
            
            string name, sex, dept, tel;
            int age;
            string connString = "Data Source=.;Initial Catalog=MySchool;Persist Security Info=True;User ID=sa;Password=yourpassword";//数据库连接字符串
            SqlConnection conn = new SqlConnection(connString);//创建connection对象
            conn.Open();//打开数据库  

            //创建数据库命令  
            SqlCommand cmd = conn.CreateCommand();
            //创建查询语句  
            cmd.CommandText = "select * from Student where Sno = '" + Id + "';select * from StudentUser where ID = '" + Id + "'";
            //从数据库中读取数据流存入reader中  
            SqlDataReader reader = cmd.ExecuteReader();

            //从reader中读取下一行数据,如果没有数据,reader.Read()返回flase  
            while (reader.Read())
            
                name = reader.GetString(reader.GetOrdinal("Sname"));
                sex = reader.GetString(reader.GetOrdinal("Ssex"));
                age = reader.GetInt16(reader.GetOrdinal("Sage"));
                dept = reader.GetString(reader.GetOrdinal("Sdept"));

                lbStuName.Text = name;
                lbStuName.Refresh();
                lbStuSex.Text = sex;
                lbStuAge.Text = age.ToString();
                lbStuDept.Text = dept;
                break;
            
            reader.NextResult();//执行下一句操作


            //从reader中读取下一行数据,如果没有数据,reader.Read()返回flase  
            while (reader.Read())
            

                tel = reader.GetString(reader.GetOrdinal("UserMobile"));

                lbPhone.Text = tel;
                break;
            

            reader.Close();


            SqlDataAdapter dap = new SqlDataAdapter("select * from SC where Sno='" + Id + "'", conn);//查询
            DataSet ds = new DataSet();//创建DataSet对象
            dap.Fill(ds);//填充DataSet数据集
            dataGridView1.DataSource = ds.Tables[0].DefaultView;//显示查询后的数据
            conn.Close();
            int i = 0;
            int x = 0, h = 0;
            int a;
            for (; i < ds.Tables[0].Rows.Count; i++)//读取DataSet中的指定数据
            
                x += int.Parse(ds.Tables[0].Rows[i][2].ToString());
                if (int.Parse(ds.Tables[0].Rows[i][2].ToString()) > 59)
                    h++;
            
            if (i == 0) a = 0;
            else a = x / i;
            lbPassnum.Text = h.ToString();
            lbAvgSco.Text = a.ToString();
        

        private void StuMain_FormClosing_1(object sender, FormClosingEventArgs e)
        
            FormLogin m2 = new FormLogin();
            m2.Show();
        

        private void StuMain_FormClosed(object sender, FormClosedEventArgs e)
        
            
        

        
    

(4)注册页面:若学生用户没有登陆账号,则需在学生登录界面选择注册按钮,进入注册界面输入相应信息进行注册。成功注册的前提是你为该校的学生且各信息已填写完整。

图10:注册页面

图11:注册成功

图12:注册失败

对应C#实现如下:  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SchoolManage

    public partial class RegisterForm : Form
    
        public RegisterForm()
        
            InitializeComponent();
        

        private void buttonRegister_Click(object sender, EventArgs e)
        
            if (tBStuId.Text.Trim() != "" && tBPassword.Text.Trim() != "" && cBSex.Text.Trim() != "" && tBPhoneNum.Text.Trim() != "")
            //确认一些信息输入非空
                try
                

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

                    string username = tBStuId.Text.Trim();  //取出账号
                    string sex = cBSex.Text.Trim();  //取出性别


                    connection.Open();

                    string sql1 = "select Sno,Ssex from Student where Sno = '" + username + "' and Ssex = '" + sex + "'";                                            //编写SQL命令,查找学生信息中是否有该用户
                    SqlCommand sqlCommand1 = new SqlCommand(sql1, connection);//确认是否有该学员
                    SqlDataReader sqlDataReader1 = sqlCommand1.ExecuteReader();
                    bool a = sqlDataReader1.HasRows;
                    sqlDataReader1.Close();
                    if (a)
                    

                        string sql2 = "select ID from StudentUser where ID = '" + username + "'";                                            //编写SQL命令
                        SqlCommand sqlCommand2 = new SqlCommand(sql2, connection);//确认是否已经注册过
                        SqlDataReader sqlDataReader2 = sqlCommand2.ExecuteReader();
                        if (!sqlDataReader2.HasRows)
                        
                            string sql3 = "insert into StudentUser (ID,   PassWord ,Sex, UserMobile, Birthday) " +
                                                            "values (@userid, @userpassword,@sex,@usermobile,@Birthday)";

                            SqlCommand command = new SqlCommand(sql3, connection);
                            SqlParameter sqlParameter = new SqlParameter("@userid", tBStuId.Text);
                            command.Parameters.Add(sqlParameter);
                            sqlParameter = new SqlParameter("@userpassword", EncryptWithMD5(tBPassword.Text));
                            command.Parameters.Add(sqlParameter);
                            sqlParameter = new SqlParameter("@sex", cBSex.Text);
                            command.Parameters.Add(sqlParameter);
                            sqlParameter = new SqlParameter("@usermobile", tBPhoneNum.Text);
                            command.Parameters.Add(sqlParameter);
                            sqlParameter = new SqlParameter("@Birthday", dateTimePicker1.Value);
                            command.Parameters.Add(sqlParameter);
                            sqlDataReader2.Close();
                            //打开数据库连接

                            command.ExecuteNonQuery();
                            connection.Close();
                            MessageBox.Show("注册成功");
                        
                        else
                        
                            MessageBox.Show("该用户已注册。");
                        

                    
                    else
                    
                        MessageBox.Show("我校无该学员");
                    



                
                catch (Exception ex)
                
                    MessageBox.Show(ex.Message);
                


                this.Close();
            
            else
            
                MessageBox.Show("请将信息填写完整!");
            

        
        public Byte[] mybyte = new byte[0];
        public static string EncryptWithMD5(string source)
        
            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 tBPassword_Leave(object sender, EventArgs e)//校验密码格式
        
            if (tBStuId.Text.Trim() != "")
            
                //使用regex(正则表达式)进行格式设置 至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符。
                Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).3,20");

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


        private void RegisterForm_FormClosing(object sender, FormClosingEventArgs e)
        
            FormStuLogin m2 = new FormStuLogin();
            m2.Show();
        

        private void buttonCancel_Click_1(object sender, EventArgs e)
        
            this.Close();
        
    

(5)管理员登录页面:若您的身份为管理员,则可在主界面选择“管理员登录”选项,进入管理员登录界面。与学生登录相似,管理员登录成功的前提是正确输入各类信息。但管理员登录不提供“注册”选项,因为管理员身份必须由系统授予——即在数据库中填入相应信息才可成为管理员。

图13:管理员登录页面

图14:管理员登录成功

对应C#实现如下:  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SchoolManage

    public partial class FormManagerLogin : Form
    
        public FormManagerLogin()
        
            InitializeComponent();
        

        public string code;
        private void buttonLogin_Click(object sender, EventArgs e)
        
            string username = tBWorknum.Text.Trim();  //取出账号
            string password = EncryptWithMD5(tBPassword.Text.Trim());  //取出密码并加密

            string myConnString = "Data Source=.;Initial Catalog=MySchool;Persist Security Info=True;User ID=sa;Password=yourpassword";

            SqlConnection sqlConnection = new SqlConnection(myConnString);  //实例化连接对象
            sqlConnection.Open();

            string sql = "select ID,PassWord from Administrator where ID = '" + username + "' and PassWord = '" + password + "'";                                            //编写SQL命令
            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);

            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

            if (sqlDataReader.HasRows && tBVerifyCode.Text == code)
            
                MessageBox.Show("欢迎使用!");             //登录成功
                ManagerMain managerMain = new ManagerMain();
                managerMain.Show();
                this.Hide();
            
            else if (sqlDataReader.HasRows && tBVerifyCode.Text != code)
            
                MessageBox.Show("验证码错误,登录失败!");
                return;
            
            else
            
                MessageBox.Show("账号密码有误,登录失败!");
                return;
            
            sqlDataReader.Close();
            sql = "insert into SysLog1 values ( '" + username + "' , 'Administrator','" + DateTime.Now + "' , '" + "Login" + "')";                                            //编写SQL命令
            SqlCommand sqlCommand1 = new SqlCommand(sql, sqlConnection);
            sqlCommand1.ExecuteNonQuery();
            sqlConnection.Close();
        

        private string EncryptWithMD5(string source)
        
            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 FormManagerLogin_Load_1(object sender, EventArgs e)
        
            //随机实例化 
            Random ran = new Random();
            int number;
            char code1;
            //取五个数 
            for (int i = 0; i < 5; i++)
            
                number = ran.Next();
                if (number % 2 == 0)
                    code1 = (char)('0' + (char)(number % 10));
                else
                    code1 = (char)('A' + (char)(number % 26)); //转化为字符 

                this.code += code1.ToString();
            

            lbVerifyCode.Text = code;
        

        private void FormManagerLogin_FormClosing_1(object sender, FormClosingEventArgs e)
        
            FormLogin m2 = new FormLogin();
            m2.Show();
        

        private void buttonCancel_Click_1(object sender, EventArgs e)
        
            this.Close();
        
    

(6)管理员主页:管理员主页一共包含五个可选项,如下图所示。其中,对于“学生信息”,“课程信息”,“学生成绩”,管理员可按需进行增、删、改、查操作。

图15:管理员主页

对应C#实现如下:  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SchoolManage

    public partial class ManagerMain : Form
    
        public ManagerMain()
        
            InitializeComponent();
        

        

        private void buttonLog_Click(object sender, EventArgs e)
        
            this.Hide();
            LogMain m = new LogMain();
            m.Show();
        

        private void buttonStu_Click(object sender, EventArgs e)
        
            this.Hide();
            StuMessageMain m = new StuMessageMain();
            m.Show();
        

        private void buttonCourse_Click(object sender, EventArgs e)
        
            this.Hide();
            CourseMessageMain m = new CourseMessageMain();
            m.Show();
        

        private void buttonGrade_Click(object sender, EventArgs e)
        
            this.Hide();
            StuGradeMain m = new StuGradeMain();
            m.Show();
        

        private void buttonExit_Click(object sender, EventArgs e)
        
            this.Close();
        

        private void ManagerMain_FormClosed(object sender, FormClosedEventArgs e)
        
            //Application.Exit();
        

        private void ManagerMain_FormClosing(object sender, FormClosingEventArgs e)
        
            FormLogin m2 = new FormLogin();
            m2.Show();
        

       

        private void ManagerMain_Load(object sender, EventArgs e)
        

        
    

(7)登录日志页面:登录日志为管理员提供该系统所有用户的登录记录

图16:登录日志页面

对应C#实现如下:  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SchoolManage

    public partial class LogMain : Form
    
        public LogMain()
        
            InitializeComponent();
        

        private void LogMain_Load(object sender, EventArgs e)
        
            // TODO: 这行代码将数据加载到表“mySchoolDataSet1.SysLog1”中。您可以根据需要移动或移除它。
            this.sysLog1TableAdapter.Fill(this.mySchoolDataSet1.SysLog1);

        

        private void buttonReturn_Click(object sender, EventArgs e)
        
            this.Close();
        

        private void LogMain_FormClosing(object sender, FormClosingEventArgs e)
        
            ManagerMain m2 = new ManagerMain();
            m2.Show();
        
    

Java学生课程管理系统设计(C/S)

C语言课程设计"学生信息管理系统设计 "怎么做?

c语言课程设计 学生信息管理系统模拟

学生信息管理系统C语言编程

求用C语言编写一个简单的学生信息管理程序和课程设计报告

sql语句面试50题@20211219