管理员登录
private void button1_Click_1(object sender, EventArgs e)//登录
{
string username = textBox1.Text.Trim(); //取出账号
J.j= textBox1.Text.Trim();//记录id
string password = EncryptWithMD5(textBox2.Text.Trim()); //取出密码并加密
string myConnString = "Data Source=.;Initial Catalog=STUDENT;Persist Security Info=True;User ID=sa;Password=tangdou"; //数据库连接
SqlConnection sqlConnection = new SqlConnection(myConnString); //实例化连接对象
sqlConnection.Open();
string sql = "select UserID,UserPassword from SyUser where UserID = '" + username + "' and UserPassword = '" + password + "'"; //编写SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection); //在数据库上执行SELECT操作
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); //读取
Form3 form3 = new Form3(); //实例化
Form13 form13 = new Form13();
if (sqlDataReader.HasRows && textBox3.Text == code) //验证码,用户名,密码都正确 登录成功
{
label1.Text = "登录成功:" + username;
form13.Show(); //显示13
}
else //登录失败
{
form3.Show();//显示登录失败窗口
}
sqlDataReader.Close();
sql = "insert into SysLog values ( '" + username + "' , '" + DateTime.Now + "' , '" + "Login" + "')"; //编写SQL命令,添加登录记录
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlCommand.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();
}
public string code;
private void Login_Load(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();
}
label5.Text = code;//在窗体显示
}
static class J//全局变量保存id
{
public static string j;
}
管理系统界面、
private void 学生信息管理ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2() { MdiParent = this };
ShowOnce(form2);//2为子窗口显示
}
private bool IsExist(string formType)
{
bool flag = false;
foreach (Form item in Application.OpenForms)
{
if (formType == item.GetType().ToString())
flag = true;
}
return flag;
}
public void ShowOnce(Form form)
{
//窗口已存在,则获取该窗口焦点
if (IsExist(form.GetType().ToString()))
{
foreach (Form item in Application.OpenForms)
{
if (item.GetType().ToString() == form.GetType().ToString())
{
//每次判断需要新建一个实例,在此释放
form.Dispose();
item.Show();
//获取窗口焦点
item.Activate();
}
}
}
else
{
//窗口不存在,打开新窗口
form.Show();
form.Activate();
}
}
private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();//退出程序
}
private void 关于ToolStripMenuItem1_Click(object sender, EventArgs e)
{
Form15 form15 = new Form15() { MdiParent = this };
ShowOnce(form15);
}
private void 课程ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form17 form17 = new Form17() { MdiParent = this };
ShowOnce(form17);
}
private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)//成绩管理
{
Form16 form16 = new Form16() { MdiParent = this };
ShowOnce(form16);
}
在数据库中创建表
添加数据
DROP TABLE IF EXISTS SC
DROP TABLE IF EXISTS Student
DROP TABLE IF EXISTS Course
DROP TABLE IF EXISTS SysUser
DROP TABLE IF EXISTS SyUser
DROP TABLE IF EXISTS SysLog
CREATE TABLE SysUser
(
UserID NCHAR(20) PRIMARY KEY,
UserPassWord NCHAR(32) ,
UserSchoolID NCHAR(20),
UserMobile NCHAR(11),
UserBirthday datetime,
UserIdentity NCHAR(20),
UserPhoto image
);
CREATE TABLE SyUser
(
UserID NCHAR(20) PRIMARY KEY,
UserPassWord NCHAR(32) ,
UserSchoolID NCHAR(20),
UserMobile NCHAR(11),
UserBirthday datetime,
UserIdentity NCHAR(20),
UserPhoto image
);
CREATE TABLE SysLog
(
UserID NCHAR(20) ,
DateAndTime datetime,
UserOperation NCHAR(200)
);
CREATE TABLE Student
(
Sno CHAR(9) PRIMARY KEY, /* 列级完整性约束条件,Sno是主码*/
Sname CHAR(20) UNIQUE, /* 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)
);
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);
SELECT * FROM SC
IF(OBJECT_ID(‘regist_recorder’) is not null) – 判断名为 regist_recorder 的触发器是否存在
DROP TRIGGER regist_recorder – 删除触发器
GO
CREATE TRIGGER regist_recorder
ON SysUser
AFTER
INSERT
AS
declare @UserName nchar(20)
declare @DateTime datetime
declare @UserOperation nchar(200)
select @UserName = system_user
select @DateTime = CONVERT(datetime,GETDATE(),120)
declare @op varchar(10)
select @op=case when exists(select 1 from inserted) and exists(select 1 from deleted)
then 'Update'
when exists(select 1 from inserted) and not exists(select 1 from deleted)
then 'Insert'
when not exists(select 1 from inserted) and exists(select 1 from deleted)
then 'Delete' end
select @UserOperation = @op
INSERT INTO SysLog(UserID,DateAndTime,UserOperation)
VALUES (@UserName,@DateTime,@UserOperation)
创建触发器
IF(OBJECT_ID('regist_recorder1') is not null) -- 判断名为 regist_recorder1 的触发器是否存在
DROP TRIGGER regist_recorder1 -- 删除触发器
GO
CREATE TRIGGER regist_recorder1
ON SyUser
AFTER
INSERT
AS
declare @UserName nchar(20)
declare @DateTime datetime
declare @UserOperation nchar(200)
select @UserName = system_user
select @DateTime = CONVERT(datetime,GETDATE(),120)
declare @op varchar(10)
select @op=case when exists(select 1 from inserted) and exists(select 1 from deleted)
then 'Update'
when exists(select 1 from inserted) and not exists(select 1 from deleted)
then 'Insert'
when not exists(select 1 from inserted) and exists(select 1 from deleted)
then 'Delete' end
select @UserOperation = @op
INSERT INTO SysLog(UserID,DateAndTime,UserOperation)
VALUES (@UserName,@DateTime,@UserOperation)