軟件:SQL Server;Visual Studio
語言:C#,SQL
兩個身份,管理員和學生。
管理員功能:管理學生專業信息、課程信息、選課信息(增刪改查),查看已註冊過的同窗信息(密碼不可見,是亂碼)以及照片。
學生功能:註冊本身的信息,查看本身的信息包括專業信息、註冊時的信息、選課及成績,修改本身的密碼。
git
在SQL Server建立數據庫,在新數據庫中新建須要用的表並添加數據。github
create database curricula_variable_system;//建立數據庫 USE curricula_variable_system; //建表,記錄註冊信息的 CREATE TABLE SysUser ( UserID NCHAR(20) , UserPassWord NCHAR(32) , /*密碼32位加密*/ UserSchoolID NCHAR(20) PRIMARY KEY, UserMobile NCHAR(11), UserBirthday datetime, UserIdentity NCHAR(20), UserPhoto image ); //建表,記錄登陸信息的 CREATE TABLE SysLog ( UserID NCHAR(20) , DateAndTime datetime, UserOperation NCHAR(200) ); //建管理員表,存管理員的帳號密碼 CREATE TABLE Teacher ( UserID NCHAR(20) , UserPassWord NCHAR(32) , /*密碼32位加密*/ ); //建學生表 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); 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' 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); //新建觸發器 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)
剛開始的登陸頁面
點擊按鈕顯示新的窗體,這是其中一個按鈕的代碼。
正則表達式
Form2 form2 = new Form2();//新建窗體 form2.Show();//顯示新建窗體 this.Hide();//隱藏當前窗體
肯定登陸
sql
string username = textBoxtea.Text.Trim(); //取出帳號 string password = EncryptWithMD5(textBoxcher.Text.Trim()); //取出密碼並加密 string myConnString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=sql";//鏈接數據庫 SqlConnection sqlConnection = new SqlConnection(myConnString); //實例化鏈接對象 sqlConnection.Open(); string sql = "select UserID,UserPassWord from Teacher where UserID = '" + username + "' and UserPassWord = '" + password + "'"; //教工號:201210,密碼:123 //編寫SQL命令 SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); if (sqlDataReader.HasRows && textBoxyan.Text == code) { MessageBox.Show("歡迎使用!"); //登陸成功 Form6 form6 = new Form6(); form6.Show(); this.Hide(); } else { MessageBox.Show("登陸失敗!"); return; } sqlDataReader.Close(); sqlConnection.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(); }
驗證碼
點擊窗體|在事件裏找Load|雙擊,而後輸入如下代碼
數據結構
public string 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;
查看照片,根據學號查看
dom
try { string connString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=sql";//數據庫鏈接字符串 SqlConnection connection = new SqlConnection(connString);//建立connection對象 //打開數據庫鏈接 connection.Open(); //建立SQL語句 string sql = "select UserPhoto from SysUser where UserSchoolID = '" + textBox1.Text + "'"; //建立SqlCommand對象 SqlCommand command = new SqlCommand(sql, connection); //建立DataAdapter對象 SqlDataAdapter dataAdapter = new SqlDataAdapter(command); //建立DataSet對象 DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet, "SysUser"); int c = dataSet.Tables["SysUser"].Rows.Count; if (c > 0) { Byte[] mybyte = new byte[0]; mybyte = (Byte[])(dataSet.Tables["SysUser"].Rows[c - 1]["UserPhoto"]); MemoryStream ms = new MemoryStream(mybyte); pictureBox2.Image = Image.FromStream(ms); } else { pictureBox2.Image = null; MessageBox.Show("無照片"); } connection.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); }
返回上一界面ide
Form6 form6 = new Form6();//上一界面的窗體 form6.Show();//顯示 this.Hide();//隱藏當前窗體
我對性別的填寫進行了限定只能是」男「或「女」。學號也限定是201215開頭再加三位數字。
SQL語句以下
ui
alter table Student add constraint c1 check(Sno between 201215000 and 201215999) alter table Student add constraint c2 check(Ssex IN('男','女'))
刪除this
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=sql");//定義 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(); } catch { MessageBox.Show("請正確選擇行!"); } finally { con.Dispose(); } this.studentTableAdapter.Fill(this.curricula_variable_systemDataSet3.Student);//Form10_Load裏的那條代碼
修改,根據學號修改姓名
String StuID = textBox1.Text.Trim(); String StuName = textBox2.Text.Trim(); SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=sql");//定義 try { con.Open(); string insertStr = "UPDATE Student SET Sname = '" + StuName + "' WHERE Sno = '" + StuID + "'"; SqlCommand cmd = new SqlCommand(insertStr, con); cmd.ExecuteNonQuery(); } catch { MessageBox.Show("輸入數據違反要求!"); } finally { con.Dispose(); } this.studentTableAdapter.Fill(this.curricula_variable_systemDataSet3.Student);//Form10_Load裏的那條代碼
查詢,根據學號
String StuID = textBox1.Text.Trim(); String conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=sql"; SqlConnection sqlConnection = new SqlConnection(conn); //實例化鏈接對象 try { sqlConnection.Open(); String select_by_id = "select * from Student where Sno='" + StuID + "'"; SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); BindingSource bindingSource = new BindingSource(); bindingSource.DataSource = sqlDataReader; dataGridView1.DataSource = bindingSource; } catch { MessageBox.Show("查詢語句有誤,請認真檢查SQL語句!"); } finally { sqlConnection.Close(); }
清空文本行
textBox1.Text = null; textBox2.Text = null; textBox3.Text = null; textBox4.Text = null; textBox5.Text = null;
添加課程
string Coucno = textBox1.Text.Trim(); string Couname = textBox2.Text.Trim(); string Coucredit = textBox3.Text.Trim(); string Coupno = textBox4.Text.Trim(); SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=sql");//定義 try { con.Open();//打開 string insertStr = "INSERT INTO Course (Cno,Cname,Cpno,Ccredit) " + "VALUES ('" + Coucno + "','" + Couname + "','" + Coupno + "','" + Coucredit + "')"; SqlCommand cmd = new SqlCommand(insertStr, con);//使用 cmd.ExecuteNonQuery(); } catch { MessageBox.Show("輸入數據違反要求!"); } finally { con.Dispose();//釋放 } this.courseTableAdapter.Fill(this.curricula_variable_systemDataSet4.Course);
刪除
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=sql");//定義 try { con.Open(); string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//選擇的當前行第一列的值,也就是Cno那列 string delete_by_id = "delete from Course where Cno=" + select_id;//sql刪除語句 SqlCommand cmd = new SqlCommand(delete_by_id, con); cmd.ExecuteNonQuery(); } catch { MessageBox.Show("請正確選擇行!"); } finally { con.Dispose(); } this.courseTableAdapter.Fill(this.curricula_variable_systemDataSet4.Course);
修改,根據課程號修改課程名
string Cno = textBox1.Text.Trim(); string Cname = textBox2.Text.Trim(); SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=sql");//定義 try { con.Open(); string insertStr = "UPDATE Course SET Cname = '" + Cname + "' WHERE Cno = '" + Cno + "'"; SqlCommand cmd = new SqlCommand(insertStr, con); cmd.ExecuteNonQuery(); } catch { MessageBox.Show("輸入數據違反要求!"); } finally { con.Dispose(); } this.courseTableAdapter.Fill(this.curricula_variable_systemDataSet4.Course);
查詢,根據課程號
string Cno = textBox1.Text.Trim(); String conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=sql"; SqlConnection sqlConnection = new SqlConnection(conn); //實例化鏈接對象 try { sqlConnection.Open(); String select_by_id = "select * from Course where Cno='" + Cno + "'"; SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); BindingSource bindingSource = new BindingSource(); bindingSource.DataSource = sqlDataReader; dataGridView1.DataSource = bindingSource; } catch { MessageBox.Show("查詢語句有誤,請認真檢查SQL語句!"); } finally { sqlConnection.Close(); }
查詢
string StuID = textBox1.Text.Trim(); String conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=sql"; SqlConnection sqlConnection = new SqlConnection(conn); //實例化鏈接對象 try { sqlConnection.Open(); String select_by_id = "select * from SC where Sno='" + StuID + "'"; SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); BindingSource bindingSource = new BindingSource(); bindingSource.DataSource = sqlDataReader; dataGridView1.DataSource = bindingSource; } catch { MessageBox.Show("查詢語句有誤,請認真檢查SQL語句!"); } finally { sqlConnection.Close(); }
修改,根據學號、課程號修改爲績
string StuID = textBox1.Text.Trim(); string Cno = textBox2.Text.Trim(); string Grade = textBox3.Text.Trim(); SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=sql");//定義 try { con.Open(); string insertStr = "UPDATE SC SET Grade = '" + Grade + "' WHERE Cno = '" + Cno + "'AND Sno='"+ StuID+"'"; SqlCommand cmd = new SqlCommand(insertStr, con); cmd.ExecuteNonQuery(); } catch { MessageBox.Show("輸入數據違反要求!"); } finally { con.Dispose(); } this.sCTableAdapter2.Fill(this.curricula_variable_systemDataSet7.SC);
添加
string StuID = textBox1.Text.Trim(); string Cno = textBox2.Text.Trim(); string Grade = textBox3.Text.Trim(); SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=sql");//定義 try { string stu = "select Sno from Student where Sno='" + StuID + "'"; if (stu != "") { con.Open();//打開 string insertStr = "INSERT INTO SC (Sno,Cno,Grade) " + "VALUES ('" + StuID + "','" + Cno + "','" + Grade + "')"; SqlCommand cmd = new SqlCommand(insertStr, con);//使用 cmd.ExecuteNonQuery(); } else { MessageBox.Show("沒有該學生!請從新輸入"); } } catch { MessageBox.Show("輸入數據違反要求!");//新加的學號在已有學號中,課程號在已有的課程中,成績在0到100之間 } finally { con.Dispose();//釋放 } this.sCTableAdapter2.Fill(this.curricula_variable_systemDataSet7.SC);
刪除,輸入學號、課程號刪除對應行。由於選課表是學號課程號一塊兒做爲主碼的因此和前邊的刪除方式不一樣。
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=sql");//定義 try { con.Open(); string StuID = textBox1.Text.Trim(); string Cno = textBox2.Text.Trim(); string delete_by_stc= "delete from SC where Sno='"+ textBox1.Text + "' and Cno='" + textBox2.Text + "'"; SqlCommand cmd = new SqlCommand(delete_by_stc, con); cmd.ExecuteNonQuery(); } catch { MessageBox.Show("請正確選擇行!"); } finally { con.Dispose(); } this.sCTableAdapter2.Fill(this.curricula_variable_systemDataSet7.SC);
查看專業信息(查看專業信息代碼相似,就是SQL語句那改爲SC表)
String conn = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=sql"; SqlConnection sqlConnection = new SqlConnection(conn); //實例化鏈接對象 String StuID = textBox1.Text.Trim(); if(textBox1.Text=="") { MessageBox.Show("請先輸入學號!"); return; } else { try { sqlConnection.Open(); String select_by_id = "select * from Student where Sno='" + StuID + "'"; SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); BindingSource bindingSource = new BindingSource(); bindingSource.DataSource = sqlDataReader; dataGridView1.DataSource = bindingSource; } catch { MessageBox.Show("查詢語句有誤,請認真檢查SQL語句!"); } finally { sqlConnection.Close(); } }
查看我的信息
String StuID = textBox1.Text.Trim(); String conn1 = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=sql"; SqlConnection sqlConnection1 = new SqlConnection(conn1); //實例化鏈接對象 if (textBox1.Text == "") { MessageBox.Show("請先輸入學號!"); return; } else { try { sqlConnection1.Open(); String select_by_id1 = "select UserID,UserSchoolID,UserMobile,UserBirthday,UserIdentity from SysUser where UserSchoolID='" + StuID + "'"; SqlCommand sqlCommand = new SqlCommand(select_by_id1, sqlConnection1); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); BindingSource bindingSource = new BindingSource(); bindingSource.DataSource = sqlDataReader; dataGridView1.DataSource = bindingSource; } catch { MessageBox.Show("查詢語句有誤,請認真檢查SQL語句!"); } finally { sqlConnection1.Close(); } }
查看照片
if (textBox1.Text == "") { MessageBox.Show("請先輸入學號!"); return; } else { try { string connString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=sql";//數據庫鏈接字符串 SqlConnection connection = new SqlConnection(connString);//建立connection對象 //打開數據庫鏈接 connection.Open(); //建立SQL語句 string sql = "select UserPhoto from SysUser where UserSchoolID = '" + textBox1.Text + "'"; //建立SqlCommand對象 SqlCommand command = new SqlCommand(sql, connection); //建立DataAdapter對象 SqlDataAdapter dataAdapter = new SqlDataAdapter(command); //建立DataSet對象 DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet, "SysUser"); int c = dataSet.Tables["SysUser"].Rows.Count; if (c > 0) { Byte[] mybyte = new byte[0]; mybyte = (Byte[])(dataSet.Tables["SysUser"].Rows[c - 1]["UserPhoto"]); MemoryStream ms = new MemoryStream(mybyte); pictureBox2.Image = Image.FromStream(ms); } else pictureBox2.Image = null; connection.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
修改密碼
肯定修改,使用正則表達式約束新密碼的格式
if (textBox3.Text == "") { MessageBox.Show("學號不能爲空!"); } if(textBox1.Text=="") { MessageBox.Show("新密碼不能爲空!"); } if (textBox2.Text == "") { MessageBox.Show("確認密碼不能爲空!"); } if(textBox1.Text.Trim()!="")//新密碼不爲空時,輸入知足正則表達式 { //使用regex(正則表達式)進行格式設置 至少有數字、大寫字母、小寫字母各一個。最少3個字符、最長20個字符。 Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}"); if (regex.IsMatch(textBox1.Text))//判斷格式是否符合要求 { //MessageBox.Show("輸入密碼格式正確!"); } else { MessageBox.Show("至少有數字、大寫字母、小寫字母各一個。最少3個字符、最長20個字符!"); return; } } if (textBox1.Text == textBox2.Text) { string sql = "update SysUser set UserPassWord='"+ EncryptWithMD5(textBox1.Text)+"' where UserSchoolID='"+ textBox3.Text.Trim()+"'"; string connString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=sql"; SqlConnection con = new SqlConnection(connString);//建立connection對象 con.Open(); SqlCommand command = new SqlCommand(sql, con); command.ExecuteNonQuery(); MessageBox.Show("新密碼已經修改完成"); con.Close(); } else { MessageBox.Show("請輸入兩次相同的密碼"); }
對密碼加密
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(); }
主要代碼都在上面了,參考的時候結合本身的稍加改動就能夠,背景圖片是pictureBox組件而後選本身喜歡的照片就能夠了,注意一下大小模式這裏,選這個圖片才顯示完整。
全部代碼我壓縮放在github上了,須要的能夠下載而後在Visual studio打開看一下點這裏
視頻講解:點這裏