VS&C#&数据库大作业-学生信息管理系统
本文共34866 字(代码占比较大),预计阅读时长10mis左右
代码下载 视频链接 见最后
指导老师: HBU_David
开发工具: Microsoft Visual Studio & Microsoft SQL Server
语言: C# & SQL
功能介绍:
—— 主窗口是登录界面,此外可大致分为3个模块:注册账号,忘记密码,选择身份进行登录。管理员可执行4个操作:查看登陆日志;学生信息管理;课程管理;成绩管理。用户两个窗口可执行3个操作:查询成绩&添加课程;查看开设课程。
VS中控件:
无操作文字:Lable
输入框:textBox
按钮:Button
数据表格:dataGridView
组合框:comboBox
日期选择:dateTimePicker
窗体背景:点击窗体边缘 更改属性->BackgroundImage
更改图标样式:(事先准备图标)
SQL准备、窗口界面以及代码目录
SQL建表准备
进行操作之前需要把每张表准备好,其实不必要一下子全想出来,做着做着就知道自己需要哪张了哈哈哈!
这里三张基本表无需多说,其余
Managetable: 用来存储管理员的信息(为了登录,修改密码…使用)
Usertable: 用来存储用户的信息(为了登录,修改密码…使用)
SchLog: 用来存放登录日志
建表代码
三张基本表(Student,Course,SC)的建表代码
这是老师写的三张基本表的建表代码 我的是在上面的基础上又加了点 因为“年少无知”忘记保存了…
--Edit by HBU_David @ HeBei University 2020.3.6
DROP TABLE IF EXISTS SC
DROP TABLE IF EXISTS Student
DROP TABLE IF EXISTS Course
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,
FOREIGN KEY (Cpno) REFERENCES Course(Cno) /* 表级完整性约束条件, Cpno是外码,被参照表是自身*/
);
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 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);
INSERT INTO SC VALUES('201215123','1',86);
INSERT INTO SC VALUES('201215123','2',88);
INSERT INTO SC VALUES('201215123','3',76);
INSERT INTO SC VALUES('201215123','4',96);
INSERT INTO SC VALUES('201215123','5',85);
INSERT INTO SC VALUES('201215123','6',83);
INSERT INTO SC VALUES('201215123','7',82);
INSERT INTO SC VALUES('201215128','2',86);
INSERT INTO SC VALUES('201215128','3',56);
INSERT INTO SC VALUES('201215128','4',66);
INSERT INTO SC VALUES('201215128','5',88);
INSERT INTO SC VALUES('201215128','6',84);
INSERT INTO SC VALUES('201215128','7',66);
INSERT INTO SC VALUES('201215125','1',86);
SELECT * FROM SC
其余三张表(Managetable,Usertable,SchLog)建表代码:
(数据为自动写入)
CREATE TABLE Usertable
(
Uidentity NCHAR(20), /*身份*/
Uname NCHAR(20) PRIMARY KEY, /*用户名*/ 主码
Upassword NCHAR(32) , /*密码*/
Uid NCHAR(20), /*工号/学号*/
Unumber NCHAR(11), /*电话号码*/
Ubirthday datetime, /*生日*/
);
CREATE TABLE Managetable
(
Midentity NCHAR(20),
Mname NCHAR(20) PRIMARY KEY,
Mpassword NCHAR(32) ,
Mid NCHAR(20),
Mnumber NCHAR(11),
Mbirthday datetime,
);
CREATE TABLE SchLog
{
id varchar(20); /*登入名即为Managetable和Usertable的用户名*/
DateAndTime datetime; /*登入时间*/
UserOperation varchar(200);
};
表中数据显示
SELECT *FROM SC;
SELECT *FROM Student;
SELECT *FROM Course;
SELECT *FROM Managetable;
SELECT *FROM Usertable;
SELECT *FROM SchLog;
执行结果:
1.FormForget(忘记密码界面)
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 Loginupdate
{
public partial class FormForget : Form
{
public FormForget()
{
InitializeComponent();
}
class MyException : Exception
{
public MyException(string message) : base(message)
{
}
}
private void button1_Click(object sender, EventArgs e)
{
string identity = comboBox1.Text.Trim();
string username = textBoxUserName.Text.Trim(); //取出账号
string userid = textBox1.Text.Trim();
string password = EncryptWithMD5(textBoxPassWord.Text.Trim()); //新密码-取出密码并加密
string password1 = EncryptWithMD5(textBox2.Text.Trim()); //确认密码-取出密码并加密
string myConnString = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=sa";
SqlConnection sqlConnection = new SqlConnection(myConnString);
sqlConnection.Open();
string sql;
if (comboBox1.Text.Equals("用户")) //判断身份
{
sql = "select Uname,Uid from Usertable where Uname = '" + username + "' and Uid = '" + userid + "'";
}
else
{
sql = "select Mid,Mid from Managetable where Mname = '" + username + "' and Mid = '" + userid + "'";
}
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)//是否存在
{
sqlDataReader.Close();
if (comboBox1.Text.Equals("管理员"))
{
try
{
if (!password.Equals(password1))
{
string message = null;
if (message == null)
{
throw new MyException("前后两次输入的密码不一样!");
}
}
string sql1 = "UPDATE Managetable SET Mpassword = '" + password + "'WHERE Mname = '" + username + "' AND Mid = '" + userid + "'";
sqlCommand = new SqlCommand(sql1, sqlConnection);
sqlCommand.ExecuteNonQuery();
MessageBox.Show("修改成功!");
}
catch (MyException myException)
{
MessageBox.Show(myException.Message);
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
sqlConnection.Close();
}
}
else
{
try
{
if (!password.Equals(password1))
{
string message = null;
if (message == null)
{
throw new MyException("前后两次输入的密码不一样!");
}
}
string sql2 = "UPDATE Usertable SET Upassword = '" + password + "'WHERE Uname = '" + username + "' AND Uid = '" + userid + "'";
sqlCommand = new SqlCommand(sql2, sqlConnection);
sqlCommand.ExecuteNonQuery();
MessageBox.Show("修改成功!");
}
catch (MyException myException)
{
MessageBox.Show(myException.Message);
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
sqlConnection.Close();
}
}
}
else
{
MessageBox.Show("不存在此用户!");
return;
}
}
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 button2_Click(object sender, EventArgs e)
{
FormLogin formLogin = new FormLogin();
this.Hide();
formLogin.Show();
}
}
}
2.FormRegister (管理员用户注册界面)
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.Threading.Tasks;
using System.Windows.Forms;
namespace Loginupdate
{
public partial class FormRegister : Form
{
public FormRegister()
{
InitializeComponent();
}
private void buttonOk_Click(object sender, EventArgs e)
{
string Rname = textBoxRname.Text.Trim();
string Rpassword = EncryptWithMD5(textBoxRpassword.Text.Trim());
string Rid = textBoxRid.Text.Trim();
string Rnumber = textBoxRnumber.Text.Trim();
string Ridentity = comboBox1.Text.Trim();
DateTime Rdate = dateTimePicker.Value;
if (Ridentity.Equals("") | Rname.Equals("") | Rpassword.Equals("") | Rid.Equals("") | Rnumber.Equals("") | Ridentity.Equals(""))
{
MessageBox.Show("请输入完整");
}
string myConnString = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=sa";
if (Ridentity.Equals("管理员"))
{
SqlConnection sqlConnection = new SqlConnection(myConnString); //实例化连接对象
sqlConnection.Open();
try
{
string sql = "INSERT INTO Managetable " +
"VALUES ('" + Ridentity + "','" + Rname + "','" + Rpassword + "','" + Rid + "','" + Rnumber + "','" + Rdate + "')";//+ "','" + image
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
sqlConnection.Close();
MessageBox.Show("注册成功!");
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
sqlConnection.Close();
}
}
else if (Ridentity.Equals("用户"))
{
SqlConnection sqlConnection = new SqlConnection(myConnString); //实例化连接对象
sqlConnection.Open();
try
{
string sql = "INSERT INTO Usertable(Uidentity,Uname,Upassword,Uid,Unumber,Ubirthday)" +
"VALUES ('" + Ridentity + "','" + Rname + "','" + Rpassword + "','" + Rid + "','" + Rnumber + "','" + Rdate + "')";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
sqlConnection.Close();
MessageBox.Show("注册成功!");
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
sqlConnection.Close();
}
}
else
MessageBox.Show("请选择正确的身份");
SqlConnection sqlConnection1 = new SqlConnection(myConnString); //实例化连接对象
string sql1 = "insert into SchLog values ( '" + Rname + "' , '" + DateTime.Now + "' , '" + "Login" + "')";
SqlCommand sqlCommand1 = new SqlCommand(sql1, sqlConnection1);//编写SQL命令
sqlCommand1 = new SqlCommand(sql1, sqlConnection1);
SqlConnection sqlConnection2 = new SqlConnection(myConnString); //实例化连接对象
string sql2 = "insert into Account values ( '" + Rname + "' , '" + Rpassword + "')";
sqlCommand1 = new SqlCommand(sql2, sqlConnection1);
sqlConnection1.Close();
}
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();
}
public Byte[] mybyte = new byte[0];
private void buttonCancel_Click(object sender, EventArgs e)
{
this.Hide();
FormLogin formLogin = new FormLogin();
formLogin.Show();
}
private void labelRpassword_Click(object sender, EventArgs e)
{
}
}
}
3.FormLogin(登录界面)
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 Loginupdate
{
public partial class FormLogin : Form
{
public FormLogin()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Text.Trim().Equals("")) //提示选择身份
{
MessageBox.Show("请选择身份");
}
string username = textBoxUserName.Text.Trim(); //取出账号
string password = EncryptWithMD5(textBoxPassWord.Text.Trim()); //取出密码并加密
FormMainUser.sno = textBox1.Text.Trim(); //FormMainUser获取学号
string myConnString = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=sa";
SqlConnection sqlConnection = new SqlConnection(myConnString); //实例化连接对象
sqlConnection.Open();
string sql;
if (comboBox1.Text.Equals("用户"))
{
sql = "select Uname,Upassword from Usertable where Uname = '" + username + "' and Upassword = '" + password + "'";
}
else
{
sql = "select Mid,Mpassword from Managetable where Mname = '" + username + "' and Mpassword = '" + password + "'";
}
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
if (textBoxCheckCode.Text == code)
{
if (comboBox1.Text.Equals("管理员"))
{
sqlDataReader.Close();
sql = "insert into SchLog values ( '" + username + "' , '" + DateTime.Now + "' , '" + "Login" + "')"; //编写SQL命令
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
MessageBox.Show("欢迎你,管理员:" + username);
FormManage formManage = new FormManage();
this.Hide();
formManage.Show();
}
else
{
sqlDataReader.Close();
sql = "insert into SchLog values ( '" + username + "' , '" + DateTime.Now + "' , '" + "Login" + "')"; //编写SQL命令
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
MessageBox.Show("欢迎你,用户:" + username);
FormUser formUser = new FormUser();
this.Hide();
formUser.Show();
}
}
}
else
{
MessageBox.Show("登录失败!");
return;
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
public string code;
private void FormLogin_Load(object sender, EventArgs e)
{
Random ran = new Random();
int number;
char code1;
this.code = "";
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();
}
label5.Text = code;
}
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 button3_Click(object sender, EventArgs e)
{
FormRegister formRegister = new FormRegister();
formRegister.ShowDialog();
this.Hide();
}
private void button4_Click(object sender, EventArgs e)
{
this.FormLogin_Load(sender, e);
}
private void button6_Click(object sender, EventArgs e)
{
FormForget formForget = new FormForget();
this.Hide();
formForget.Show();
}
}
}
3.1-FormManage(管理员操作界面)
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 Loginupdate
{
public partial class FormManage : Form
{
public FormManage()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
FormCourse formCourse = new FormCourse();
formCourse.Show();
this.Hide();
}
private void button5_Click(object sender, EventArgs e)
{
this.Hide();
FormLogin formLogin = new FormLogin();
formLogin.Show();
}
private void button1_Click(object sender, EventArgs e)
{
FormLog formLog = new FormLog();
formLog.Show();
this.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
FormMain formMain = new FormMain();
formMain.Show();
this.Hide();
}
private void button4_Click(object sender, EventArgs e)
{
FormSC formSC = new FormSC();
formSC.Show();
this.Hide();
}
private void FormManage_Load(object sender, EventArgs e) //自动显示照片
{
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
3.1.1-FormLog(查看登录日志)
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 Loginupdate
{
public partial class FormLog : Form
{
public FormLog()
{
InitializeComponent();
}
private void FormLog_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“schoolDataSet7.SchLog”中。您可以根据需要移动或删除它。
this.schLogTableAdapter.Fill(this.schoolDataSet7.SchLog);
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
FormManage formManage = new FormManage();
formManage.Show();
}
private void fillByToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.schLogTableAdapter.FillBy(this.schoolDataSet7.SchLog);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
private void 最新登录日志ToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.schLogTableAdapter.最新登录日志(this.schoolDataSet7.SchLog);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
}
}
3.1.2-FormMain(管理员学生信息操作界面)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Loginupdate
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
class MyException : Exception
{
public MyException(string message) : base(message)
{
}
}
private void FormMain_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“schoolDataSet4.SC”中。您可以根据需要移动或删除它。
this.sCTableAdapter.Fill(this.schoolDataSet4.SC);
// TODO: 这行代码将数据加载到表“schoolDataSet3.Course”中。您可以根据需要移动或删除它。
this.courseTableAdapter.Fill(this.schoolDataSet3.Course);
// TODO: 这行代码将数据加载到表“schoolDataSet2.Student”中。您可以根据需要移动或删除它。
this.studentTableAdapter.Fill(this.schoolDataSet2.Student);
}
private void buttonCLOSE_Click(object sender, EventArgs e)
{
this.Hide();
FormManage formManage = new FormManage();
formManage.Show();
}
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=School;User ID=sa;Password=sa");
private void buttonINSERT_Click(object sender, EventArgs e) //插入
{
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
String StuSex = textBox3.Text.Trim();
String StuSdept = textBox4.Text.Trim();
String StuAge = textBox5.Text.Trim();
try
{
con.Open();
string insertStr = "INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) " +
"VALUES ('" + StuID + "','" + StuName + "','" + StuSex + "','" + StuSdept + "'," + StuAge + ")";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
MessageBox.Show("添加成功!");
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Close();
}
this.studentTableAdapter.Fill(this.schoolDataSet2.Student);
}
private void buttonDELETE_Click(object sender, EventArgs e)//删除
{
try
{
con.Open();
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
string delete_by_id = "delete from Student where Sno='" + select_id + "'";//sql删除语句
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd.ExecuteNonQuery();
MessageBox.Show("删除成功!");
}
catch
{
MessageBox.Show("请点击行最左侧进行删除!");
}
finally
{
con.Dispose();
}
this.studentTableAdapter.Fill(this.schoolDataSet2.Student);
}
private void buttonUPDATE_Click(object sender, EventArgs e)//修改
{
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
String StuSex = textBox3.Text.Trim();
String StuSdept = textBox4.Text.Trim();
String StuAge = textBox5.Text.Trim();
try
{
if ((StuID == "") | (StuName == "") | (StuSex == "") | (StuSdept == "") | (StuAge == ""))
{
string message = null;
if (message == null)
{
throw new MyException("输入不能为空");
}
}
con.Open();
string insertStr = "UPDATE Student SET Sname = '" + StuName + "',Ssex = '" + StuSex + "',Sdept='" + StuSdept + "',Sage='" + StuAge + "' WHERE Sno = '" + StuID + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
MessageBox.Show("修改成功!");
}
catch (MyException myException)
{
MessageBox.Show(myException.Message);
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Close();
}
//}
this.studentTableAdapter.Fill(this.schoolDataSet2.Student);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
}
private void buttonSELECT_Click(object sender, EventArgs e)//查找
{
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
String StuSex = textBox3.Text.Trim();
String StuSdept = textBox4.Text.Trim();
String StuAge = textBox5.Text.Trim();
try
{
String select_by_id = "select * from Student where ";
int flag = 0; //0是单条件,1是多条件
con.Open();
if (StuID != "")
select_by_id += "Sno='" + StuID + "'"; //单条件 按学号
if (StuName != "")
{
if (flag == 0)
{
select_by_id += "Sname LIKE'" + StuName + "'";
flag = 1;
}
if (flag == 1)
select_by_id += "And Sname LIKE'" + StuName + "'";
}
if (StuAge != "")
{
if (flag == 0)
{
select_by_id += "Sage='" + StuAge + "'";
flag = 1;
}
if (flag == 1)
select_by_id += "And Sage='" + StuAge + "'";
}
if (StuSdept != "")
{
if (flag == 0)
{
select_by_id += "Sdept='" + StuSdept + "'";
flag = 1;
}
if (flag == 1)
select_by_id += "And Sdept='" + StuSdept + "'";
}
if (StuSex != "")
{
if (flag == 0)
{
select_by_id += "Ssex='" + StuSex + "'";
flag = 1;
}
if (flag == 1)
select_by_id += "And Ssex='" + StuSex + "'";
}
SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句!");
}
finally
{
con.Close();
}
this.studentTableAdapter.Fill(this.schoolDataSet2.Student);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=School;User ID=sa;Password=sa");
try
{
String select_by_id = "select * from Student";
con.Open();
SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句!");
}
finally
{
con.Close();
}
}
}
}
3.1.3-FormCourse(管理员课程管理界面)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Loginupdate
{
public partial class FormCourse : Form
{
public FormCourse()
{
InitializeComponent();
}
class MyException : Exception
{
public MyException(string message) : base(message)
{
}
}
private void FormCourse_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“schoolDataSet5.Course”中。您可以根据需要移动或删除它。
this.courseTableAdapter.Fill(this.schoolDataSet5.Course);
}
private void buttonCLOSE_Click(object sender, EventArgs e)
{
this.Hide();
FormManage formManage = new FormManage();
formManage.Show();
}
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=School;User ID=sa;Password=sa");
private void buttonINSERT_Click(object sender, EventArgs e) // 插入
{
String Cno = textBox1.Text.Trim();
String Cname = textBox2.Text.Trim();
String Cpno = textBox3.Text.Trim();
String Ccredit = textBox4.Text.Trim();
string insertStr;
try
{
con.Open();
if (Cpno.Equals(""))
{
insertStr = "INSERT INTO Course (Cno,Cname,Ccredit) " +
"VALUES ('" + Cno + "','" + Cname + "','" + Ccredit + "')";
}
else
{
insertStr = "INSERT INTO Course (Cno,Cname,Cpno,Ccredit) " +
"VALUES ('" + Cno + "','" + Cname + "','" + Cpno + "','" + Ccredit + "')";
}
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
MessageBox.Show("添加成功!");
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Close();
}
this.courseTableAdapter.Fill(this.schoolDataSet5.Course);
}
private void buttonDELETE_Click(object sender, EventArgs e)
{
try
{
con.Open();
string select_Cno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
string select_Cpno = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();//选择的当前行第一列的值,也就是ID
string delete_by_Cno = "delete from Course where Cno='" + select_Cno + "'AND Cpno='" + select_Cpno + "'";//sql删除语句
SqlCommand cmd = new SqlCommand(delete_by_Cno, con);
cmd.ExecuteNonQuery();
MessageBox.Show("删除成功!");
}
catch
{
MessageBox.Show("请点击行最左侧进行删除!");
}
finally
{
con.Close();
}
this.courseTableAdapter.Fill(this.schoolDataSet5.Course);
}
private void buttonUPDATE_Click(object sender, EventArgs e)//修改--全部输入以修改
{
String Cno = textBox1.Text.Trim();
String Cname = textBox2.Text.Trim();
String Cpno = textBox3.Text.Trim();
String Ccredit = textBox4.Text.Trim();
try
{
if ((Cno == "") | (Cname == "") | (Cpno == "") | (Ccredit == ""))
{
string message = null;
if (message == null)
{
throw new MyException("输入不能为空");
}
}
con.Open();
string insertStr = "UPDATE Course SET Cname = '" + Cname + "',Ccredit='" + Ccredit + "'WHERE Cno = '" + Cno + "'";// +"'AND Cpno='" +Cpno+ "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
MessageBox.Show("修改成功!");
}
catch (MyException myException)
{
MessageBox.Show(myException.Message);
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Close();
}
this.courseTableAdapter.Fill(this.schoolDataSet5.Course);
}
private void buttonSELECT_Click(object sender, EventArgs e)//查找
{
String Cno = textBox1.Text.Trim();
String Cname = textBox2.Text.Trim();
String Cpno = textBox3.Text.Trim();
String Ccredit = textBox4.Text.Trim();
try
{
String select_by_id = "select * from Course where ";
int flag = 0; //0是单条件,1是多条件
con.Open();
if (Cno != "")
select_by_id += "Cno='" + Cno + "'"; //单条件 按学号
if (Cname != "")
{
if (flag == 0)
{
select_by_id += "Cname LIKE'" + Cname + "'";
flag = 1;
}
if (flag == 1)
select_by_id += "And Cname LIKE'" + Cname + "'";
}
if (Cpno != "")
{
if (flag == 0)
{
select_by_id += "Cpno='" + Cpno + "'";
flag = 1;
}
if (flag == 1)
select_by_id += "And Cpno='" + Cpno + "'";
}
if (Ccredit != "")
{
if (flag == 0)
{
select_by_id += "Ccredit='" + Ccredit + "'";
flag = 1;
}
if (flag == 1)
select_by_id += "And Ccredit='" + Ccredit + "'";
}
SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句!");
}
finally
{
con.Close();
}
this.courseTableAdapter.Fill(this.schoolDataSet5.Course);
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=School;User ID=sa;Password=sa");
try
{
String select_by_id = "select * from course";
con.Open();
SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句!");
}
finally
{
con.Close();
}
}
}
}
3.1.4-FormSC(管理员学生成绩操作界面)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Loginupdate
{
public partial class FormSC : Form
{
public FormSC()
{
InitializeComponent();
}
class MyException : Exception
{
public MyException(string message) : base(message)
{
}
}
private void FormSC_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“schoolDataSet6.SC”中。您可以根据需要移动或删除它。
this.sCTableAdapter.Fill(this.schoolDataSet6.SC);
}
private void buttonCLOSE_Click(object sender, EventArgs e)
{
this.Hide();
FormManage formManage = new FormManage();
formManage.Show();
}
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=School;User ID=sa;Password=sa");
private void buttonINSERT_Click(object sender, EventArgs e)
{
String Sno = textBox1.Text.Trim();
String Cno = textBox2.Text.Trim();
String Grade = textBox3.Text.Trim();
try
{
con.Open();
string insertStr = "INSERT INTO SC (Sno,Cno,Grade) " +
"VALUES ('" + Sno + "','" + Cno + "','" + Grade + "')";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
MessageBox.Show("添加成功!");
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Close();
}
this.sCTableAdapter.Fill(this.schoolDataSet6.SC);
}
private void buttonDELETE_Click(object sender, EventArgs e) ///删除
{
String Sno = textBox1.Text.Trim();
String Cno = textBox2.Text.Trim();
String Grade = textBox3.Text.Trim();
try
{
con.Open();
string select_Sno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
string select_Cno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
string delete_by_id = "delete from SC where Sno='" + select_Sno + "'AND Cno='" + Cno + "'";//sql删除语句
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd.ExecuteNonQuery();
MessageBox.Show("删除成功!");
}
catch
{
MessageBox.Show("请点击行最左侧进行删除!");
}
finally
{
con.Dispose();
}
this.sCTableAdapter.Fill(this.schoolDataSet6.SC);
}
private void buttonUPDATE_Click(object sender, EventArgs e) //修改
{
String Sno = textBox1.Text.Trim();
String Cno = textBox2.Text.Trim();
String Grade = textBox3.Text.Trim();
try
{
if ((Sno == "") | (Cno == "") | (Grade == ""))
{
string message = null;
if (message == null)
{
throw new MyException("输入不能为空");
}
}
con.Open();
string insertStr = "UPDATE SC SET Grade = '" + Grade + "' WHERE Sno = '" + Sno + "'AND Cno ='" + Cno + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch (MyException myException)
{
MessageBox.Show(myException.Message);
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Close();
}
this.sCTableAdapter.Fill(this.schoolDataSet6.SC);
}
private void buttonSELECT_Click(object sender, EventArgs e) //查询
{
String Sno = textBox1.Text.Trim();
String Cno = textBox2.Text.Trim();
String Grade = textBox3.Text.Trim();
try
{
String select_by_id = "select * from SC where ";
int flag = 0; //0是单条件,1是多条件
con.Open();
if (Sno != "")
select_by_id += "Sno='" + Sno + "'"; //单条件 按学号
if (Cno != "")
{
if (flag == 0)
{
select_by_id += "Cno LIKE'" + Cno + "'";
flag = 1;
}
if (flag == 1)
select_by_id += "And Cno LIKE'" + Cno + "'";
}
SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句!");
}
finally
{
con.Close();
}
this.sCTableAdapter.Fill(this.schoolDataSet6.SC);
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=School;User ID=sa;Password=sa");
try
{
String select_by_id = "select * from SC";
con.Open();
SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句!");
}
finally
{
con.Close();
}
}
private void 学号_课程号ToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.sCTableAdapter.学号_课程号(this.schoolDataSet6.SC);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
private void 课程号_成绩ToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.sCTableAdapter.课程号_成绩(this.schoolDataSet6.SC);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
private void 课程号_成绩ToolStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
}
}
3.2-FormUser(用户操作界面)
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 Loginupdate
{
public partial class FormUser : Form
{
public FormUser()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
FormCourseUser formCourseUser = new FormCourseUser();
formCourseUser.Show();
this.Hide();
}
private void button1_Click(object sender, EventArgs e)
{
FormMainUser formMainUser = new FormMainUser();
formMainUser.Show();
this.Hide();
}
private void button3_Click(object sender, EventArgs e)
{
this.Hide();
FormLogin formLogin = new FormLogin();
formLogin.Show();
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
3.2.1-FormMainUser (用户查询操作&添加课程界面)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Loginupdate
{
public partial class FormMainUser : Form
{
public FormMainUser()
{
InitializeComponent();
}
private void FormMainUser_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“schoolDataSet8.SC”中。您可以根据需要移动或删除它。
this.sCTableAdapter.Fill(this.schoolDataSet8.SC);
}
String conn = "Data Source=.;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=sa";
private void button1_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection(conn); //实例化连接对象
try
{
String select_by_id = "select * from SC where Sno='" + sno + "'";
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
this.dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句!");
}
finally
{
sqlConnection.Close();
}
}
public static string sno;
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
FormUser formUser = new FormUser();
formUser.Show();
}
private void button3_Click(object sender, EventArgs e)
{
String Cno = textBox2.Text.Trim();
SqlConnection sqlConnection = new SqlConnection(conn); //实例化连接对象
try
{
sqlConnection.Open();
string insertStr = "INSERT INTO SC (Sno,Cno,Grade) " +
"VALUES ('" + sno + "','" + Cno + "'," + 0 + ")";
SqlCommand cmd = new SqlCommand(insertStr, sqlConnection);
cmd.ExecuteNonQuery();
MessageBox.Show("添加成功!");
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
sqlConnection.Close();
}
this.sCTableAdapter.Fill(this.schoolDataSet8.SC);
}
}
}
3.2.2-FormCourseUser(用户查看所选课程界面)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Loginupdate
{
public partial class FormCourseUser : Form
{
public FormCourseUser()
{
InitializeComponent();
}
private void FormCourseUser_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“schoolDataSet9.Course”中。您可以根据需要移动或删除它。
this.courseTableAdapter.Fill(this.schoolDataSet9.Course);
}
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=School;User ID=sa;Password=sa");
private void button3_Click(object sender, EventArgs e)
{
this.Hide();
FormUser formUser = new FormUser();
formUser.Show();
}
}
}
感悟
数据库终于做完了!一块石头落地!
本来我是信心满满,后来越做越不太情愿(拒绝的)的,当时什么也不懂,尤其是看到同学们都做好录屏之后,心里更是着急的鸭皮,事情就是这样,你需要以一个积极的心态,向上的信心慢慢地做,在过程中就会找到解决问题的方法,这也是自己一个成长的过程。
比如大实验之前,我对SQL与C#的了解不熟,C#需要先连接数据库,再执行每一个事务,dispose和close什么时候该用哪个?这得看具体情况,比如我重复执行那个操作时,我选择dispose就会错误,但是close就是正确的。…
数据库大作业遇到的问题及解决方案:数据库大作业遇到的问题及解决方案
源代码下载打包:
链接:https://pan.baidu.com/s/1nEy-X26BBldyfKcXehverg
提取码:4tlq
视频地址:视频暂时还没录好 ,等录完把地址po在这里。
下一篇: 堆栈的实现与应用--工具