SQL server中的一些操作,贴图可能更加直观一些:
Course表:
DetailedCourse表:
--创建数据库
DROP DATABASE IF EXISTS curricula_variable_system;
CREATE DATABASE curricula_variable_system;
--建表
USE curricula_variable_system;
DROP TABLE IF EXISTS SC
DROP TABLE IF EXISTS Student
DROP TABLE IF EXISTS Course
DROP TABLE IF EXISTS SysUser
DROP TABLE IF EXISTS SysLog
Drop TABLE IF EXISTS SysManage
CREATE TABLE SysUser
(
UserID NCHAR(20) PRIMARY KEY,
UserPassWord NCHAR(32) ,
UserBindingID NCHAR(20),
UserSchoolID NCHAR(20),
UserMobile NCHAR(11),
UserBirthday datetime,
UserPhoto image
);
CREATE TABLE SysManage
(
UserID NCHAR(20) PRIMARY KEY,
UserPassWord NCHAR(32) ,
UserSchoolID NCHAR(20),
UserMobile NCHAR(11),
UserBirthday datetime,
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),
Ccredit SMALLINT
);
CREATE TABLE DetailedCourse//详细课表
(
Cno CHAR(4),
Cname CHAR(40),
Ccredit SMALLINT,
ClassTime NCHAR(10),
Lessonperiod NCHAR(10),
Weeks NCHAR(10),
PRIMARY KEY (Cno,ClassTime,Lessonperiod)
);
CREATE TABLE SC
(
Sno CHAR(9),
Cno CHAR(4),
Cname NCHAR(10),
ClassTime NCHAR(10),
Lessonperiod NCHAR(10),
Weeks NCHAR(10),
Grade SMALLINT,
PRIMARY KEY (Sno,Cno,Cname,ClassTime,Lessonperiod,Weeks), /* 主码由两个属性构成,必须作为表级完整性进行定义*/
FOREIGN KEY (Sno) REFERENCES Student(Sno), /* 表级完整性约束条件,Sno是外码,被参照表是Student */
FOREIGN KEY (Cno)REFERENCES Course(Cno) , /* 表级完整性约束条件, Cno是外码,被参照表是Course*/
FOREIGN KEY (Cno,ClassTime,Lessonperiod)REFERENCES DetailedCourse(Cno,ClassTime,Lessonperiod)
);
INSERT INTO SysUser VALUES ('admin','123',NULL,'000','13812345678',1999-1-1,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,Ccredit) VALUES ('1','数据库',4);
INSERT INTO Course(Cno,Cname,Ccredit) VALUES ('2','数学',4);
INSERT INTO Course(Cno,Cname,Ccredit) VALUES ('3','信息系统',4);
INSERT INTO Course(Cno,Cname,Ccredit) VALUES ('4','操作系统',4);
INSERT INTO Course(Cno,Cname,Ccredit) VALUES ('5','数据结构',4);
INSERT INTO Course(Cno,Cname,Ccredit) VALUES ('6','数据处理',4);
INSERT INTO Course(Cno,Cname,Ccredit) VALUES ('7','Pascal语言',4);
SELECT * FROM Course
INSERT INTO DetailedCourse(Cno,Cname,Ccredit,ClassTime,Lessonperiod,Weeks) VALUES('1','数据库',4,'周一','1-2','1-12周');
INSERT INTO DetailedCourse(Cno,Cname,Ccredit,ClassTime,Lessonperiod,Weeks) VALUES('1','数据库',4,'周三','5-6','1-12周');
INSERT INTO DetailedCourse(Cno,Cname,Ccredit,ClassTime,Lessonperiod,Weeks) VALUES('1','数据库',4,'周二','3-4','1-12周');
INSERT INTO DetailedCourse(Cno,Cname,Ccredit,ClassTime,Lessonperiod,Weeks) VALUES('1','数据库',4,'周五','1-2','1-12周');
INSERT INTO DetailedCourse(Cno,Cname,Ccredit,ClassTime,Lessonperiod,Weeks) VALUES('2','数学',4,'周一','3-4','1-17周');
INSERT INTO DetailedCourse(Cno,Cname,Ccredit,ClassTime,Lessonperiod,Weeks) VALUES('2','数学',4,'周三','9-10','1-17周');
INSERT INTO DetailedCourse(Cno,Cname,Ccredit,ClassTime,Lessonperiod,Weeks) VALUES('3','信息系统',4,'周一','5-6','1-17周');
INSERT INTO DetailedCourse(Cno,Cname,Ccredit,ClassTime,Lessonperiod,Weeks) VALUES('3','信息系统',4,'周五','5-6','1-17周');
INSERT INTO DetailedCourse(Cno,Cname,Ccredit,ClassTime,Lessonperiod,Weeks) VALUES('4','操作系统',4,'周二','5-6','1-12周');
INSERT INTO DetailedCourse(Cno,Cname,Ccredit,ClassTime,Lessonperiod,Weeks) VALUES('4','操作系统',4,'周三','9-10','1-12周');
INSERT INTO DetailedCourse(Cno,Cname,Ccredit,ClassTime,Lessonperiod,Weeks) VALUES('5','数据结构',4,'周四','3-4','1-17周');
INSERT INTO DetailedCourse(Cno,Cname,Ccredit,ClassTime,Lessonperiod,Weeks) VALUES('5','数据结构',4,'周五','7-8','1-17周');
INSERT INTO DetailedCourse(Cno,Cname,Ccredit,ClassTime,Lessonperiod,Weeks) VALUES('6','数据处理',4,'周三','1-2','1-12周');
INSERT INTO DetailedCourse(Cno,Cname,Ccredit,ClassTime,Lessonperiod,Weeks) VALUES('7','Pascal语言',4,'周四','5-6','1-12周');
INSERT INTO SC(Sno,Cno,Cname,ClassTime,Lessonperiod,Weeks) VALUES ('201215121 ','1','数据库','周一','1-2','1-12周',null);
INSERT INTO SC(Sno,Cno,Cname,ClassTime,Lessonperiod,Weeks) VALUES ('201215121 ','2','数学','周一','3-4','1-17周',null);
INSERT INTO SC(Sno,Cno,Cname,ClassTime,Lessonperiod,Weeks) VALUES ('201215121 ','3','信息系统','周一','5-6','1-17周',null);
INSERT INTO SC(Sno,Cno,Cname,ClassTime,Lessonperiod,Weeks) VALUES ('201215122 ','2','数学','周三','9-10','1-17周',null);
INSERT INTO SC(Sno,Cno,Cname,ClassTime,Lessonperiod,Weeks) VALUES ('201215122 ','3','信息系统','周五','5-6','1-12周',null);
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_recorder2') is not null) -- 判断名为 regist_recorder 的触发器是否存在
DROP TRIGGER regist_recorder2 -- 删除触发器
GO
CREATE TRIGGER regist_recorder2
ON SysManage
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)
一个弹出小窗口MessageBox:,设置了透明度90%,用来进行信息弹窗,因为不想点击确定设置的。
MessageBox代码:
public partial class MessageBox : Form
{
public MessageBox()
{
InitializeComponent();
}
//输入一个字符的show函数
public void show(string a)
{
label1.Text = a.ToString();
this.Show();
}
//一个字符串的showdialog函数
public void showDialog(string a)
{
label1.Text = a.ToString();
this.ShowDialog();
}
private void MessageBox_Load(object sender, EventArgs e){}
}
这里为了让程序看起来简便些,就把下面窗口都有的一些代码列出来(这些代码拿出来,程序会报错的):
//跳出一个小窗口用,这里先建立变量,建立一个定时器timer,想弹出信息的时候,就开启定时器,打开窗口,到时间了就关闭窗口,设定是2.5s
MessageBox m;
private void timer1_Tick(object sender, EventArgs e)
{
//到点就让Messagebox关闭,
m.Close();
timer1.Enabled = false;
}
//以下代码是为实现窗口可拖动,因为FormBorderStyle设置为NONE(搜的),但是不能点到控件上(每个窗口都有用到,为了简约,只在这个代码中显示一次)
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
private void Login_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
//不同的窗体按钮名称可能会有所不同
//关闭按钮
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
//最小化按钮
private void button3_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
窗口之间的调用如下图:
Login(登录窗体)
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
//静态变量,为之后的绑定页面提供变量
private static string username;
public static string getusername()
{
return username;
}
//MD5
public static string EncryptWithMD5(string source)
{
byte[] sor = Encoding.UTF8.GetBytes(source); //将source转化为UF8编码存到sor字节数组内
MD5 md5 = MD5.Create(); //MD5声明
byte[] result = md5.ComputeHash(sor); //使用MD5加密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 linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) //点击注册
{
Register register = new Register();
register.ShowDialog(); //打开新窗体Register
}
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;
//加载一张默认图片
this.pictureBox1.Image = Image.FromFile(@"D:\my\csharp工程文件夹\图片\a.jpg");
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
//点击登录
private void button1_Click(object sender, EventArgs e)
{
username = textBox1.Text.Trim(); //取出账号
string password = EncryptWithMD5(textBox2.Text.Trim()); //取出密码并加密
if(username.Equals("")||password.Equals(""))
{
m = new MessageBox();
timer1.Enabled = true;
m.show("账号或密码不可为空!");
return;
}
string myConnString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(myConnString); //实例化连接对象
sqlConnection.Open(); //数据库打开
//选择管理员或者学生
if (radioButton1.Checked || radioButton2.Checked)
{
if (radioButton1.Checked)//管理员登录
{
string sql = "select UserID,UserPassWord from SysManage where UserID = '" + username + "' and UserPassWord = '" + password + "'"; //编写SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows && textBox3.Text == code) //查询结果非空并且验证码正确
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("欢迎使用!");
//登录成功 新窗体打开,继续执行代码
ManageMain form2 = new ManageMain();
form2.Show();
this.Hide();
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("登录失败!");
return;
}
sqlDataReader.Close();
//插入登录记录
sql = "insert into SysLog values ( '" + username + "' , '" + DateTime.Now + "' , '" + "Login" + "')"; //编写SQL命令
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlCommand.ExecuteNonQuery();
}
else if (radioButton2.Checked)//学生登录
{
string sql = "select UserID,UserPassWord from SysUser where UserID = '" + username + "' and UserPassWord = '" + password + "'"; //编写SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows&& textBox3.Text == code) //查询结果非空并且验证码正确
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("欢迎使用!"); //登录成功
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("登录失败!");
return;
}
sqlDataReader.Close();
sql = "select * from SysUser where UserBindingID is null and UserID = '"+username+"'";
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
//新窗体打开,继续执行代码
Bind form3 = new Bind();
form3.Show();
this.Hide();
}
else
{
//新窗体打开,继续执行代码
StudentMain form3 = new StudentMain();
form3.Show();
this.Hide();
}
sqlDataReader.Close();
//插入登录记录
sql = "insert into SysLog values ( '" + username + "' , '" + DateTime.Now + "' , '" + "Login" + "')"; //编写SQL命令
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlCommand.ExecuteNonQuery();
}
sqlConnection.Close();
}
else
{
//管理员,学生都没选择
m = new MessageBox();
timer1.Enabled = true;
m.show("请选择要登录的身份!");
}
}
//点击事件
private void textBox1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Equals("用户名"))
textBox1.Text = "";
}
//离开用户名时
private void textBox1_Leave(object sender, EventArgs e)
{
if(textBox1.Text .Equals(""))
{
textBox1.Text = "用户名";
}
else
{
string myConnString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(myConnString); //实例化连接对象
sqlConnection.Open(); //数据库打开
string sql;
try
{
//管理员
if (radioButton1.Checked)
{
sql = "select UserPhoto from SysManage where UserID = '" + textBox1.Text + "'";
SqlDataAdapter da = new SqlDataAdapter(sql, sqlConnection);
DataTable dt = new DataTable();
da.Fill(dt);
//把图片转换一下
MemoryStream ms = new MemoryStream((byte[])dt.Rows[0][0]);
Bitmap bmpt = new Bitmap(ms);
this.pictureBox1.Image = bmpt;
// pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
else if (radioButton2.Checked)
{
sql = "select UserPhoto from SysUser where UserID = '" + textBox1.Text + "'";
SqlDataAdapter da = new SqlDataAdapter(sql, sqlConnection);
DataTable dt = new DataTable();
da.Fill(dt);
//显示图片
MemoryStream ms = new MemoryStream((byte[])dt.Rows[0][0]);
Bitmap bmpt = new Bitmap(ms);
this.pictureBox1.Image = bmpt;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
catch
{
m = new MessageBox();
timer1.Enabled = true;
m.show("没有该用户名!");
}
}
}
//这些操作是为了文本框上有文字,点击之后就没有了,没有输入的话再离开就又会显示文字
private void textBox2_Click(object sender, EventArgs e)
{
if (textBox2.Text.Equals("密码"))
textBox2.Text = "";
textBox2.PasswordChar = '*';
}
private void textBox2_Leave(object sender, EventArgs e)
{
if (textBox2.Text.Equals(""))
{
textBox2.PasswordChar = '\0';
textBox2.Text = "密码";
}
}
private void textBox3_Click(object sender, EventArgs e)
{
if (textBox3.Text.Equals("验证码"))
textBox3.Text = "";
}
private void textBox3_Leave(object sender, EventArgs e)
{
if (textBox3.Text.Equals(""))
{
textBox3.Text = "验证码";
}
}
//点击验证码事件
private void label5_Click(object sender, EventArgs e)
{
code = "";
//随机实例化
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;
}
}
public partial class Register : Form
{
public Register()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) //注册按钮
{
string connString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=123456";//数据库连接字符串
SqlConnection connection = new SqlConnection(connString);//创建connection对象
if (radioButton1.Checked || radioButton2.Checked)
{
if (radioButton1.Checked)
{
if (textBox1.Text.Equals("用户名") || textBox2.Text.Equals("密码") || textBox3.Equals("学校编号") || textBox4.Text.Equals("电话")|| mybyte.Length == 0)
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("填入不规范,无法注册!");
return;
}
else
{
//密码格式
Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");
if (regex.IsMatch(textBox2.Text))//判断格式是否符合要求
{
//MessageBox.Show("输入密码格式正确!");
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符!");
return;
}
}
try
{
string sql = "insert into SysManage (UserID, UserPassWord , UserSchoolID, UserMobile, UserBirthday , UserPhoto ) " +
"values (@userid, @userpassword,@userschoolid,@usermobile,@userbirthday,@userphoto)";
SqlCommand command = new SqlCommand(sql, connection);
SqlParameter sqlParameter = new SqlParameter("@userid", textBox1.Text);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@userpassword", EncryptWithMD5(textBox2.Text));
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@userschoolid", textBox3.Text);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@usermobile", textBox4.Text);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@userbirthday", dateTimePicker1.Value);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@userphoto", SqlDbType.VarBinary, mybyte.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, mybyte);
command.Parameters.Add(sqlParameter);
//打开数据库连接
connection.Open();
command.ExecuteNonQuery();
connection.Close();
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("用户注册成功!");
}
catch (Exception ex)
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("程序发生错误!");
}
}
else if (radioButton2.Checked)
{
if (textBox1.Text.Equals("用户名") || textBox2.Text.Equals("密码") || textBox3.Equals("学校编号") || textBox4.Text.Equals("电话") || mybyte.Length == 0)
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("填入不规范,无法注册!");
return;
}
else
{
}
try
{
string sql = "insert into SysUser (UserID, UserPassWord , UserSchoolID, UserMobile, UserBirthday , UserPhoto ) " +
"values (@userid, @userpassword,@userschoolid,@usermobile,@userbirthday,@userphoto)";
SqlCommand command = new SqlCommand(sql, connection);
SqlParameter sqlParameter = new SqlParameter("@userid", textBox1.Text);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@userpassword", EncryptWithMD5(textBox2.Text));
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@userschoolid", textBox3.Text);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@usermobile", textBox4.Text);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@userbirthday", dateTimePicker1.Value);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@userphoto", SqlDbType.VarBinary, mybyte.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, mybyte);
command.Parameters.Add(sqlParameter);
//打开数据库连接
connection.Open();
command.ExecuteNonQuery();
connection.Close();
m = new MessageBox();
timer1.Enabled = true;
m.show("用户注册成功!");
}
catch (Exception ex)
{
m = new MessageBox();
timer1.Enabled = true;
m.show("程序发生错误!");
}
}
this.Close();
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.show("请选择您要注册的身份!");
}
}
public Byte[] mybyte = new byte[0];
private void button3_Click(object sender, EventArgs e)//上传头像
{
//打开浏览图片对话框
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.ShowDialog();
string picturePath = openFileDialog.FileName;//获取图片路径
//文件的名称,每次必须更换图片的名称,这里很为不便
//创建FileStream对象
FileStream fs = new FileStream(picturePath, FileMode.Open, FileAccess.Read);
//声明Byte数组
mybyte = new byte[fs.Length];
//读取数据
fs.Read(mybyte, 0, mybyte.Length);
pictureBox1.Image = Image.FromStream(fs);
fs.Close();
}
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 textBox2_Leave(object sender, EventArgs e)
{
if (textBox2.Text.Trim() != "")
{
//使用regex(正则表达式)进行格式设置 至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符。
Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");
if (regex.IsMatch(textBox2.Text))//判断格式是否符合要求
{
//MessageBox.Show("输入密码格式正确!");
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符!");
}
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("信息不能为空!");
textBox2.PasswordChar = '\0';
textBox2.Text = "密码";
}
}
//离开用户名行
private void textBox1_Leave(object sender, EventArgs e)
{
if (textBox1.Text.Trim() != "")
{
Regex regex = new Regex(@".{2,10}");
if (regex.IsMatch(textBox1.Text))//判断格式是否符合要求
{
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("用户名设置在2 - 3个字符!");
}
string myConnString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(myConnString);
sqlConnection.Open();
string username = textBox1.Text.Trim();
if (radioButton1.Checked || radioButton2.Checked)
{
if (radioButton1.Checked) //管理员
{
string sql = "select UserID from SysManage where UserID = '" + username + "'"; //编写SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("用户名重复!");
}
else
{
}
sqlConnection.Close();
}
else if (radioButton2.Checked)//学生
{
string sql = "select UserID from SysUser where UserID = '" + username + "'"; //编写SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("用户名重复!");
}
else
{
}
sqlConnection.Close();
}
}
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("信息不能为空!");
textBox1.Text = "用户名";
}
}
//离开学校编号行
private void textBox3_Leave(object sender, EventArgs e)
{
if (textBox3.Text.Trim() != "")
{
Regex regex = new Regex(@".{3,3}");
if (regex.IsMatch(textBox3.Text))//判断格式是否符合要求
{
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("请输入3个字符!");
}
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("内容不可为空!");
textBox3.Text = "学校编号";
}
}
//离开电话行
private void textBox4_Leave(object sender, EventArgs e)
{
if (textBox4.Text.Trim() != "")
{
Regex regex = new Regex(@".{11,11}");
if (regex.IsMatch(textBox4.Text))//判断格式是否符合要求
{
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("请输入11个字符!");
}
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("内容不可为空!");
textBox4.Text = "电话";
}
}
//点一下字就消失
private void textBox1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Equals("用户名"))
{
textBox1.Text = "";
}
}
private void textBox2_Click(object sender, EventArgs e)
{
if (textBox2.Text.Equals("密码"))
textBox2.Text = "";
textBox2.PasswordChar = '*';
}
private void textBox3_Click(object sender, EventArgs e)
{
if (textBox3.Text.Equals("学校编号"))
textBox3.Text = "";
}
private void textBox4_Click(object sender, EventArgs e)
{
if (textBox4.Text.Equals("电话"))
{
textBox4.Text = "";
}
}
}
Bind窗口:
public partial class Bind : Form
{
public Bind()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
String StudentID = textBox1.Text.Trim();
string Sname = textBox2.Text.Trim();
string Sage = textBox3.Text.Trim();
string myConnString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(myConnString);
sqlConnection.Open();
string sql = "select Sno from Student where Sno = '" + StudentID + "' and Sname = '" + Sname + "' and Sage = '" + Sage + "' "; //编写SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.show("绑定失败!学生信息有误!");
return;
}
sqlDataReader.Close();
sql = "select UserBindingID from SysUser where UserBindingID = '" + StudentID + "'";
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
m = new MessageBox();
timer1.Enabled = true;
m.show("绑定失败!该学号已被绑定!");
return;
}
sqlDataReader.Close();
sql = "update SysUser set UserBindingID = '" + StudentID + "' where UserID = '" + Login.getusername() + "'";
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlDataReader = sqlCommand.ExecuteReader();
m = new MessageBox();
timer1.Enabled = true;
m.show("绑定成功!");
StudentMain a = new StudentMain();
a.Show();
this.Hide();
sqlConnection.Close();
}
}
StudentMain窗口:
(查询课程选项卡)
(添加课程选项卡)
(删除学生选项卡)
public partial class StudentMain : Form
{
public StudentMain()
{
InitializeComponent();
}
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
//重绘控件,使选项卡文字水平
{
SolidBrush _Brush = new SolidBrush(Color.Black);//单色画刷
RectangleF _TabTextArea = (RectangleF)tabControl1.GetTabRect(e.Index);//绘制区域
StringFormat _sf = new StringFormat();//封装文本布局格式信息
_sf.LineAlignment = StringAlignment.Center;
_sf.Alignment = StringAlignment.Center;
e.Graphics.DrawString(tabControl1.Controls[e.Index].Text, SystemInformation.MenuFont, _Brush, _TabTextArea, _sf);
//标签背景填充颜色
SolidBrush BackBrush = new SolidBrush(Color.FromArgb(16, 24, 80));
//标签文字填充颜色
SolidBrush FrontBrush = new SolidBrush(Color.FromArgb(210, 210, 221));
StringFormat StringF = new StringFormat();
//设置文字对齐方式
StringF.Alignment = StringAlignment.Center;
StringF.LineAlignment = StringAlignment.Center;
for (int i = 0; i < tabControl1.TabPages.Count; i++)
{
//获取标签头工作区域
Rectangle Rec = tabControl1.GetTabRect(i);
e.Graphics.FillRectangle(BackBrush, Rec);
//绘制标签头文字
e.Graphics.DrawString(tabControl1.TabPages[i].Text, new Font("楷体", 10), FrontBrush, Rec, StringF);
}
}
private void StudentMain_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“curricula_variable_systemDataSet16.Course”中。您可以根据需要移动或删除它。
this.courseTableAdapter4.Fill(this.curricula_variable_systemDataSet16.Course);
//加载一个小头像和一个名字
string myConnString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(myConnString); //实例化连接对象
sqlConnection.Open(); //数据库打开
string sql;
sql = "select Sname from Student where Sno = (select UserBindingID from SysUser where UserID = '" + Login.getusername() + "')";
SqlDataAdapter da = new SqlDataAdapter(sql, sqlConnection);
DataTable dt = new DataTable();
da.Fill(dt);
label9.Text = dt.Rows[0][0].ToString();
sql = "select UserPhoto from SysUser where UserID = '" + Login.getusername() + "'";
SqlDataAdapter da2 = new SqlDataAdapter(sql, sqlConnection);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
MemoryStream ms = new MemoryStream((byte[])dt2.Rows[0][0]);
Bitmap bmpt = new Bitmap(ms);
this.pictureBox1.Image = bmpt;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
//按课程名
private void button1_Click_1(object sender, EventArgs e) //查询课程按钮
{
//将选中的信息读取出来 课程名称
string cname = comboBox1.Text.Trim();
//数据库连接
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
try
{
sqlConnection.Open();
string select;
if (cname.Equals("全部"))
//查询全部课程,根据用户名搜索绑定学号,因为没法直接获取学号(已经绑定过学号的用户直接进入到了学生界面)
select = "select * from SC where Sno= (select UserBindingID from SysUser where UserID = '" + Login.getusername() + "') ";
else
select = "select * from SC where Sno= (select UserBindingID from SysUser where UserID = '" + Login.getusername() + "') and Cname= '" + cname + "' ";
SqlCommand sqlCommand = new SqlCommand(select, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource(); //绑定
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
m = new MessageBox();
timer1.Enabled = true;
m.show("程序发生错误!");
}
finally
{
sqlConnection.Close();
}
}
//按课序号查询按钮
private void button4_Click(object sender, EventArgs e)
{
//将选中的信息读取出来 课程名称
string cno = textBox1.Text.Trim();
//数据库连接
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
try
{
sqlConnection.Open();
string select;
if (cno.Equals(""))
//查询全部课程,根据用户名搜索绑定学号,因为没法直接获取学号(已经绑定过学号的用户直接进入到了学生界面)
select = "select * from SC where Sno= (select UserBindingID from SysUser where UserID = '" + Login.getusername() + "') ";
else
select = "select * from SC where Sno= (select UserBindingID from SysUser where UserID = '" + Login.getusername() + "') and Cno= '" + cno + "' ";
SqlCommand sqlCommand = new SqlCommand(select, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource(); //绑定
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
m = new MessageBox();
timer1.Enabled = true;
m.show("程序发生错误!");
}
finally
{
sqlConnection.Close();
}
}
//搜索课程按钮,用于添加的
private void button3_Click(object sender, EventArgs e)
{
//要搜索的课程名字
string cname = comboBox3.Text.Trim();
//连接数据库
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
try
{
sqlConnection.Open();
string sql;
if (cname.Equals("全部"))//查询全部课程
//查询详细课程表中没有被这个学生选过的全部课程信息
sql = "select * from DetailedCourse where NOT EXISTS (select * from SC where Sno = (select UserBindingID from SysUser where UserID = '" + Login.getusername() + "') and Cno=DetailedCourse.Cno and ClassTime=DetailedCourse.ClassTime and LessonPeriod=DetailedCourse.LessonPeriod and Weeks=DetailedCourse.Weeks )";
else
//查询详细课程表中没有被这个学生选过的客户要求的一个课程信息
sql = "select * from DetailedCourse where NOT EXISTS (select * from SC where Sno = (select UserBindingID from SysUser where UserID = '" + Login.getusername() + "') and Cno=DetailedCourse.Cno and ClassTime=DetailedCourse.ClassTime and LessonPeriod=DetailedCourse.LessonPeriod and Weeks=DetailedCourse.Weeks ) and Cname = '" + cname + " '";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource(); //绑定显示信息
bindingSource.DataSource = sqlDataReader;
dataGridView2.DataSource = bindingSource;
}
catch
{
m = new MessageBox();
timer1.Enabled = true;
m.show("程序发生错误!");
}
finally
{
sqlConnection.Close();
}
}
//用于添加的第二个搜索课程,按课序号
private void button5_Click(object sender, EventArgs e)
{
//要搜索的课序号名字
string cno = textBox2.Text.Trim();
//连接数据库
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
try
{
sqlConnection.Open();
string sql;
if (cno.Equals(""))//没有填写课序号,按全部搜索
//查询详细课程表中没有被这个学生选过的全部课程信息
sql = "select * from DetailedCourse where NOT EXISTS (select * from SC where Sno = (select UserBindingID from SysUser where UserID = '" + Login.getusername() + "') and Cno=DetailedCourse.Cno and ClassTime=DetailedCourse.ClassTime and LessonPeriod=DetailedCourse.LessonPeriod and Weeks=DetailedCourse.Weeks )";
else
//查询详细课程表中没有被这个学生选过的客户要求的一个课程信息
sql = "select * from DetailedCourse where NOT EXISTS (select * from SC where Sno = (select UserBindingID from SysUser where UserID = '" + Login.getusername() + "') and Cno=DetailedCourse.Cno and ClassTime=DetailedCourse.ClassTime and LessonPeriod=DetailedCourse.LessonPeriod and Weeks=DetailedCourse.Weeks ) and Cno = '" + cno + " '";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource(); //绑定显示信息
bindingSource.DataSource = sqlDataReader;
dataGridView2.DataSource = bindingSource;
}
catch
{
m = new MessageBox();
timer1.Enabled = true;
m.show("程序发生错误!");
}
finally
{
sqlConnection.Close();
}
}
//添加课程按钮
private void button6_Click(object sender, EventArgs e)
{
//行数
int a = dataGridView2.CurrentCell.RowIndex;
string cname = comboBox3.Text.Trim();
string select_cno = dataGridView2.Rows[a].Cells[0].Value.ToString().Trim();
string select_cname = dataGridView2.Rows[a].Cells[1].Value.ToString().Trim();
//没有取学分
string select_jie = dataGridView2.Rows[a].Cells[3].Value.ToString().Trim();
string select_ke = dataGridView2.Rows[a].Cells[4].Value.ToString().Trim();
string select_zhou = dataGridView2.Rows[a].Cells[5].Value.ToString().Trim();
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
//取到要选的课程的上课次数
string select = "select count(*) from DetailedCourse where Cno = '" + select_cno + "'";
SqlDataAdapter da = new SqlDataAdapter(select, sqlConnection);
DataTable dt = new DataTable();
da.Fill(dt);
int count = (int)dt.Rows[0][0];
select = "select * from DetailedCourse where Cno = '" + select_cno + "'";
select = "select * from DetailedCourse where Cno = '" + select_cno + "'";
SqlDataAdapter da2 = new SqlDataAdapter(select, sqlConnection);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
int c = 1;//用以检查是否有重复项
//检查时间是否有冲突
for(int i=0;i<count;i++)
{
select_jie = dt2.Rows[i][3].ToString();
select_ke = dt2.Rows[i][4].ToString();
select = "select * from SC where Sno = (select UserBindingID from SysUser where UserID = '" + Login.getusername() + "') INTERSECT select * from SC where ClassTime = '" + select_jie + "'and Lessonperiod = '" + select_ke + "'";
SqlCommand sqlCommand2 = new SqlCommand(select, sqlConnection);
SqlDataReader sqlDataReader2 = sqlCommand2.ExecuteReader();
if (sqlDataReader2.HasRows)
{
c = 0;
}
sqlDataReader2.Close();
}
if(c==0)
{
m = new MessageBox();
timer1.Enabled = true;
m.show("该课程与已选课程时间有冲突,不能选择!");
return;
}
//开始添加
string sql;
for (int i=0;i<count;i++)
{
select_jie = dt2.Rows[i][3].ToString();
select_ke = dt2.Rows[i][4].ToString();
select_zhou = dt2.Rows[i][5].ToString();
sql = "insert into SC values ((select UserBindingID from SysUser where UserID = '" + Login.getusername() + "'),'" + select_cno + "','" + select_cname + "','" + select_jie + "','" + select_ke + "','" + select_zhou + "',NULL)";
SqlCommand sqlCommand3 = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader3 = sqlCommand3.ExecuteReader();
sqlDataReader3.Close();
}
m = new MessageBox();
timer1.Enabled = true;
m.show("添加课程成功!");
//相当于刷新一下(这里没有贴刷新代码,与上方查询一致)(不做展示)
sqlConnection.Close();
}
//搜索课程按钮,用于删除的
private void button2_Click(object sender, EventArgs e)
{
//要搜索的课程名字
string cname = comboBox4.Text.Trim();
//连接数据库
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
try
{
sqlConnection.Open();
string sql;
if (cname.Equals("全部"))//查询全部课程
//查询详细课程表中被这个学生选过的全部课程信息
sql = "select * from DetailedCourse where EXISTS (select * from SC where Sno = (select UserBindingID from SysUser where UserID = '" + Login.getusername() + "') and Cno=DetailedCourse.Cno and ClassTime=DetailedCourse.ClassTime and LessonPeriod=DetailedCourse.LessonPeriod and Weeks=DetailedCourse.Weeks )";
else
//查询详细课程表中被这个学生选过的客户要求的一个课程信息
sql = "select * from DetailedCourse where EXISTS (select * from SC where Sno = (select UserBindingID from SysUser where UserID = '" + Login.getusername() + "') and Cno=DetailedCourse.Cno and ClassTime=DetailedCourse.ClassTime and LessonPeriod=DetailedCourse.LessonPeriod and Weeks=DetailedCourse.Weeks ) and Cname = '" + cname + " '";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource(); //绑定显示信息
bindingSource.DataSource = sqlDataReader;
dataGridView3.DataSource = bindingSource;
}
catch
{
m = new MessageBox();
timer1.Enabled = true;
m.show("程序发生错误!");
}
finally
{
sqlConnection.Close();
}
}
//点击删除里的按学号查询的按钮
private void button7_Click(object sender, EventArgs e)
{
//将选中的信息读取出来 课程名称
string cno = textBox3.Text.Trim();
//数据库连接
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
try
{
sqlConnection.Open();
string select;
if (cno.Equals(""))
//查询全部课程,根据用户名搜索绑定学号,因为没法直接获取学号(已经绑定过学号的用户直接进入到了学生界面)
select = "select * from SC where Sno= (select UserBindingID from SysUser where UserID = '" + Login.getusername() + "') ";
else
select = "select * from SC where Sno= (select UserBindingID from SysUser where UserID = '" + Login.getusername() + "') and Cno= '" + cno + "' ";
SqlCommand sqlCommand = new SqlCommand(select, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource(); //绑定
bindingSource.DataSource = sqlDataReader;
dataGridView3.DataSource = bindingSource;
}
catch
{
m = new MessageBox();
timer1.Enabled = true;
m.show("程序发生错误!");
}
finally
{
sqlConnection.Close();
}
}
//点击删除课程按钮
private void button8_Click(object sender, EventArgs e)
{
//提取选中的信息
string cname = comboBox4.Text.Trim();
string select_cno = Convert.ToString(dataGridView3.CurrentRow.Cells[0].Value).Trim();
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
string sql;
sql = "delete from SC where Sno=(select UserBindingID from SysUser where UserID = '" + Login.getusername() + "') and Cno = '" + select_cno + "'";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
sqlDataReader.Close();
m = new MessageBox();
timer1.Enabled = true;
m.show("删除课程成功!");
//这里会刷新一遍,就是重新查一遍,这里按照课程名那里查询(不做展示)
sqlConnection.Close();
}
//这一段代码是参考了网上的代码,就是合并单元格。下面两个函数与其内容相同
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 1 && e.RowIndex != -1 || e.ColumnIndex == 2 && e.RowIndex != -1)
{
Brush bruch = new SolidBrush(dataGridView1.GridColor);
SolidBrush sbruch = new SolidBrush(e.CellStyle.BackColor);
using (Pen pen = new Pen(bruch))
{
e.Graphics.FillRectangle(sbruch, e.CellBounds);
if (e.RowIndex < dataGridView1.Rows.Count - 1 && dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != null && dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString())
{
e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(pen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
}
else
{
e.Graphics.DrawLine(pen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
}
if (e.RowIndex == dataGridView1.Rows.Count - 1)
{
e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
}
if (e.Value != null)
{
if (!(e.RowIndex > 0 && dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() == e.Value.ToString()))
{
e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault);
}
}
e.Handled = true;
}
}
}
//相同内容和上面
private void dataGridView2_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
}
private void dataGridView3_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
//内容和上面函数几乎相同,不再粘贴
}
//查询成绩按钮
private void button9_Click(object sender, EventArgs e)
{
string conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn);
sqlConnection.Open();
string sql;
sql = "select distinct Cno,Cname,Grade from SC where Sno=(select UserBindingID from SysUser where UserID = '" + Login.getusername() + "') ";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource(); //绑定显示信息
bindingSource.DataSource = sqlDataReader;
dataGridView4.DataSource = bindingSource;
sqlConnection.Close();
}
//点击头像旁边的倒三角
private void button12_Click(object sender, EventArgs e)
{
if(this.panel2.Visible == false)
this.panel2.Visible = true;
else
this.panel2.Visible = false;
}
//修改个人信息按钮
private void button13_Click(object sender, EventArgs e)
{
//进入修改个人信息页面
AlterInfroStu a = new AlterInfroStu();
a.ShowDialog();
string myConnString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=123456";
//查询一下名字还是不是她
SqlConnection sqlConnection = new SqlConnection(myConnString); //实例化连接对象
sqlConnection.Open(); //数据库打开
//重新加载一下头像和名字
string sql;
sql = "select Sname from Student where Sno = (select UserBindingID from SysUser where UserID = '" + Login.getusername() + "')";
SqlDataAdapter da = new SqlDataAdapter(sql, sqlConnection);
DataTable dt = new DataTable();
da.Fill(dt);
label9.Text = dt.Rows[0][0].ToString();
sql = "select UserPhoto from SysUser where UserID = '" + Login.getusername() + "'";
SqlDataAdapter da2 = new SqlDataAdapter(sql, sqlConnection);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
MemoryStream ms = new MemoryStream((byte[])dt2.Rows[0][0]);
Bitmap bmpt = new Bitmap(ms);
this.pictureBox1.Image = bmpt;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
//退出登录
private void button14_Click(object sender, EventArgs e)
{
Application.Exit();//结束进程
}
}
AlterInforStu窗口:
这里运行其实有一些错误,在修改密码上,①首先是对原密码是否正确的判断,②修改后的密码两次输入是否一致;③修改后的密码格式是否符合要求。只有都符合,才能修改,可是执行的过程中,如果我已经执行过了②或者③,或者他们都执行过了,并且又提示“密码不一致”或者“按格式修改密码”这两个,我再次输入正确格式后,点击修改,这个程序就会卡住,什么服务器未响应。我搜索了一下,一般大数据会有这个错,可我相关的数据库统共就一条信息,断点测试语句没有问题,在数据库执行可以更改,所以我并不清楚这是什么原因。演示的时候就跳过了这一部分。
public partial class AlterInfroStu : Form
{
public AlterInfroStu()
{
InitializeComponent();
}
//现在绑定的学号,为了和修改之后的学号进行对比。规定解除绑定时不做操作,点击绑定后直接更改UserBindingID的值
//万一用户解除绑定后没动,又要绑定了相同的学号,那在检测这个学号的时候,会出现该学号已经被绑定,但事实是绑定这个学号的账户其实就是它自己,所以需要一个变量进行前后对比。
string forbing;
//点击解除绑定
private void button2_Click(object sender, EventArgs e)
{
if(button2.Text.Equals("解除绑定"))
{
forbing = textBox6.Text;
//可以更改了
textBox6.ReadOnly = false;
textBox7.ReadOnly = false;
textBox8.ReadOnly = false;
button2.Text = "绑定";
}
else//下面一大段代码是从bind里面复制过来的相同内容
{
//连接数据库
string connString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=123456";//数据库连接字符串
SqlConnection conn = new SqlConnection(connString);
conn.Open();
//检查是否存在要绑定得这个学号
string sql = "select * from Student where Sno = '" + textBox6.Text.Trim() + "' and Sname = '"+textBox7.Text.Trim()+"' and Sage = '"+textBox8.Text.Trim()+"'";
SqlCommand sqlCommand = new SqlCommand(sql, conn);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.show("绑定失败!学生信息有误!");
return;
}
sqlDataReader.Close();
//这个要绑定的学号就是之前那个
if (textBox6.Text.Equals(forbing))
{
}
else
{
//检查是否被绑定过
sql = "select UserBindingID from SysUser where UserBindingID = '" + textBox6.Text.Trim() + "'";
sqlCommand = new SqlCommand(sql, conn);
sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
m = new MessageBox();
timer1.Enabled = true;
m.show("绑定失败!该学号已被绑定!");
return;
}
sqlDataReader.Close();
sql = "update SysUser set UserBindingID = '" + textBox6.Text.Trim() + "' where UserID = '" + Login.getusername() + "'";
sqlCommand = new SqlCommand(sql, conn);
sqlDataReader = sqlCommand.ExecuteReader();
}
m = new MessageBox();
timer1.Enabled = true;
m.show("绑定成功!");
textBox6.ReadOnly = true;
textBox7.ReadOnly = true;
textBox8.ReadOnly = true;
button2.Text = "解除绑定";
conn.Close();
}
}
//点击修改头像
public Byte[] mybyte = new byte[0];
private void button3_Click(object sender, EventArgs e)
{
try
{
//打开浏览图片对话框
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.ShowDialog();
string picturePath = openFileDialog.FileName;//获取图片路径
//文件的名称,每次必须更换图片的名称,这里很为不便
//创建FileStream对象
FileStream fs = new FileStream(picturePath, FileMode.Open, FileAccess.Read);
//声明Byte数组
mybyte = new byte[fs.Length];
//读取数据
fs.Read(mybyte, 0, mybyte.Length);
pictureBox2.Image = Image.FromStream(fs);
fs.Close();
}
catch
{
}
}
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 AlterInfroStu_Load(object sender, EventArgs e)
{
//账号
textBox1.Text = Login.getusername().Trim();
textBox1.ReadOnly = true;
string connString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=123456";//数据库连接字符串
SqlConnection connection = new SqlConnection(connString);//创建connection对象
//把这个学生原本的信息都显示出来
string sql = "select * from SysUser where UserID = '" + Login.getusername() + "'";
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataTable dt = new DataTable();
da.Fill(dt);
textBox4.Text = dt.Rows[0][3].ToString().Trim();
textBox5.Text = dt.Rows[0][4].ToString().Trim();
//tostring转换成datetime
dateTimePicker1.Value = DateTime.Parse(dt.Rows[0][5].ToString());
//头像
sql = "select UserPhoto from SysUser where UserID = '" + Login.getusername() + "'";
SqlDataAdapter da2 = new SqlDataAdapter(sql, connection);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
MemoryStream ms = new MemoryStream((byte[])dt2.Rows[0][0]);
Bitmap bmpt = new Bitmap(ms);
this.pictureBox2.Image = bmpt;
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
//绑定学号
sql = "select Sno,Sname,Sage from Student where Sno = (select UserBindingID from SysUser where UserID ='" + Login.getusername() + "')";
SqlDataAdapter da3 = new SqlDataAdapter(sql, connection);
DataTable dt3 = new DataTable();
da3.Fill(dt3);
textBox6.Text = dt3.Rows[0][0].ToString().Trim();
textBox7.Text = dt3.Rows[0][1].ToString().Trim();
textBox8.Text = dt3.Rows[0][2].ToString().Trim();
textBox6.ReadOnly = true;
textBox7.ReadOnly = true;
textBox8.ReadOnly = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
m.Close();
timer1.Enabled = false;
}
//点击修改按钮
private void button1_Click(object sender, EventArgs e)
{
string connString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=123456";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
//必须绑定学号,改也得改完了
if (button2.Text.Equals("绑定"))
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("请绑定学号!");
return;
}
else
{
//学校编号和电话不许填空
if(textBox4.Text==null || textBox5.Text ==null)
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("学校编号和电话不能为空!");
return;
}
else
{
//密码三行都被填过了
if (textBox9.Text != "密码" && textBox2.Text != "新密码" && textBox3.Text != "确认密码")
{
string password = EncryptWithMD5(textBox9.Text); //md5
string sql = "select * from SysUser where UserID = '" + Login.getusername() + "' and UserPassWord = '" + password + "'";
SqlCommand sqlCommand = new SqlCommand(sql, conn);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
//这个账户的密码输入正确
if (sqlDataReader.HasRows)
{
if (textBox2.Text == textBox3.Text)
{
Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");
if (regex.IsMatch(textBox2.Text))//判断格式是否符合要求
{
//MessageBox.Show("输入密码格式正确!");
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("密码至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符!");
return;
}
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("两次密码不一致!");
return;
}
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("原密码输入不正确,无法更改!");
return;
}
sqlDataReader.Close();
string passwordnew = EncryptWithMD5(textBox2.Text).ToString();
string sql2 = "update SysUser set UserPassWord = '" + passwordnew + "' where UserID = '" + Login.getusername() + "'";
SqlCommand sqlCommand2 = new SqlCommand(sql2, conn);
SqlDataReader sqlDataReader2 = sqlCommand2.ExecuteReader();
sqlDataReader2.Close();
}
//将学校编号啥的修改了
string sql3 = "update SysUser set UserSchoolID = '" + textBox4.Text + "',UserMobile = '" + textBox5.Text + "',UserBirthday = '" + dateTimePicker1.Value + "' where UserID = '" + Login.getusername() + "'";
SqlCommand sqlCommand3 = new SqlCommand(sql3, conn);
SqlDataReader sqlDataReader3 = sqlCommand3.ExecuteReader();
sqlDataReader3.Close();
if(mybyte.Length==0)
{
}
else
{
//将头像修改了
sql3 = "update SysUser set UserPhoto = @userphoto where UserID = '" + Login.getusername() + "'";
SqlCommand command = new SqlCommand(sql3, conn);
SqlParameter sqlParameter = new SqlParameter("@userphoto", SqlDbType.VarBinary, mybyte.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, mybyte);
command.Parameters.Add(sqlParameter);
command.ExecuteNonQuery();
}
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("修改成功!");
conn.Close();
}
}
}
//点击原密码
private void textBox9_Click(object sender, EventArgs e)
{
if(textBox9.Text.Equals("原密码"))
textBox9.Text = "";
textBox9.PasswordChar = '*';
}
//离开原密码
private void textBox9_Leave(object sender, EventArgs e)
{
if (textBox9.Text.Equals(""))
{
textBox9.PasswordChar = '\0';
textBox9.Text = "原密码";
}
}
//新密码
private void textBox2_Click(object sender, EventArgs e)
{
if (textBox2.Text.Equals("密码"))
textBox2.Text = "";
textBox2.PasswordChar = '*';
}
//新密码
private void textBox2_Leave(object sender, EventArgs e)
{
if (textBox2.Text.Equals(""))
{
textBox2.PasswordChar = '\0';
textBox2.Text = "密码";
}
}
//确认密码
private void textBox3_Click(object sender, EventArgs e)
{
if (textBox3.Text.Equals("确认密码"))
textBox3.Text = "";
textBox3.PasswordChar = '*';
}
//确认密码
private void textBox3_Leave(object sender, EventArgs e)
{
if (textBox3.Text.Equals(""))
{
textBox3.Text = "确认密码";
textBox3.PasswordChar = '\0';
}
}
}
AlterInforMan窗口:(功能和上边那个相似,放到一起了,其实是一样的,可以选择不看)
代码:
public partial class AlterInforMan : Form
{
public AlterInforMan()
{
InitializeComponent();
}
//点击修改头像
public Byte[] mybyte = new byte[0];
private void button3_Click(object sender, EventArgs e)
{
//打开浏览图片对话框
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.ShowDialog();
string picturePath = openFileDialog.FileName;//获取图片路径
//文件的名称,每次必须更换图片的名称,这里很为不便
//创建FileStream对象
FileStream fs = new FileStream(picturePath, FileMode.Open, FileAccess.Read);
//声明Byte数组
mybyte = new byte[fs.Length];
//读取数据
fs.Read(mybyte, 0, mybyte.Length);
pictureBox2.Image = Image.FromStream(fs);
fs.Close();
}
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 AlterInforMan_Load(object sender, EventArgs e)
{
//将数据提取出来
textBox1.Text = Login.getusername().Trim();
textBox1.ReadOnly = true;
string connString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=123456";//数据库连接字符串
SqlConnection connection = new SqlConnection(connString);//创建connection对象
string sql = "select * from SysManage where UserID = '" + Login.getusername() + "'";
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataTable dt = new DataTable();
da.Fill(dt);
//将原信息都显示出来
textBox4.Text = dt.Rows[0][2].ToString().Trim();
textBox5.Text = dt.Rows[0][3].ToString().Trim();
//tostring转换成datatime
dateTimePicker1.Value = DateTime.Parse(dt.Rows[0][4].ToString());
//头像
sql = "select UserPhoto from SysManage where UserID = '" + Login.getusername() + "'";
SqlDataAdapter da2 = new SqlDataAdapter(sql, connection);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
MemoryStream ms = new MemoryStream((byte[])dt2.Rows[0][0]);
Bitmap bmpt = new Bitmap(ms);
this.pictureBox2.Image = bmpt;
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
}
//点击原密码
private void textBox9_Click(object sender, EventArgs e)
{
if (textBox9.Text.Equals("原密码"))
textBox9.Text = "";
textBox9.PasswordChar = '*';
}
//离开原密码
private void textBox9_Leave(object sender, EventArgs e)
{
if (textBox9.Text.Equals(""))
{
textBox9.PasswordChar = '\0';
textBox9.Text = "原密码";
}
}
//新密码
private void textBox2_Click(object sender, EventArgs e)
{
if (textBox2.Text.Equals("密码"))
textBox2.Text = "";
textBox2.PasswordChar = '*';
}
//新密码
private void textBox2_Leave(object sender, EventArgs e)
{
if (textBox2.Text.Equals(""))
{
textBox2.PasswordChar = '\0';
textBox2.Text = "密码";
}
}
private void textBox3_Click(object sender, EventArgs e)
{
if (textBox3.Text.Equals("确认密码"))
textBox3.Text = "";
textBox3.PasswordChar = '*';
}
//确认密码
private void textBox3_Leave(object sender, EventArgs e)
{
if (textBox3.Text.Equals(""))
{
textBox3.Text = "确认密码";
textBox3.PasswordChar = '\0';
}
}
//点击修改
private void button1_Click(object sender, EventArgs e)
{
string connString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=123456";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
//学校编号和电话不许填空
if (textBox4.Text == null || textBox5.Text == null)
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("学校编号和电话不能为空!");
return;
}
else
{
//密码三行都被填过了
if (textBox9.Text != "密码" && textBox2.Text != "新密码" && textBox3.Text != "确认密码")
{
string password = EncryptWithMD5(textBox9.Text); //md5
string sql = "select * from SysManage where UserID = '" + Login.getusername() + "' and UserPassWord = '" + password + "'";
SqlCommand sqlCommand = new SqlCommand(sql, conn);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
//这个账户的密码输入正确
if (sqlDataReader.HasRows)
{
if (textBox2.Text == textBox3.Text)
{
Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");
if (regex.IsMatch(textBox2.Text))//判断格式是否符合要求
{
//MessageBox.Show("输入密码格式正确!");
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("密码至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符!");
return;
}
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("两次密码不一致!");
return;
}
}
else
{
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("原密码输入不正确,无法更改!");
return;
}
sqlDataReader.Close();
string passwordnew = EncryptWithMD5(textBox2.Text);
string sql2 = "update SysManage set UserPassWord = '" + passwordnew + "' where UserID = '" + Login.getusername() + "'";
SqlCommand sqlCommand2 = new SqlCommand(sql2, conn);
SqlDataReader sqlDataReader2 = sqlCommand2.ExecuteReader();
sqlDataReader2.Close();
}
string sql3 = "update SysManage set UserSchoolID = '" + textBox4.Text + "',UserMobile = '" + textBox5.Text + "',UserBirthday = '" + dateTimePicker1.Value + "' where UserID = '" + Login.getusername() + "'";
SqlCommand sqlCommand3 = new SqlCommand(sql3, conn);
SqlDataReader sqlDataReader3 = sqlCommand3.ExecuteReader();
sqlDataReader3.Close();
//将头像修改了
sql3 = "update SysManage set UserPhoto = @userphoto where UserID = '" + Login.getusername() + "'";
SqlCommand command = new SqlCommand(sql3, conn);
SqlParameter sqlParameter = new SqlParameter("@userphoto", SqlDbType.VarBinary, mybyte.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, mybyte);
command.Parameters.Add(sqlParameter);
command.ExecuteNonQuery();
m = new MessageBox();
timer1.Enabled = true;
m.showDialog("修改成功!");
conn.Close();
}
}
}