数据库实验4:稍微复杂的设计页面(MD5)(c#)
廢話不多說,我們現在開始,老生常談的話題建立項目以及設計初始化的頁面我就不說了,詳細不會了解之前博客,我們先放出來頁面的設計。
登錄頁面:
注冊頁面:
主頁面:
首先我們先來運行一波
先注冊
注冊
登錄
主頁面
按refresh
按Show photo
開始我們的代碼篇
首先,因為我們這個體系是跟數據庫相連接的,所以我們要定義一個數據庫
定義好數據庫,開始我們的代碼篇
首先一個新的知識,關于鼠標離開這個文本框,我們定義格式是否與我們相同,我們定義提醒框是,函數是在這里找,點擊文本框,右邊的屬性中找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.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);//創建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;//獲取圖片路徑//文件的名稱,每次必須更換圖片的名稱,這里很為不便//創建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);//創建connection對象//打開數據庫連接connection.Open();//創建SQL語句string sql = "select UserPhoto from SysUser where UserID = '" + 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);pictureBox1.Image = Image.FromStream(ms);}elsepictureBox1.Image = null;connection.Close();}catch (Exception ex){MessageBox.Show(ex.Message);}}} }主要內容就是以上,說來也慚愧,周一講的這些內容,被我用在周三的實驗課上寫,而且小毛病出現了不少,我用VS次數還是太少,我關了一些東西,我不知道從哪里打開,老師上課講的直接點擊數據源,向里面導表,也不知道我是之前關了,還是我沒有,我找不到在哪里顯示。我就一個一個導入表,導入表的時候,我的登陸表他只顯示一列,不知道這是什么原因,最后原因沒找到,又重新寫了一遍,就在我以為所有的事情大功告成的時候,我再次運行的時候說我數據庫沒鏈接成功,我鏈接后,也不知道我是點擊了哪里,我在SQL SERVER中建立的數據庫沒了,我說重新建的時候可能我之前沒清理干凈又說我不能減,我又視圖關軟件開軟件,關機開機操作了一波,結果并沒有什么卵用,最后用了DROP DATABASE 數據庫名,刪干凈后又重新建立了一個,最后在寫完一篇代碼。
對于大作業,我還沒有開始設想,可能會晚點,今天先到這里。
總結
以上是生活随笔為你收集整理的数据库实验4:稍微复杂的设计页面(MD5)(c#)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言时钟信号设计原理,单片机时钟电路原
- 下一篇: Active Directory 域滲透