程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

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

发布于2023-05-20 21:38     阅读(981)     评论(0)     点赞(12)     收藏(4)


目录

利用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语句如下:

  1. USE MySchool;
  2. DROP TABLE IF EXISTS SC /*成绩*/
  3. DROP TABLE IF EXISTS Student /*学生信息*/
  4. DROP TABLE IF EXISTS Course /*课程*/
  5. DROP TABLE IF EXISTS StudentUser /*学生用户信息*/
  6. DROP TABLE IF EXISTS Administrator /*管理员用户信息*/
  7. DROP TABLE IF EXISTS SysLog /*注册日志*/
  8. DROP TABLE IF EXISTS SysLog1 /*登陆日志*/
  9. DROP TABLE IF EXISTS AVG1 /*登陆日志*/
  10. CREATE TABLE StudentUser
  11. (
  12. ID NCHAR(20) PRIMARY KEY, /*学号*/
  13. PassWord NCHAR(32) , /*密码*/
  14. Sex CHAR(2) , /*性别*/
  15. Birthday datetime, /*生日*/
  16. UserMobile NCHAR(11), /*电话号码*/
  17. );
  18. CREATE TABLE Administrator
  19. (
  20. ID NCHAR(20) PRIMARY KEY, /*工号*/
  21. PassWord NCHAR(32) , /*密码*/
  22. Sex CHAR(2), /*性别*/
  23. Birthday datetime, /*生日*/
  24. UserMobile NCHAR(11), /*电话号码*/
  25. );
  26. CREATE TABLE SysLog
  27. ( [UserMobile]
  28. UserID NCHAR(20) , /*id*/
  29. dentity CHAR(20), /*学生或管理员*/
  30. DateAndTime datetime, /*注册时间*/
  31. UserOperation NCHAR(200) /*操作方式*/
  32. );
  33. CREATE TABLE SysLog1
  34. (
  35. UserID NCHAR(20) , /*id*/
  36. dentity CHAR(20), /*学生或管理员*/
  37. DateAndTime datetime, /*登陆时间*/
  38. UserOperation NCHAR(200) /*登陆操作方式*/
  39. );
  40. CREATE TABLE Student
  41. (
  42. Sno CHAR(9) PRIMARY KEY, /* 列级完整性约束条件,Sno是主码*/
  43. Sname CHAR(20) UNIQUE, /* Sname取唯一值*/
  44. Ssex CHAR(2), /*性别*/
  45. Sage SMALLINT, /*年龄*/
  46. Sdept CHAR(20) /*专业*/
  47. );
  48. CREATE TABLE Course
  49. (
  50. Cno CHAR(4) PRIMARY KEY,
  51. Cname CHAR(40),
  52. Cpno CHAR(4),
  53. Ccredit SMALLINT,
  54. );
  55. CREATE TABLE SC
  56. (
  57. Sno CHAR(9),
  58. Cno CHAR(4),
  59. Grade SMALLINT,
  60. PRIMARY KEY (Sno,Cno), /* 主码由两个属性构成,必须作为表级完整性进行定义*/
  61. FOREIGN KEY (Sno) REFERENCES Student(Sno), /* 表级完整性约束条件,Sno是外码,被参照表是Student */
  62. FOREIGN KEY (Cno)REFERENCES Course(Cno) /* 表级完整性约束条件, Cno是外码,被参照表是Course*/
  63. );
  64. INSERT INTO StudentUser VALUES ('20181101111','123456','女',1999-1-1,'13812345678',NULL);
  65. INSERT INTO Administrator VALUES ('2018110',substring(sys.fn_sqlvarbasetostr(HashBytes('MD5','123456')),3,32),'女',1989-1-1,'13812345687',NULL);
  66. INSERT INTO Administrator VALUES ('2018111',substring(sys.fn_sqlvarbasetostr(HashBytes('MD5','123456')),3,32),'女',1989-2-1,'13812655687',NULL);
  67. INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215121','李勇','男','CS',20);
  68. INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215122','刘晨','女','CS',19);
  69. INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215123','王敏','女','MA',18);
  70. INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215125','张立','男','IS',19);
  71. INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215128','陈冬','男','IS',20);
  72. SELECT * FROM Student
  73. INSERT INTO Course(Cno,Cname,Cpno,Ccredit) VALUES ('1','数据库',NULL,4);
  74. INSERT INTO Course(Cno,Cname,Cpno,Ccredit) VALUES ('2','数学',NULL,4);
  75. INSERT INTO Course(Cno,Cname,Cpno,Ccredit) VALUES ('3','信息系统',NULL,4);
  76. INSERT INTO Course(Cno,Cname,Cpno,Ccredit) VALUES ('4','操作系统',NULL,4);
  77. INSERT INTO Course(Cno,Cname,Cpno,Ccredit) VALUES ('5','数据结构',NULL,4);
  78. INSERT INTO Course(Cno,Cname,Cpno,Ccredit) VALUES ('6','数据处理',NULL,4);
  79. INSERT INTO Course(Cno,Cname,Cpno,Ccredit) VALUES ('7','Pascal语言',NULL,4);
  80. UPDATE Course SET Cpno = '5' WHERE Cno = '1'
  81. UPDATE Course SET Cpno = '1' WHERE Cno = '3'
  82. UPDATE Course SET Cpno = '6' WHERE Cno = '4'
  83. UPDATE Course SET Cpno = '7' WHERE Cno = '5'
  84. UPDATE Course SET Cpno = '6' WHERE Cno = '7'
  85. SELECT * FROM Course
  86. INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215121 ','1',92);
  87. INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215121 ','2',85);
  88. INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215121 ','3',88);
  89. INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215122 ','2',90);
  90. INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215122 ','3',80);
  91. SELECT * FROM SC
  92. --当学生用户信息更新,触发器启动,将更新的内容存至注册日志
  93. IF(OBJECT_ID('regist_recorder1') is not null) -- 判断名为 regist_recorder 的触发器是否存在
  94. DROP TRIGGER regist_recorder1 -- 删除触发器
  95. GO
  96. CREATE TRIGGER regist_recorder1
  97. ON StudentUser
  98. AFTER
  99. INSERT
  100. AS
  101. declare @UserName nchar(20)
  102. declare @DateTime datetime
  103. declare @UserOperation nchar(200)
  104. declare @dentity CHAR(20)
  105. select @UserName = ID FROM StudentUser
  106. select @DateTime = CONVERT(datetime,GETDATE(),120)
  107. select @dentity ='StudentUser'
  108. declare @op varchar(10)
  109. select @op=case when exists(select 1 from inserted) and exists(select 1 from deleted)
  110. then 'Update'
  111. when exists(select 1 from inserted) and not exists(select 1 from deleted)
  112. then 'Insert'
  113. when not exists(select 1 from inserted) and exists(select 1 from deleted)
  114. then 'Delete' end
  115. select @UserOperation = @op
  116. INSERT INTO SysLog(UserID,dentity,DateAndTime,UserOperation)
  117. VALUES (@UserName,@dentity,@DateTime,@UserOperation)
  118. --当管理员信息更新,触发器启动,将更新的内容存至注册日志
  119. IF(OBJECT_ID('regist_recorder2') is not null) -- 判断名为 regist_recorder 的触发器是否存在
  120. DROP TRIGGER regist_recorder2 -- 删除触发器
  121. GO
  122. CREATE TRIGGER regist_recorder2
  123. ON Administrator
  124. AFTER
  125. INSERT
  126. AS
  127. declare @UserName nchar(20)
  128. declare @DateTime datetime
  129. declare @UserOperation nchar(200)
  130. declare @dentity CHAR(20)
  131. select @UserName = ID FROM Administrator
  132. select @DateTime = CONVERT(datetime,GETDATE(),120)
  133. select @dentity ='Administrator'
  134. declare @op varchar(10)
  135. select @op=case when exists(select 1 from inserted) and exists(select 1 from deleted)
  136. then 'Update'
  137. when exists(select 1 from inserted) and not exists(select 1 from deleted)
  138. then 'Insert'
  139. when not exists(select 1 from inserted) and exists(select 1 from deleted)
  140. then 'Delete' end
  141. select @UserOperation = @op
  142. INSERT INTO SysLog(UserID,dentity,DateAndTime,UserOperation)
  143. VALUES (@UserName,@dentity,@DateTime,@UserOperation)
  144. --建一个表存储各科平均分
  145. --建一个表存储各科平均分
  146. CREATE TABLE AVG1
  147. (
  148. Cname CHAR(10), /* 科目*/
  149. avg1 INT
  150. );
  151. INSERT INTO AVG1(Cname,avg1) VALUES ('数据库',NULL);
  152. INSERT INTO AVG1(Cname,avg1) VALUES ('数学',NULL);
  153. INSERT INTO AVG1(Cname,avg1) VALUES ('信息系统',NULL);
  154. INSERT INTO AVG1(Cname,avg1) VALUES ('操作系统',NULL);
  155. INSERT INTO AVG1(Cname,avg1) VALUES ('数据结构',NULL);
  156. INSERT INTO AVG1(Cname,avg1) VALUES ('数据处理',NULL);
  157. INSERT INTO AVG1(Cname,avg1) VALUES ('Pascal语言',NULL);
  158. --将成绩信息通过下列存储过程算出各科平均分并存储至AVG表
  159. IF (exists (select * from sys.objects where name = 'COURSE_AVG1'))
  160. DROP PROCEDURE COURSE_AVG1
  161. GO
  162. CREATE PROCEDURE COURSE_AVG1
  163. AS
  164. BEGIN TRANSACTION TRANS
  165. DECLARE
  166. @SX INT, /* 数学总分 */
  167. @XXXT INT, /* 信息系统总分 */
  168. @CZXT INT, /* 操作系统总分 */
  169. @SJJG INT, /* 数据结构总分 */
  170. @SJK_C INT, /* 数据库总分 */
  171. @SJCL INT, /*数据处理总分*/
  172. @P INT /*Pascal语言*/
  173. SELECT @SX=AVG(Grade) FROM SC
  174. WHERE Cno='2 '
  175. SELECT @XXXT=AVG(Grade) FROM SC
  176. WHERE Cno='3'
  177. SELECT @CZXT=AVG(Grade) FROM SC
  178. WHERE Cno='4'
  179. SELECT @SJJG=AVG(Grade) FROM SC
  180. WHERE Cno='5'
  181. SELECT @SJK_C=AVG(Grade) FROM SC
  182. WHERE Cno='1'
  183. SELECT @SJCL=AVG(Grade) FROM SC
  184. WHERE Cno='6'
  185. SELECT @P=AVG(Grade) FROM SC
  186. WHERE Cno='7'
  187. BEGIN
  188. UPDATE AVG1 SET avg1=@SJK_C WHERE Cname='数据库'
  189. UPDATE AVG1 SET avg1=@SX WHERE Cname='数学'
  190. UPDATE AVG1 SET avg1=@XXXT WHERE Cname='信息系统'
  191. UPDATE AVG1 SET avg1=@CZXT WHERE Cname='操作系统'
  192. UPDATE AVG1 SET avg1=@SJJG WHERE Cname='数据结构'
  193. UPDATE AVG1 SET avg1=@SJCL WHERE Cname='数据处理'
  194. UPDATE AVG1 SET avg1=@P WHERE Cname='Pascal语言'
  195. COMMIT TRANSACTION TRANS;
  196. END

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

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

 图4:主界面

对应C#实现如下: 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace SchoolManage
  11. {
  12. public partial class FormLogin : Form
  13. {
  14. public FormLogin()
  15. {
  16. InitializeComponent();
  17. }
  18. private void buttonStuLogin_Click(object sender, EventArgs e)
  19. {
  20. FormStuLogin FormStuLogin = new FormStuLogin(); //学生登录窗体
  21. FormStuLogin.Show();//跳转至学生登录窗体
  22. this.Hide(); //隐藏原窗体
  23. }
  24. private void buttonManagerLogin_Click(object sender, EventArgs e)
  25. {
  26. FormManagerLogin FormManagerLogin = new FormManagerLogin(); //管理员登录窗体
  27. FormManagerLogin.Show();//跳转至管理员登录窗体
  28. this.Hide();
  29. }
  30. private void FormLogin_FormClosing_1(object sender, FormClosingEventArgs e)
  31. {
  32. }
  33. private void FormLogin_FormClosed(object sender, FormClosedEventArgs e)
  34. {
  35. while (MessageBox.Show("即将退出系统,您确认退出吗?", "提示!", MessageBoxButtons.YesNo)== DialogResult.Yes)
  36. {
  37. System.Environment.Exit(System.Environment.ExitCode);
  38. }
  39. }
  40. }
  41. }

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

图5:学生登录页面

图6:学生登录成功

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

图7:学生登录失败

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace SchoolManage
  13. {
  14. public partial class FormStuLogin : Form
  15. {
  16. public FormStuLogin()
  17. {
  18. InitializeComponent();
  19. }
  20. public string code;
  21. private string EncryptWithMD5(string source)//MD5加密
  22. {
  23. byte[] sor = Encoding.UTF8.GetBytes(source);
  24. MD5 md5 = MD5.Create();
  25. byte[] result = md5.ComputeHash(sor);
  26. StringBuilder strbul = new StringBuilder(40);
  27. for (int i = 0; i < result.Length; i++)
  28. {
  29. strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
  30. }
  31. return strbul.ToString();
  32. }
  33. private void buttonLogin_Click(object sender, EventArgs e)
  34. {
  35. string username = tBUserName.Text.Trim(); //取出账号
  36. string password = EncryptWithMD5(tBPassword.Text.Trim()); //取出密码并加密
  37. string myConnString = "Data Source=.;Initial Catalog=MySchool;Persist Security Info=True;User ID=sa;Password=yourpassword";
  38. SqlConnection sqlConnection = new SqlConnection(myConnString); //实例化连接对象
  39. sqlConnection.Open();
  40. string sql = "select ID,PassWord from StudentUser where ID = '" + username + "' and PassWord = '" + password + "'"; //编写SQL命令
  41. SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
  42. SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();//读取数据
  43. if (sqlDataReader.HasRows && tBVerifyCode.Text == code)//验证是否有该用户及密码且验证码正确
  44. {
  45. MessageBox.Show("欢迎使用!"); //登录成功
  46. StuMain stuMain = new StuMain();
  47. stuMain.GetId(username);
  48. stuMain.Show();//显示下一界面
  49. this.Hide();
  50. }
  51. else if (sqlDataReader.HasRows && tBVerifyCode.Text != code)
  52. {
  53. MessageBox.Show("验证码错误,登录失败!");
  54. return;
  55. }
  56. else
  57. {
  58. MessageBox.Show("账号密码有误,登录失败!");
  59. return;
  60. }
  61. sqlDataReader.Close();
  62. sql = "insert into SysLog1 values ( '" + username + "' , 'Student','" + DateTime.Now + "' , '" + "Login" + "')"; //编写SQL命令,把登陆信息存入登录日志
  63. SqlCommand sqlCommand1 = new SqlCommand(sql, sqlConnection);
  64. sqlCommand1.ExecuteNonQuery();
  65. sqlConnection.Close();
  66. }
  67. private void buttonReg_Click(object sender, EventArgs e)
  68. {
  69. this.Hide();
  70. RegisterForm registerForm = new RegisterForm();
  71. registerForm.Show();
  72. }
  73. private void FormStuLogin_Load_1(object sender, EventArgs e)
  74. {
  75. //随机实例化
  76. Random ran = new Random();
  77. int number;
  78. char code1;
  79. //取五个数
  80. for (int i = 0; i < 5; i++)
  81. {
  82. number = ran.Next();
  83. if (number % 2 == 0)
  84. code1 = (char)('0' + (char)(number % 10));
  85. else
  86. code1 = (char)('A' + (char)(number % 26)); //转化为字符
  87. this.code += code1.ToString();
  88. }
  89. lbVerifyCode.Text = code;
  90. }
  91. private void lbVerifyCode_Click(object sender, EventArgs e)
  92. {
  93. }
  94. private void buttonCancel_Click_1(object sender, EventArgs e)
  95. {
  96. this.Close();
  97. }
  98. private void FormStuLogin_FormClosing_1(object sender, FormClosingEventArgs e)
  99. {
  100. FormLogin m2 = new FormLogin();
  101. m2.Show();
  102. }
  103. private void lbPassword_Click(object sender, EventArgs e)
  104. {
  105. }
  106. }
  107. }

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

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

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

对应C#实现如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace SchoolManage
  13. {
  14. public partial class StuMain : Form
  15. {
  16. public StuMain()
  17. {
  18. InitializeComponent();
  19. }
  20. private void buttonExit_Click(object sender, EventArgs e)
  21. {
  22. this.Close();
  23. }
  24. string Id;
  25. public void GetId(string id)//上个窗体调用此函数,将id值传过来
  26. {
  27. lbStuId.Text = id;
  28. lbStuId.Refresh();
  29. Id = id;
  30. }
  31. private void StuMain_Load_1(object sender, EventArgs e)
  32. {
  33. // TODO: 这行代码将数据加载到表“mySchoolDataSet.Course”中。您可以根据需要移动或移除它。
  34. this.courseTableAdapter.Fill(this.mySchoolDataSet.Course);
  35. string name, sex, dept, tel;
  36. int age;
  37. string connString = "Data Source=.;Initial Catalog=MySchool;Persist Security Info=True;User ID=sa;Password=yourpassword";//数据库连接字符串
  38. SqlConnection conn = new SqlConnection(connString);//创建connection对象
  39. conn.Open();//打开数据库
  40. //创建数据库命令
  41. SqlCommand cmd = conn.CreateCommand();
  42. //创建查询语句
  43. cmd.CommandText = "select * from Student where Sno = '" + Id + "';select * from StudentUser where ID = '" + Id + "'";
  44. //从数据库中读取数据流存入reader中
  45. SqlDataReader reader = cmd.ExecuteReader();
  46. //从reader中读取下一行数据,如果没有数据,reader.Read()返回flase
  47. while (reader.Read())
  48. {
  49. name = reader.GetString(reader.GetOrdinal("Sname"));
  50. sex = reader.GetString(reader.GetOrdinal("Ssex"));
  51. age = reader.GetInt16(reader.GetOrdinal("Sage"));
  52. dept = reader.GetString(reader.GetOrdinal("Sdept"));
  53. lbStuName.Text = name;
  54. lbStuName.Refresh();
  55. lbStuSex.Text = sex;
  56. lbStuAge.Text = age.ToString();
  57. lbStuDept.Text = dept;
  58. break;
  59. }
  60. reader.NextResult();//执行下一句操作
  61. //从reader中读取下一行数据,如果没有数据,reader.Read()返回flase
  62. while (reader.Read())
  63. {
  64. tel = reader.GetString(reader.GetOrdinal("UserMobile"));
  65. lbPhone.Text = tel;
  66. break;
  67. }
  68. reader.Close();
  69. SqlDataAdapter dap = new SqlDataAdapter("select * from SC where Sno='" + Id + "'", conn);//查询
  70. DataSet ds = new DataSet();//创建DataSet对象
  71. dap.Fill(ds);//填充DataSet数据集
  72. dataGridView1.DataSource = ds.Tables[0].DefaultView;//显示查询后的数据
  73. conn.Close();
  74. int i = 0;
  75. int x = 0, h = 0;
  76. int a;
  77. for (; i < ds.Tables[0].Rows.Count; i++)//读取DataSet中的指定数据
  78. {
  79. x += int.Parse(ds.Tables[0].Rows[i][2].ToString());
  80. if (int.Parse(ds.Tables[0].Rows[i][2].ToString()) > 59)
  81. h++;
  82. }
  83. if (i == 0) a = 0;
  84. else a = x / i;
  85. lbPassnum.Text = h.ToString();
  86. lbAvgSco.Text = a.ToString();
  87. }
  88. private void StuMain_FormClosing_1(object sender, FormClosingEventArgs e)
  89. {
  90. FormLogin m2 = new FormLogin();
  91. m2.Show();
  92. }
  93. private void StuMain_FormClosed(object sender, FormClosedEventArgs e)
  94. {
  95. }
  96. }
  97. }

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

图10:注册页面

图11:注册成功

图12:注册失败

对应C#实现如下:  

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Security.Cryptography;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace SchoolManage
  15. {
  16. public partial class RegisterForm : Form
  17. {
  18. public RegisterForm()
  19. {
  20. InitializeComponent();
  21. }
  22. private void buttonRegister_Click(object sender, EventArgs e)
  23. {
  24. if (tBStuId.Text.Trim() != "" && tBPassword.Text.Trim() != "" && cBSex.Text.Trim() != "" && tBPhoneNum.Text.Trim() != "")
  25. {//确认一些信息输入非空
  26. try
  27. {
  28. string connString = "Data Source=.;Initial Catalog=MySchool;Persist Security Info=True;User ID=sa;Password=yourpassword";//数据库连接字符串
  29. SqlConnection connection = new SqlConnection(connString);//创建connection对象
  30. string username = tBStuId.Text.Trim(); //取出账号
  31. string sex = cBSex.Text.Trim(); //取出性别
  32. connection.Open();
  33. string sql1 = "select Sno,Ssex from Student where Sno = '" + username + "' and Ssex = '" + sex + "'"; //编写SQL命令,查找学生信息中是否有该用户
  34. SqlCommand sqlCommand1 = new SqlCommand(sql1, connection);//确认是否有该学员
  35. SqlDataReader sqlDataReader1 = sqlCommand1.ExecuteReader();
  36. bool a = sqlDataReader1.HasRows;
  37. sqlDataReader1.Close();
  38. if (a)
  39. {
  40. string sql2 = "select ID from StudentUser where ID = '" + username + "'"; //编写SQL命令
  41. SqlCommand sqlCommand2 = new SqlCommand(sql2, connection);//确认是否已经注册过
  42. SqlDataReader sqlDataReader2 = sqlCommand2.ExecuteReader();
  43. if (!sqlDataReader2.HasRows)
  44. {
  45. string sql3 = "insert into StudentUser (ID, PassWord ,Sex, UserMobile, Birthday) " +
  46. "values (@userid, @userpassword,@sex,@usermobile,@Birthday)";
  47. SqlCommand command = new SqlCommand(sql3, connection);
  48. SqlParameter sqlParameter = new SqlParameter("@userid", tBStuId.Text);
  49. command.Parameters.Add(sqlParameter);
  50. sqlParameter = new SqlParameter("@userpassword", EncryptWithMD5(tBPassword.Text));
  51. command.Parameters.Add(sqlParameter);
  52. sqlParameter = new SqlParameter("@sex", cBSex.Text);
  53. command.Parameters.Add(sqlParameter);
  54. sqlParameter = new SqlParameter("@usermobile", tBPhoneNum.Text);
  55. command.Parameters.Add(sqlParameter);
  56. sqlParameter = new SqlParameter("@Birthday", dateTimePicker1.Value);
  57. command.Parameters.Add(sqlParameter);
  58. sqlDataReader2.Close();
  59. //打开数据库连接
  60. command.ExecuteNonQuery();
  61. connection.Close();
  62. MessageBox.Show("注册成功");
  63. }
  64. else
  65. {
  66. MessageBox.Show("该用户已注册。");
  67. }
  68. }
  69. else
  70. {
  71. MessageBox.Show("我校无该学员");
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. MessageBox.Show(ex.Message);
  77. }
  78. this.Close();
  79. }
  80. else
  81. {
  82. MessageBox.Show("请将信息填写完整!");
  83. }
  84. }
  85. public Byte[] mybyte = new byte[0];
  86. public static string EncryptWithMD5(string source)
  87. {
  88. byte[] sor = Encoding.UTF8.GetBytes(source);
  89. MD5 md5 = MD5.Create();
  90. byte[] result = md5.ComputeHash(sor);
  91. StringBuilder strbul = new StringBuilder(40);
  92. for (int i = 0; i < result.Length; i++)
  93. {
  94. strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
  95. }
  96. return strbul.ToString();
  97. }
  98. private void tBPassword_Leave(object sender, EventArgs e)//校验密码格式
  99. {
  100. if (tBStuId.Text.Trim() != "")
  101. {
  102. //使用regex(正则表达式)进行格式设置 至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符。
  103. Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");
  104. if (regex.IsMatch(tBPassword.Text))//判断格式是否符合要求
  105. {
  106. //MessageBox.Show("输入密码格式正确!");
  107. }
  108. else
  109. {
  110. MessageBox.Show("至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符!");
  111. tBPassword.Focus();
  112. }
  113. }
  114. else
  115. {
  116. MessageBox.Show("请填写密码!");
  117. }
  118. }
  119. private void RegisterForm_FormClosing(object sender, FormClosingEventArgs e)
  120. {
  121. FormStuLogin m2 = new FormStuLogin();
  122. m2.Show();
  123. }
  124. private void buttonCancel_Click_1(object sender, EventArgs e)
  125. {
  126. this.Close();
  127. }
  128. }
  129. }

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

图13:管理员登录页面

图14:管理员登录成功

对应C#实现如下:  

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace SchoolManage
  13. {
  14. public partial class FormManagerLogin : Form
  15. {
  16. public FormManagerLogin()
  17. {
  18. InitializeComponent();
  19. }
  20. public string code;
  21. private void buttonLogin_Click(object sender, EventArgs e)
  22. {
  23. string username = tBWorknum.Text.Trim(); //取出账号
  24. string password = EncryptWithMD5(tBPassword.Text.Trim()); //取出密码并加密
  25. string myConnString = "Data Source=.;Initial Catalog=MySchool;Persist Security Info=True;User ID=sa;Password=yourpassword";
  26. SqlConnection sqlConnection = new SqlConnection(myConnString); //实例化连接对象
  27. sqlConnection.Open();
  28. string sql = "select ID,PassWord from Administrator where ID = '" + username + "' and PassWord = '" + password + "'"; //编写SQL命令
  29. SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
  30. SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
  31. if (sqlDataReader.HasRows && tBVerifyCode.Text == code)
  32. {
  33. MessageBox.Show("欢迎使用!"); //登录成功
  34. ManagerMain managerMain = new ManagerMain();
  35. managerMain.Show();
  36. this.Hide();
  37. }
  38. else if (sqlDataReader.HasRows && tBVerifyCode.Text != code)
  39. {
  40. MessageBox.Show("验证码错误,登录失败!");
  41. return;
  42. }
  43. else
  44. {
  45. MessageBox.Show("账号密码有误,登录失败!");
  46. return;
  47. }
  48. sqlDataReader.Close();
  49. sql = "insert into SysLog1 values ( '" + username + "' , 'Administrator','" + DateTime.Now + "' , '" + "Login" + "')"; //编写SQL命令
  50. SqlCommand sqlCommand1 = new SqlCommand(sql, sqlConnection);
  51. sqlCommand1.ExecuteNonQuery();
  52. sqlConnection.Close();
  53. }
  54. private string EncryptWithMD5(string source)
  55. {
  56. byte[] sor = Encoding.UTF8.GetBytes(source);
  57. MD5 md5 = MD5.Create();
  58. byte[] result = md5.ComputeHash(sor);
  59. StringBuilder strbul = new StringBuilder(40);
  60. for (int i = 0; i < result.Length; i++)
  61. {
  62. strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
  63. }
  64. return strbul.ToString();
  65. }
  66. private void FormManagerLogin_Load_1(object sender, EventArgs e)
  67. {
  68. //随机实例化
  69. Random ran = new Random();
  70. int number;
  71. char code1;
  72. //取五个数
  73. for (int i = 0; i < 5; i++)
  74. {
  75. number = ran.Next();
  76. if (number % 2 == 0)
  77. code1 = (char)('0' + (char)(number % 10));
  78. else
  79. code1 = (char)('A' + (char)(number % 26)); //转化为字符
  80. this.code += code1.ToString();
  81. }
  82. lbVerifyCode.Text = code;
  83. }
  84. private void FormManagerLogin_FormClosing_1(object sender, FormClosingEventArgs e)
  85. {
  86. FormLogin m2 = new FormLogin();
  87. m2.Show();
  88. }
  89. private void buttonCancel_Click_1(object sender, EventArgs e)
  90. {
  91. this.Close();
  92. }
  93. }
  94. }

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

图15:管理员主页

对应C#实现如下:  

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace SchoolManage
  11. {
  12. public partial class ManagerMain : Form
  13. {
  14. public ManagerMain()
  15. {
  16. InitializeComponent();
  17. }
  18. private void buttonLog_Click(object sender, EventArgs e)
  19. {
  20. this.Hide();
  21. LogMain m = new LogMain();
  22. m.Show();
  23. }
  24. private void buttonStu_Click(object sender, EventArgs e)
  25. {
  26. this.Hide();
  27. StuMessageMain m = new StuMessageMain();
  28. m.Show();
  29. }
  30. private void buttonCourse_Click(object sender, EventArgs e)
  31. {
  32. this.Hide();
  33. CourseMessageMain m = new CourseMessageMain();
  34. m.Show();
  35. }
  36. private void buttonGrade_Click(object sender, EventArgs e)
  37. {
  38. this.Hide();
  39. StuGradeMain m = new StuGradeMain();
  40. m.Show();
  41. }
  42. private void buttonExit_Click(object sender, EventArgs e)
  43. {
  44. this.Close();
  45. }
  46. private void ManagerMain_FormClosed(object sender, FormClosedEventArgs e)
  47. {
  48. //Application.Exit();
  49. }
  50. private void ManagerMain_FormClosing(object sender, FormClosingEventArgs e)
  51. {
  52. FormLogin m2 = new FormLogin();
  53. m2.Show();
  54. }
  55. private void ManagerMain_Load(object sender, EventArgs e)
  56. {
  57. }
  58. }
  59. }

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

图16:登录日志页面

对应C#实现如下:  

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace SchoolManage
  11. {
  12. public partial class LogMain : Form
  13. {
  14. public LogMain()
  15. {
  16. InitializeComponent();
  17. }
  18. private void LogMain_Load(object sender, EventArgs e)
  19. {
  20. // TODO: 这行代码将数据加载到表“mySchoolDataSet1.SysLog1”中。您可以根据需要移动或移除它。
  21. this.sysLog1TableAdapter.Fill(this.mySchoolDataSet1.SysLog1);
  22. }
  23. private void buttonReturn_Click(object sender, EventArgs e)
  24. {
  25. this.Close();
  26. }
  27. private void LogMain_FormClosing(object sender, FormClosingEventArgs e)
  28. {
  29. ManagerMain m2 = new ManagerMain();
  30. m2.Show();
  31. }
  32. }
  33. }

(8)学生信息页面:为管理员提供学生信息,且管理员可按需进行增、删、改、查操作。

图17:学生信息页面

对应C#实现如下:  

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace SchoolManage
  12. {
  13. public partial class StuMessageMain : Form
  14. {
  15. public StuMessageMain()
  16. {
  17. InitializeComponent();
  18. }
  19. private void StuMessageMain_Load(object sender, EventArgs e)
  20. {
  21. // TODO: 这行代码将数据加载到表“mySchoolDataSet2.Student”中。您可以根据需要移动或移除它。
  22. this.studentTableAdapter.Fill(this.mySchoolDataSet2.Student);
  23. }
  24. private void buttonReturn_Click(object sender, EventArgs e)
  25. {
  26. this.Close();
  27. }
  28. private void buttonAdd_Click(object sender, EventArgs e)
  29. {
  30. String StuID = tBStuId.Text.Trim();//读取文本框的值
  31. String StuName = tBStuName.Text.Trim();
  32. String StuSex = tBStuSex.Text.Trim();
  33. String StuSdept = tBStuDept.Text.Trim();
  34. int StuAge = int.Parse(tBStuAge.Text.Trim());
  35. SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
  36. try
  37. {
  38. con.Open();
  39. string insertStr = "INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) " +
  40. "VALUES ('" + StuID + "','" + StuName + "','" + StuSex + "','" + StuSdept + "'," + StuAge + ")";
  41. SqlCommand cmd = new SqlCommand(insertStr, con);//通过sql语句对表添加数值
  42. cmd.ExecuteNonQuery();
  43. }
  44. catch
  45. {
  46. MessageBox.Show("输入数据违反要求!");
  47. }
  48. finally
  49. {
  50. con.Dispose();
  51. tBStuId.Text = "";
  52. tBStuName.Text = "";
  53. tBStuSex.Text = "";
  54. tBStuAge.Text = "";
  55. tBStuDept.Text = "";
  56. }
  57. this.studentTableAdapter.Fill(this.mySchoolDataSet2.Student);//刷新表
  58. }
  59. private void buttonDelete_Click(object sender, EventArgs e)
  60. {
  61. SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
  62. try
  63. {
  64. con.Open();
  65. string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
  66. string delete_by_id = "delete from Student where Sno=" + select_id;//sql删除语句
  67. SqlCommand cmd = new SqlCommand(delete_by_id, con);
  68. cmd.ExecuteNonQuery();
  69. }
  70. catch
  71. {
  72. MessageBox.Show("请正确选择行!");
  73. }
  74. finally
  75. {
  76. con.Dispose();
  77. }
  78. this.studentTableAdapter.Fill(this.mySchoolDataSet2.Student);
  79. }
  80. private void buttonEdit_Click(object sender, EventArgs e)
  81. {
  82. String StuID = tBStuId.Text.Trim();
  83. String StuName = tBStuName.Text.Trim();
  84. String StuSex = tBStuSex.Text.Trim();
  85. String StuSdept = tBStuDept.Text.Trim();
  86. int StuAge = 0;
  87. if (tBStuAge.Text != "")
  88. {
  89. StuAge = int.Parse(tBStuAge.Text.Trim());
  90. }
  91. SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
  92. try
  93. {
  94. con.Open();
  95. if(StuName != "")
  96. {
  97. string insertStr = "UPDATE Student SET Sname = '" + StuName + "' WHERE Sno = '" + StuID + "'";
  98. SqlCommand cmd = new SqlCommand(insertStr, con);
  99. cmd.ExecuteNonQuery();
  100. }
  101. if (StuSex != "")
  102. {
  103. string insertStr = "UPDATE Student SET Ssex = '" + StuSex + "' WHERE Sno = '" + StuID + "'";
  104. SqlCommand cmd = new SqlCommand(insertStr, con);
  105. cmd.ExecuteNonQuery();
  106. }
  107. if (StuSdept != "")
  108. {
  109. string insertStr = "UPDATE Student SET Sdept = '" + StuSdept + "' WHERE Sno = '" + StuID + "'";
  110. SqlCommand cmd = new SqlCommand(insertStr, con);
  111. cmd.ExecuteNonQuery();
  112. }
  113. if (tBStuAge.Text != "")
  114. {
  115. string insertStr = "UPDATE Student SET Sage = '" + StuAge + "' WHERE Sno = '" + StuID + "'";
  116. SqlCommand cmd = new SqlCommand(insertStr, con);
  117. cmd.ExecuteNonQuery();
  118. }
  119. }
  120. catch
  121. {
  122. MessageBox.Show("输入数据违反要求!");
  123. }
  124. finally
  125. {
  126. con.Dispose();
  127. tBStuId.Text = "";
  128. tBStuName.Text = "";
  129. tBStuSex.Text = "";
  130. tBStuAge.Text = "";
  131. tBStuDept.Text = "";
  132. }
  133. this.studentTableAdapter.Fill(this.mySchoolDataSet2.Student);
  134. }
  135. private void buttonSearch_Click(object sender, EventArgs e)
  136. {
  137. String StuID = tBStuId.Text.Trim();
  138. String conn = "Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword";
  139. SqlConnection sqlConnection = new SqlConnection(conn); //实例化连接对象
  140. try
  141. {
  142. sqlConnection.Open();
  143. String select_by_id = "select * from Student where Sno='" + StuID + "'";
  144. SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
  145. SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
  146. BindingSource bindingSource = new BindingSource();
  147. bindingSource.DataSource = sqlDataReader;
  148. dataGridView1.DataSource = bindingSource;
  149. }
  150. catch
  151. {
  152. MessageBox.Show("查询语句有误,请认真检查SQL语句!");
  153. }
  154. finally
  155. {
  156. sqlConnection.Close();
  157. tBStuId.Text = "";
  158. }
  159. }
  160. private void StuMessageMain_FormClosing(object sender, FormClosingEventArgs e)
  161. {
  162. ManagerMain m2 = new ManagerMain();
  163. m2.Show();
  164. }
  165. }
  166. }

(9)课程信息页面:为管理员提供课程信息,且管理员可按需进行增、删、改、查操作。

图18:课程信息页面

对应C#实现如下:  

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace SchoolManage
  12. {
  13. public partial class CourseMessageMain : Form
  14. {
  15. public CourseMessageMain()
  16. {
  17. InitializeComponent();
  18. }
  19. private void CourseMessageMain_Load(object sender, EventArgs e)
  20. {
  21. // TODO: 这行代码将数据加载到表“mySchoolDataSet3.Course”中。您可以根据需要移动或移除它。
  22. this.courseTableAdapter.Fill(this.mySchoolDataSet3.Course);
  23. }
  24. private void buttonAdd_Click(object sender, EventArgs e)
  25. {
  26. String cno = tBCno.Text.Trim();
  27. String cn = tBCname.Text.Trim();
  28. String cpo = tBCpno.Text.Trim();
  29. int cd = 0;
  30. if(tBCcredit.Text != "")
  31. {
  32. cd = int.Parse(tBCcredit.Text.Trim());
  33. }
  34. if (cpo == "")
  35. {
  36. cpo = "NULL";
  37. }
  38. SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
  39. try
  40. {
  41. if(cno == "" || cn =="" || cd == 0)
  42. {
  43. MessageBox.Show("输入数据违反要求!");
  44. }
  45. else
  46. {
  47. con.Open();
  48. string insertStr = "INSERT INTO Course (Cno,Cname,Cpno,Ccredit) " +
  49. "VALUES ('" + cno + "','" + cn + "','" + cpo + "','" + cd + "')";
  50. SqlCommand cmd = new SqlCommand(insertStr, con);
  51. cmd.ExecuteNonQuery();
  52. }
  53. }
  54. catch
  55. {
  56. }
  57. finally
  58. {
  59. con.Dispose();
  60. tBCno.Text = "";
  61. tBCname.Text = "";
  62. tBCpno.Text = "";
  63. tBCcredit.Text = "";
  64. }
  65. this.courseTableAdapter.Fill(this.mySchoolDataSet3.Course);
  66. }
  67. private void buttonDelete_Click(object sender, EventArgs e)
  68. {
  69. SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
  70. try
  71. {
  72. con.Open();
  73. string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
  74. string delete_by_id = "delete from Course where Cno=" + select_id;//sql删除语句
  75. SqlCommand cmd = new SqlCommand(delete_by_id, con);
  76. cmd.ExecuteNonQuery();
  77. }
  78. catch
  79. {
  80. MessageBox.Show("请正确选择行!");
  81. }
  82. finally
  83. {
  84. con.Dispose();
  85. }
  86. this.courseTableAdapter.Fill(this.mySchoolDataSet3.Course);
  87. }
  88. private void buttonEdit_Click(object sender, EventArgs e)
  89. {
  90. String cno = tBCno.Text.Trim();
  91. String cn = tBCname.Text.Trim();
  92. String cpo = tBCpno.Text.Trim();
  93. int cd = 0;
  94. if(tBCcredit.Text != "")
  95. {
  96. cd = int.Parse(tBCcredit.Text.Trim());
  97. }
  98. if (cpo == "")
  99. {
  100. cpo = "NULL";
  101. }
  102. SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
  103. con.Open();
  104. if(cn != "")
  105. {
  106. string insertStr = "UPDATE Course SET Cname = '" + cn + "' WHERE Cno = '" + cno + "'";
  107. SqlCommand cmd = new SqlCommand(insertStr, con);
  108. cmd.ExecuteNonQuery();
  109. }
  110. if (cpo != "")
  111. {
  112. string insertStr = "UPDATE Course SET Cpno = '" + cpo + "' WHERE Cno = '" + cno + "'";
  113. SqlCommand cmd = new SqlCommand(insertStr, con);
  114. cmd.ExecuteNonQuery();
  115. }
  116. if (tBCcredit.Text != "")
  117. {
  118. string insertStr = "UPDATE Course SET Ccredit = '" + cd + "' WHERE Cno = '" + cno + "'";
  119. SqlCommand cmd = new SqlCommand(insertStr, con);
  120. cmd.ExecuteNonQuery();
  121. }
  122. if(cno == "")
  123. {
  124. MessageBox.Show("输入数据违反要求!");
  125. }
  126. con.Dispose();
  127. tBCno.Text = "";
  128. tBCname.Text = "";
  129. tBCpno.Text = "";
  130. tBCcredit.Text = "";
  131. this.courseTableAdapter.Fill(this.mySchoolDataSet3.Course);
  132. }
  133. private void buttonSearch_Click(object sender, EventArgs e)
  134. {
  135. String cno = tBCno.Text.Trim();
  136. String conn = "Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword";
  137. SqlConnection sqlConnection = new SqlConnection(conn); //实例化连接对象
  138. try
  139. {
  140. sqlConnection.Open();
  141. String select_by_id = "select * from Course where Cno='" + cno + "'";
  142. SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
  143. SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
  144. BindingSource bindingSource = new BindingSource();
  145. bindingSource.DataSource = sqlDataReader;
  146. dataGridView1.DataSource = bindingSource;
  147. }
  148. catch
  149. {
  150. MessageBox.Show("查询语句有误,请认真检查SQL语句!");
  151. }
  152. finally
  153. {
  154. sqlConnection.Close();
  155. tBCno.Text = "";
  156. }
  157. }
  158. private void buttonReturn_Click(object sender, EventArgs e)
  159. {
  160. this.Close();
  161. }
  162. private void CourseMessageMain_FormClosing(object sender, FormClosingEventArgs e)
  163. {
  164. ManagerMain m2 = new ManagerMain();
  165. m2.Show();
  166. }
  167. }
  168. }

(10)学生成绩信息页面:为管理员提供学生成绩信息,且管理员可按需进行增、删、改、查操作。

图19:学生成绩信息页面

对应C#实现如下:  

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace SchoolManage
  12. {
  13. public partial class StuGradeMain : Form
  14. {
  15. public StuGradeMain()
  16. {
  17. InitializeComponent();
  18. }
  19. private void StuGradeMain_Load(object sender, EventArgs e)
  20. {
  21. // TODO: 这行代码将数据加载到表“mySchoolDataSet4.SC”中。您可以根据需要移动或移除它。
  22. this.sCTableAdapter.Fill(this.mySchoolDataSet4.SC);
  23. }
  24. private void buttonAdd_Click(object sender, EventArgs e)
  25. {
  26. String sno = tBSId.Text.Trim();
  27. String cno = tBCno.Text.Trim();
  28. int gra = int.Parse(tBGrade.Text.Trim());
  29. SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
  30. try
  31. {
  32. con.Open();
  33. string insertStr = "INSERT INTO SC (Sno,Cno,Grade) " +
  34. "VALUES ('" + sno + "','" + cno + "','" + gra + "')";
  35. SqlCommand cmd = new SqlCommand(insertStr, con);
  36. cmd.ExecuteNonQuery();
  37. }
  38. catch
  39. {
  40. MessageBox.Show("输入数据违反要求!");
  41. }
  42. finally
  43. {
  44. con.Dispose();
  45. tBSId.Text = "";
  46. tBCno.Text = "";
  47. tBGrade.Text = "";
  48. }
  49. this.sCTableAdapter.Fill(this.mySchoolDataSet4.SC);
  50. }
  51. private void buttonDelete_Click(object sender, EventArgs e)
  52. {
  53. SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
  54. try
  55. {
  56. con.Open();
  57. string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
  58. string select_cid = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();//选择的当前行第二列的值,也就是CID
  59. string deletesql = "delete from SC where Sno='" + select_id + "' AND Cno='" + select_cid + "'";//sql删除语句
  60. SqlCommand cmd = new SqlCommand(deletesql, con);
  61. cmd.ExecuteNonQuery();
  62. }
  63. catch
  64. {
  65. MessageBox.Show("请正确选择行!");
  66. }
  67. finally
  68. {
  69. con.Dispose();
  70. }
  71. this.sCTableAdapter.Fill(this.mySchoolDataSet4.SC);
  72. }
  73. private void buttonEdit_Click(object sender, EventArgs e)
  74. {
  75. String sno = tBSId.Text.Trim();
  76. String cno = tBCno.Text.Trim();
  77. int gra = int.Parse(tBGrade.Text.Trim());
  78. SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
  79. try
  80. {
  81. con.Open();
  82. string insertStr = "UPDATE SC SET Grade='" + gra + "' WHERE Sno = '" + sno + "' AND Cno = '" + cno + "'";
  83. SqlCommand cmd = new SqlCommand(insertStr, con);
  84. cmd.ExecuteNonQuery();
  85. }
  86. catch
  87. {
  88. MessageBox.Show("输入数据违反要求!");
  89. }
  90. finally
  91. {
  92. con.Dispose();
  93. tBSId.Text = "";
  94. tBCno.Text = "";
  95. tBGrade.Text = "";
  96. }
  97. this.sCTableAdapter.Fill(this.mySchoolDataSet4.SC);
  98. }
  99. private void buttonSearch_Click(object sender, EventArgs e)
  100. {
  101. String sno = tBSId.Text.Trim();
  102. String cno = tBCno.Text.Trim();
  103. String conn = "Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword";
  104. SqlConnection sqlConnection = new SqlConnection(conn); //实例化连接对象
  105. try
  106. {
  107. String selectsql = "select * from SC where Sno='" + sno + "'";
  108. sqlConnection.Open();
  109. if(sno != "" && cno == "")
  110. {
  111. selectsql = "select * from SC where Sno='" + sno + "'";
  112. }
  113. if (sno != "" && cno != "")
  114. {
  115. selectsql = "select * from SC where Sno='" + sno + "' AND Cno='" + cno + "'";
  116. }
  117. SqlCommand sqlCommand = new SqlCommand(selectsql, sqlConnection);
  118. SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
  119. BindingSource bindingSource = new BindingSource();
  120. bindingSource.DataSource = sqlDataReader;
  121. dataGridView1.DataSource = bindingSource;
  122. }
  123. catch
  124. {
  125. MessageBox.Show("查询语句有误,请认真检查SQL语句!");
  126. }
  127. finally
  128. {
  129. sqlConnection.Close();
  130. tBSId.Text = "";
  131. tBCno.Text = "";
  132. }
  133. }
  134. private void buttonMes_Click(object sender, EventArgs e)
  135. {
  136. string connString = "Data Source=.;Initial Catalog=MySchool;Persist Security Info=True;User ID=sa;Password=yourpassword";//数据库连接字符串
  137. SqlConnection connection = new SqlConnection(connString);//创建connection对象
  138. string sql1 = "EXEC COURSE_AVG1";//编写SQL命令
  139. SqlCommand sqlCommand1 = new SqlCommand(sql1, connection);
  140. connection.Open();
  141. sqlCommand1.ExecuteNonQuery();
  142. connection.Close();
  143. this.Hide();
  144. StatisiticMain m2 = new StatisiticMain();
  145. m2.Show();
  146. }
  147. private void buttonReturn_Click(object sender, EventArgs e)
  148. {
  149. this.Close();
  150. }
  151. private void StuGradeMain_FormClosing(object sender, FormClosingEventArgs e)
  152. {
  153. ManagerMain m2 = new ManagerMain();
  154. m2.Show();
  155. }
  156. }
  157. }

(11)成绩统计页面:为管理员提供统计信息。

图20:成绩统计页面

对应C#实现如下:  

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace SchoolManage
  12. {
  13. public partial class StatisiticMain : Form
  14. {
  15. public StatisiticMain()
  16. {
  17. InitializeComponent();
  18. }
  19. private void StatisiticMain_Load(object sender, EventArgs e)
  20. {
  21. // TODO: 这行代码将数据加载到表“mySchoolDataSet6.AVG1”中。您可以根据需要移动或移除它。
  22. this.aVG1TableAdapter.Fill(this.mySchoolDataSet6.AVG1);
  23. }
  24. private void buttonReturn_Click(object sender, EventArgs e)
  25. {
  26. this.Close();
  27. }
  28. private void StatisiticMain_FormClosing(object sender, FormClosingEventArgs e)
  29. {
  30. StuGradeMain m2 = new StuGradeMain();
  31. m2.Show();
  32. }
  33. }
  34. }

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

(注:以上页面中的“返回”、“退出”、“取消”、“×”选项均使得页面从当前界面返回到上一界面或上一级界面,并不使程序退出,仅主界面的“×”号可退出系统。)

图21:退出系统

以上就是本次数据库课程设计的全部内容啦,欢迎小伙伴们交流心得,批评指正。

转载请注明出处,未经同意禁止转载!

原文链接:https://blog.csdn.net/qq_47403671/article/details/125084509



所属网站分类: 技术文章 > 博客

作者:php程序员

链接:http://www.phpheidong.com/blog/article/546099/34a46c6878ec32bc0c5a/

来源:php黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

12 0
收藏该文
已收藏

评论内容:(最多支持255个字符)