日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

数据库实验4:稍微复杂的设计页面(MD5)(c#)

發(fā)布時間:2024/3/26 C# 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据库实验4:稍微复杂的设计页面(MD5)(c#) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

廢話不多說,我們現在開始,老生常談的話題建立項目以及設計初始化的頁面我就不說了,詳細不會了解之前博客,我們先放出來頁面的設計。
登錄頁面:
注冊頁面:

主頁面:
首先我們先來運行一波
先注冊
注冊
登錄
主頁面
按refresh
按Show photo
開始我們的代碼篇
首先,因為我們這個體系是跟數據庫相連接的,所以我們要定義一個數據庫

--Edit by David @ HeBei University 2019DROP 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 SysLogCREATE 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 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 SysUser VALUES ('admin','123','000','13812345678',1999-1-1,'0',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 StudentINSERT 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','信息系統(tǒng)',NULL,4); INSERT INTO Course(Cno,Cname,Cpno,Ccredit) VALUES ('4','操作系統(tǒng)',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 CourseINSERT 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 SCIF(OBJECT_ID('regist_recorder') is not null) -- 判斷名為 regist_recorder 的觸發(fā)器是否存在 DROP TRIGGER regist_recorder -- 刪除觸發(fā)器 GOCREATE TRIGGER regist_recorder ON SysUser AFTER INSERT AS declare @UserName nchar(20)declare @DateTime datetimedeclare @UserOperation nchar(200)select @UserName = system_userselect @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' endselect @UserOperation = @opINSERT INTO SysLog(UserID,DateAndTime,UserOperation)VALUES (@UserName,@DateTime,@UserOperation)

定義好數據庫,開始我們的代碼篇
首先一個新的知識,關于鼠標離開這個文本框,我們定義格式是否與我們相同,我們定義提醒框是,函數是在這里找,點擊文本框,右邊的屬性中找Leave,點擊,會出現在函數中
首先登陸頁面

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.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms;namespace Resgister_Login {public partial class login : Form{public login(){InitializeComponent();}private void label1_Click(object sender, EventArgs e){}private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) //轉化頁面{resgister res = new resgister();res.ShowDialog();}public string code;private void button1_Click(object sender, EventArgs e){string username = textBox1.Text.Trim(); //取出賬號string password = EncryptWithMD5(textBox2.Text.Trim()); //取出密碼并加密if (username == "admin")password = "123";//測試用例,便于初始化時候的 admin 密碼 123可以順利登陸。程序完成后可注釋掉這行代碼。//string connstr = ConfigurationManager.ConnectionStrings["connectionString"].ToString(); //讀取連接字符串string myConnString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=******";SqlConnection sqlConnection = new SqlConnection(myConnString); //實例化連接對象sqlConnection.Open();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){MessageBox.Show("歡迎使用!"); //登錄成功Main form2 = new Main();form2.Show();this.Hide();}else{MessageBox.Show("登錄失敗!");return;}sqlDataReader.Close();sql = "insert into SysLog values ( '" + username + "' , '" + DateTime.Now + "' , '" + "Login" + "')"; //編寫SQL命令sqlCommand = new SqlCommand(sql, sqlConnection);sqlCommand.ExecuteNonQuery();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();}private void button2_Click(object sender, EventArgs e){this.Close();}private void label5_Click(object sender, EventArgs e){}private void login_Load_1(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));elsecode1 = (char)('A' + (char)(number % 26)); //轉化為字符 this.code += code1.ToString();}label5.Text = code;}private void textBox1_Leave(object sender, EventArgs e){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個字符!");textBox1.Focus();}}else{MessageBox.Show("Please fill in the full information!");}}} }

注冊

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; using System.IO; using System.Security.Cryptography;namespace Resgister_Login {public partial class resgister : Form{public resgister(){InitializeComponent();}private void label2_Click(object sender, EventArgs e){}private void label5_Click(object sender, EventArgs e){}private void label6_Click(object sender, EventArgs e){}private void resgister_Load(object sender, EventArgs e){}public Byte[] mybyte = new byte[0];private void button2_Click(object sender, EventArgs e){try{string connString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=******";//數據庫連接字符串SqlConnection connection = new SqlConnection(connString);//創(chuàng)建connection對象string sql = "insert into SysUser (UserID, UserPassWord , UserSchoolID, UserMobile, UserBirthday , UserIdentity , UserPhoto ) " +"values (@userid, @userpassword,@userschoolid,@usermobile,@userbirthday,@useridentity,@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("@useridentity", comboBox1.Text);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();MessageBox.Show("register succeed");}catch (Exception ex){MessageBox.Show(ex.Message);}this.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();}private void button1_Click(object sender, EventArgs e){//打開瀏覽圖片對話框OpenFileDialog openFileDialog = new OpenFileDialog();openFileDialog.ShowDialog();string picturePath = openFileDialog.FileName;//獲取圖片路徑//文件的名稱,每次必須更換圖片的名稱,這里很為不便//創(chuàng)建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();}private void button3_Click(object sender, EventArgs e){this.Close();}private void resgister_FormClosed(object sender, FormClosedEventArgs e){}private void button2_Leave(object sender, EventArgs e){}private void comboBox1_SelectedIndexChanged(object sender, EventArgs e){}} }

主頁面

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 Resgister_Login {public partial class Main : Form{public Main(){InitializeComponent();}private void Main_Load(object sender, EventArgs e){// TODO: 這行代碼將數據加載到表“curricula_variable_systemDataSet14.SC”中。您可以根據需要移動或刪除它。this.sCTableAdapter.Fill(this.curricula_variable_systemDataSet14.SC);// TODO: 這行代碼將數據加載到表“curricula_variable_systemDataSet13.Course”中。您可以根據需要移動或刪除它。this.courseTableAdapter.Fill(this.curricula_variable_systemDataSet13.Course);// TODO: 這行代碼將數據加載到表“curricula_variable_systemDataSet12.Student”中。您可以根據需要移動或刪除它。this.studentTableAdapter.Fill(this.curricula_variable_systemDataSet12.Student);// TODO: 這行代碼將數據加載到表“curricula_variable_systemDataSet11.SysLog”中。您可以根據需要移動或刪除它。this.sysLogTableAdapter.Fill(this.curricula_variable_systemDataSet11.SysLog);// TODO: 這行代碼將數據加載到表“curricula_variable_systemDataSet.SysUser”中。您可以根據需要移動或刪除它。this.sysUserTableAdapter.Fill(this.curricula_variable_systemDataSet.SysUser);}private void button1_Click(object sender, EventArgs e){this.sysLogTableAdapter.Fill(this.curricula_variable_systemDataSet11.SysLog);}private void button3_Click(object sender, EventArgs e){Application.Exit();}private void button2_Click(object sender, EventArgs e){try{string connString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Passwor=******";//數據庫連接字符串SqlConnection connection = new SqlConnection(connString);//創(chuàng)建connection對象//打開數據庫連接connection.Open();//創(chuàng)建SQL語句string sql = "select UserPhoto from SysUser where UserID = '" + textBox1.Text + "'";//創(chuàng)建SqlCommand對象SqlCommand command = new SqlCommand(sql, connection);//創(chuàng)建DataAdapter對象SqlDataAdapter dataAdapter = new SqlDataAdapter(command);//創(chuàng)建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);pictureBox1.Image = Image.FromStream(ms);}elsepictureBox1.Image = null;connection.Close();}catch (Exception ex){MessageBox.Show(ex.Message);}}} }

主要內容就是以上,說來也慚愧,周一講的這些內容,被我用在周三的實驗課上寫,而且小毛病出現了不少,我用VS次數還是太少,我關了一些東西,我不知道從哪里打開,老師上課講的直接點擊數據源,向里面導表,也不知道我是之前關了,還是我沒有,我找不到在哪里顯示。我就一個一個導入表,導入表的時候,我的登陸表他只顯示一列,不知道這是什么原因,最后原因沒找到,又重新寫了一遍,就在我以為所有的事情大功告成的時候,我再次運行的時候說我數據庫沒鏈接成功,我鏈接后,也不知道我是點擊了哪里,我在SQL SERVER中建立的數據庫沒了,我說重新建的時候可能我之前沒清理干凈又說我不能減,我又視圖關軟件開軟件,關機開機操作了一波,結果并沒有什么卵用,最后用了DROP DATABASE 數據庫名,刪干凈后又重新建立了一個,最后在寫完一篇代碼。
對于大作業(yè),我還沒有開始設想,可能會晚點,今天先到這里。

總結

以上是生活随笔為你收集整理的数据库实验4:稍微复杂的设计页面(MD5)(c#)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。