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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

学生信息管理系统——代码展示

發(fā)布時間:2024/1/18 windows 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 学生信息管理系统——代码展示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

整體程序流程圖

下面從登錄窗口開始,逐個給出相應的窗體截圖和對應代碼

Login

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.Threading.Tasks; using System.Windows.Forms;namespace WindowsFormsApp1 {public partial class Login : System.Windows.Forms.Form{private string Scode;//存放驗證碼private static char[] constant ={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};//驗證碼單個字符的可能取值public Login(){InitializeComponent();this.Randomcode();}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();}public void Randomcode(){int i = 0;Scode = "";Random rand = new Random();for (i = 0; i < 4; i++){Scode += constant[rand.Next(0, 57)];}textBox1.BackColor = Color.FromArgb(rand.Next(200,225), rand.Next(200, 225), rand.Next(200, 225));textBox1.ForeColor = Color.FromArgb(rand.Next(0,100), rand.Next(0, 100), rand.Next(0, 100));textBox1.Text = Scode;i = rand.Next(1, 3);if(i==1){textBox1.TextAlign = HorizontalAlignment.Center;}if (i == 2){textBox1.TextAlign = HorizontalAlignment.Left;}if (i == 3){textBox1.TextAlign = HorizontalAlignment.Right;}}private void button1_Click(object sender, EventArgs e){string code= textBox3.Text.Trim();code=code.ToUpper();Scode = Scode.ToUpper();if (code!=Scode){MessageBox.Show("驗證碼錯誤!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);this.Randomcode();}else{string username = textBoxUserName.Text.Trim(); //取出賬號string password = EncryptWithMD5(textBoxPassWord.Text.Trim()); //取出密碼string myConnString = "Data Source=.;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=***********";SqlConnection sqlConnection = new SqlConnection(myConnString); //實例化連接 sqlConnection.Open();string sql = "select userid,password from usertable where userid = '" + username + "' and password = '" + password + "'"; //編寫SQL命令SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();if (sqlDataReader.HasRows){Program.user = username;MessageBox.Show("歡迎!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); //登錄成功sqlConnection.Close();sqlConnection = new SqlConnection(myConnString); //實例化連接 sqlConnection.Open();sql = "select userid,password from usertable where userid = '" + username + "' and useridentity = '教師'"; //編寫SQL命令sqlCommand = new SqlCommand(sql, sqlConnection);sqlDataReader = sqlCommand.ExecuteReader();if (sqlDataReader.HasRows){SQLForm sQLForm = new SQLForm();//新建一個主窗體this.Hide();DialogResult d = sQLForm.ShowDialog();//顯示主窗體if (d == DialogResult.OK){this.Visible = true;this.Randomcode();}elsethis.Close();}else{LSQLForm sQLForm = new LSQLForm();//新建一個主窗體this.Hide();DialogResult d = sQLForm.ShowDialog();//顯示主窗體if (d == DialogResult.OK){this.Visible = true;this.Randomcode();}elsethis.Close();}}else{MessageBox.Show("賬號或密碼錯誤!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);this.Randomcode();}sqlConnection.Close();//最后別忘了停止sql流程}}private void button2_Click(object sender, EventArgs e){Application.Exit();}private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e){this.Randomcode();textBox1.Text = Scode;}private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e){Register Form = new Register();//新建一個主窗體Form.Show();}} }

Register

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.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms;namespace WindowsFormsApp1 {public partial class Register : Form{private string Scode;private static char[] constant ={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};public Byte[] mybyte = new byte[0];public Register(){InitializeComponent();this.Randomcode();}public void Randomcode(){int i = 0;Scode = "";Random rand = new Random();for (i = 0; i < 4; i++){Scode += constant[rand.Next(0, 57)];}textBox6.BackColor = Color.FromArgb(rand.Next(200, 225), rand.Next(200, 225), rand.Next(200, 225));textBox6.ForeColor = Color.FromArgb(rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));textBox6.Text = Scode;i = rand.Next(1, 3);if (i == 1){textBox6.TextAlign = HorizontalAlignment.Center;}if (i == 2){textBox6.TextAlign = HorizontalAlignment.Left;}if (i == 3){textBox6.TextAlign = HorizontalAlignment.Right;}}private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e){this.Randomcode();}private void button2_Click(object sender, EventArgs e){//打開瀏覽圖片對話框OpenFileDialog openFileDialog = new OpenFileDialog();openFileDialog.ShowDialog();string picturePath = openFileDialog.FileName;//獲取圖片路徑//文件的名稱,每次必須更換圖片的名稱,這里很為不便//創(chuàng)建FileStream對象if(picturePath!=""){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){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){string code = textBox5.Text.Trim();code = code.ToUpper();Scode = Scode.ToUpper();if (code != Scode){MessageBox.Show("驗證碼錯誤!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);this.Randomcode();}else{if(label9.Text!=""|| label8.Text != ""){MessageBox.Show("用戶名或密碼格式錯誤", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);this.Randomcode();}else{if(comboBox1.Text==""){MessageBox.Show("賬戶類型不能為空", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);this.Randomcode();}else{if(pictureBox1.Image==null){MessageBox.Show("頭像不能為空", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);this.Randomcode();}else{if (textBox1.Text == ""){MessageBox.Show("電話號碼不能為空", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);this.Randomcode();}else{if (textBox4.Text == ""){MessageBox.Show("學號或教職工號不能為空", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);this.Randomcode();}else{try{string connString = "Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=***********";//數據庫連接字符串SqlConnection connection = new SqlConnection(connString);//創(chuàng)建connection對象string sql = "insert into usertable (userid, password , number, phone, birthday , useridentity , userphoto ) " +"values (@userid, @password,@number,@phone,@birthday,@useridentity,@userphoto)";SqlCommand command = new SqlCommand(sql, connection);SqlParameter sqlParameter = new SqlParameter("@userid", textBox3.Text.Trim());command.Parameters.Add(sqlParameter);sqlParameter = new SqlParameter("@password", EncryptWithMD5(textBox2.Text.Trim()));command.Parameters.Add(sqlParameter);sqlParameter = new SqlParameter("@number", textBox4.Text.Trim());command.Parameters.Add(sqlParameter);sqlParameter = new SqlParameter("@phone", textBox1.Text.Trim());command.Parameters.Add(sqlParameter);sqlParameter = new SqlParameter("@birthday", dateTimePicker1.Value);command.Parameters.Add(sqlParameter);sqlParameter = new SqlParameter("@useridentity", comboBox1.Text.Trim());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("注冊成功", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);this.Close();}catch (Exception ex){MessageBox.Show(ex.Message);}}}}}}}}private void textBox3_Leave(object sender, EventArgs e){string username = textBox3.Text.Trim();string myConnString = "Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=************";SqlConnection sqlConnection = new SqlConnection(myConnString); //實例化連接 sqlConnection.Open();string sql = "select userid from usertable where userid = '" + username + "'"; //編寫SQL命令SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();if(sqlDataReader.HasRows){label8.Text = "*該用戶名已經存在";}else{if (textBox3.Text.Trim() != ""){label8.Text = "";}else{label8.Text = "*用戶名不能為空";}//查詢該用戶名是否已經存在}sqlConnection.Close();}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))//判斷格式是否符合要求{label9.Text = "";}else{label9.Text = "*至少包含數字、大小寫字母各一個。且不超過20個字符";}}else{label9.Text = "*密碼不能為空";}}} }

LSQLForm

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;namespace WindowsFormsApp1 {public partial class LSQLForm : Form{public LSQLForm(){InitializeComponent();}private void LSQLForm_Load(object sender, EventArgs e){// TODO: 這行代碼將數據加載到表“sinformationDataSet2.Student”中。您可以根據需要移動或刪除它。this.studentTableAdapter.Fill(this.sinformationDataSet2.Student);}private void 個人信息ToolStripMenuItem_Click(object sender, EventArgs e){information Form = new information();//新建一個主窗體Form.Show();//顯示主窗體}private void 查詢ToolStripMenuItem_Click(object sender, EventArgs e){SelectForm Form = new SelectForm();//新建一個主窗體Form.ShowDialog();//顯示主窗體if (Program.sql != ""){String conn = "Data Source =.; Initial Catalog = Sinformation; Persist Security Info = True; User ID = sa; Password = **********";SqlConnection sqlConnection = new SqlConnection(conn); //實例化連接對象try{sqlConnection.Open();SqlCommand sqlCommand = new SqlCommand(Program.sql, sqlConnection);SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();BindingSource bindingSource = new BindingSource();bindingSource.DataSource = sqlDataReader;dataGridView1.DataSource = bindingSource;}catch{}finally{sqlConnection.Close();}}this.studentTableAdapter.Fill(this.sinformationDataSet2.Student);Program.sql = "";}private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e){String conn = "Data Source =.; Initial Catalog = Sinformation; Persist Security Info = True; User ID = sa; Password = *********";SqlConnection sqlConnection = new SqlConnection(conn);string sql = "select * from Student";sqlConnection.Open();SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();BindingSource bindingSource = new BindingSource();bindingSource.DataSource = sqlDataReader;dataGridView1.DataSource = bindingSource;sqlConnection.Close();}private void 修改信息ToolStripMenuItem_Click(object sender, EventArgs e){Updateinformation Form = new Updateinformation();//新建一個主窗體Form.Show();//顯示主窗體}private void 修改密碼ToolStripMenuItem_Click(object sender, EventArgs e){changepassword Form = new changepassword();Form.Show();}private void 退出登錄ToolStripMenuItem_Click(object sender, EventArgs e){this.DialogResult = DialogResult.OK;this.Close();}} }

information

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 WindowsFormsApp1 {public partial class information : Form{public information(){InitializeComponent();string str = "Data Source =.; Initial Catalog = test; Persist Security Info = True; User ID = sa; Password = ************";SqlConnection con = new SqlConnection(str);con.Open();string sql = "select * from usertable where userid='"+Program.user+"'";SqlCommand com = new SqlCommand(sql, con);SqlDataReader read = com.ExecuteReader(); //用com(變量名)點上ExecuteReader()方法,該方法的類型是SqlDataReader類型read.Read();textBox3.Text = read["userid"].ToString();//查詢列名1的數據,方法為: read(變量名)["列名"]; 該方法返回的是object類型textBox4.Text = read["number"].ToString(); //如上textBox1.Text = read["phone"].ToString();dateTimePicker1.Value = Convert.ToDateTime(read["birthday"]);textBox2.Text = read["useridentity"].ToString();con.Close();con.Open();com = new SqlCommand(sql, con);SqlDataAdapter dataAdapter = new SqlDataAdapter(com);//創(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;con.Close();}private void button1_Click(object sender, EventArgs e){this.Close();}} }

Updateinformation

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 WindowsFormsApp1 {public partial class Updateinformation : Form{private string Scode;private static char[] constant ={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};public Byte[] mybyte = new byte[0];public Updateinformation(){InitializeComponent();string str = "Data Source =.; Initial Catalog = test; Persist Security Info = True; User ID = sa; Password = **********";SqlConnection con = new SqlConnection(str);con.Open();string sql = "select * from usertable where userid='" + Program.user + "'";SqlCommand com = new SqlCommand(sql, con);SqlDataReader read = com.ExecuteReader(); //用com(變量名)點上ExecuteReader()方法,該方法的類型是SqlDataReader類型read.Read();textBox3.Text = read["userid"].ToString();//查詢列名1的數據,方法為: read(變量名)["列名"]; 該方法返回的是object類型textBox4.Text = read["number"].ToString(); //如上textBox1.Text = read["phone"].ToString();dateTimePicker1.Value = Convert.ToDateTime(read["birthday"]);textBox2.Text = read["useridentity"].ToString();con.Close();con.Open();com = new SqlCommand(sql, con);SqlDataAdapter dataAdapter = new SqlDataAdapter(com);//創(chuàng)建DataSet對象DataSet dataSet = new DataSet();dataAdapter.Fill(dataSet, "SysUser");int c = dataSet.Tables["SysUser"].Rows.Count;if (c > 0){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;con.Close();this.Randomcode();}public void Randomcode(){int i = 0;Scode = "";Random rand = new Random();for (i = 0; i < 4; i++){Scode += constant[rand.Next(0, 57)];}textBox6.BackColor = Color.FromArgb(rand.Next(200, 225), rand.Next(200, 225), rand.Next(200, 225));textBox6.ForeColor = Color.FromArgb(rand.Next(0, 100), rand.Next(0, 100), rand.Next(0, 100));textBox6.Text = Scode;i = rand.Next(1, 3);if (i == 1){textBox6.TextAlign = HorizontalAlignment.Center;}if (i == 2){textBox6.TextAlign = HorizontalAlignment.Left;}if (i == 3){textBox6.TextAlign = HorizontalAlignment.Right;}}private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e){this.Randomcode();}private void button2_Click(object sender, EventArgs e){//打開瀏覽圖片對話框OpenFileDialog openFileDialog = new OpenFileDialog();openFileDialog.ShowDialog();string picturePath = openFileDialog.FileName;//獲取圖片路徑//文件的名稱,每次必須更換圖片的名稱,這里很為不便//創(chuàng)建FileStream對象if (picturePath != ""){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 button1_Click(object sender, EventArgs e){string code = textBox5.Text.Trim();code = code.ToUpper();Scode = Scode.ToUpper();if (code != Scode){MessageBox.Show("驗證碼錯誤!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);this.Randomcode();}else{if (textBox2.Text == ""){MessageBox.Show("賬戶類型不能為空", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);this.Randomcode();}else{if (textBox1.Text == ""){MessageBox.Show("電話號碼不能為空", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);this.Randomcode();}else{if (textBox4.Text == ""){MessageBox.Show("學號或教職工號不能為空", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);this.Randomcode();}else{try{string connString = "Data Source=.;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=*********";//數據庫連接字符串SqlConnection connection = new SqlConnection(connString);//創(chuàng)建connection對象string sql = "UPDATE usertable SET number=@number,phone=@phone,birthday=@birthday,userphoto=@userphoto WHERE userid = '" + Program.user+"'";SqlCommand command = new SqlCommand(sql, connection);SqlParameter sqlParameter = new SqlParameter("@number", textBox4.Text.Trim());command.Parameters.Add(sqlParameter);sqlParameter = new SqlParameter("@phone", textBox1.Text.Trim());command.Parameters.Add(sqlParameter);sqlParameter = new SqlParameter("@birthday", 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();MessageBox.Show("修改成功");this.Close();}catch (Exception ex){MessageBox.Show(ex.Message);}}}}}}private void button3_Click(object sender, EventArgs e){this.Close();}}}

changepassword

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 WindowsFormsApp1 {public partial class changepassword : Form{public changepassword(){InitializeComponent();}private void button2_Click(object sender, EventArgs e){this.Close();}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))//判斷格式是否符合要求{label4.Text = "";}else{label4.Text = "*至少包含數字、大小寫字母各一個。且不超過20個字符";}}else{label4.Text = "*密碼不能為空";}}private void textBox3_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))//判斷格式是否符合要求{label5.Text = "";}else{label5.Text = "*至少包含數字、大小寫字母各一個。且不超過20個字符";}}else{label5.Text = "*密碼不能為空";}}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){string password1, password2, password0;password0 = textBox1.Text.Trim();password1 = textBox2.Text.Trim();password2 = textBox3.Text.Trim();if(password0== password1){MessageBox.Show("新密碼與舊密碼不能相同!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);}else{if (label4.Text != ""){MessageBox.Show("新密碼格式錯誤!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);}else{if (password1 != password2){MessageBox.Show("兩次輸入的新密碼不同!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);}else{string myConnString = "Data Source=.;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=**********";SqlConnection sqlConnection = new SqlConnection(myConnString); //實例化連接 sqlConnection.Open();password0 = EncryptWithMD5(password0);string sql = "select userid,password from usertable where userid = '" + Program.user + "' and password = '" + password0 + "'"; //編寫SQL命令SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();if (sqlDataReader.HasRows){sqlConnection.Close();password1 = EncryptWithMD5(password1);sqlConnection = new SqlConnection(myConnString);sqlConnection.Open();sql = "UPDATE usertable SET password='" + password1 + "' WHERE userid = '" + Program.user + "'";sqlCommand = new SqlCommand(sql, sqlConnection);sqlCommand.ExecuteNonQuery();MessageBox.Show("修改成功!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);this.Close();}else{MessageBox.Show("原密碼錯誤!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);}sqlConnection.Close();}}}}} }

SelectForm

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace WindowsFormsApp1 {public partial class SelectForm : Form{public SelectForm(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){string StuName = textBox2.Text.Trim();string StuNum = textBox1.Text.Trim();string StuSex = comboBox1.Text;string StuAge = textBox3.Text.Trim();string StuDept = comboBox2.Text;Program.sql += "select * from Student where";if(StuName!=""){if (Program.sql != "select * from Student where")Program.sql += " and";Program.sql += " Sname='"+ StuName+"'";}if (StuNum != ""){if (Program.sql != "select * from Student where")Program.sql += " and";Program.sql += " Sno='" + StuNum + "'";}if (StuSex != ""){if (Program.sql != "select * from Student where")Program.sql += " and";Program.sql += " Ssex='" + StuSex + "'";}if (StuAge != ""){if (Program.sql != "select * from Student where")Program.sql += " and";Program.sql += " Sage=" + StuAge;}if (StuDept != ""){if (Program.sql != "select * from Student where")Program.sql += " and";Program.sql += " Sdept='" + StuDept + "'";}if(Program.sql== "select * from Student where"){MessageBox.Show("查詢不能為空!","notice", MessageBoxButtons.OK, MessageBoxIcon.Error);}else{this.Close();}}private void button2_Click(object sender, EventArgs e){Program.sql = "";this.Close();}} }

SQLForm

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; using System.Threading.Tasks; using System.Windows.Forms;namespace WindowsFormsApp1 {public partial class SQLForm : System.Windows.Forms.Form{public SQLForm(){InitializeComponent();}private void SQLForm_Load(object sender, EventArgs e){// TODO: 這行代碼將數據加載到表“sinformationDataSet.Student”中。您可以根據需要移動或刪除它。this.studentTableAdapter.Fill(this.sinformationDataSet.Student);}private void button1_Click(object sender, EventArgs e){AddForm Form = new AddForm();//新建一個主窗體Form.ShowDialog();//顯示主窗體this.studentTableAdapter.Fill(this.sinformationDataSet.Student);}private void button2_Click(object sender, EventArgs e){SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = Sinformation; Persist Security Info = True; User ID = sa; Password = ************");try{con.Open();string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//選擇的當前行第一列的值,也就是IDstring delete_by_id = "delete from Student where Sno=" + select_id;//sql刪除語句SqlCommand cmd = new SqlCommand(delete_by_id, con);cmd.ExecuteNonQuery();}catch{MessageBox.Show("請選中要刪除的行", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);}finally{con.Close();}this.studentTableAdapter.Fill(this.sinformationDataSet.Student);}private void button3_Click(object sender, EventArgs e){try{string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//選擇的當前行第一列的值,也就是IDUpdateForm Form = new UpdateForm(select_id);//新建一個主窗體Form.ShowDialog();//顯示主窗體this.studentTableAdapter.Fill(this.sinformationDataSet.Student);}catch{MessageBox.Show("請選中要修改的行", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);}finally{}}private void button5_Click(object sender, EventArgs e){String conn = "Data Source =.; Initial Catalog = Sinformation; Persist Security Info = True; User ID = sa; Password = ***********";SqlConnection sqlConnection = new SqlConnection(conn);string sql = "select * from Student";sqlConnection.Open();SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();BindingSource bindingSource = new BindingSource();bindingSource.DataSource = sqlDataReader;dataGridView1.DataSource = bindingSource;sqlConnection.Close();}private void button4_Click(object sender, EventArgs e){SelectForm Form = new SelectForm();//新建一個主窗體Form.ShowDialog();//顯示主窗體if(Program.sql != ""){String conn = "Data Source =.; Initial Catalog = Sinformation; Persist Security Info = True; User ID = sa; Password = *********";SqlConnection sqlConnection = new SqlConnection(conn); //實例化連接對象try{sqlConnection.Open();SqlCommand sqlCommand = new SqlCommand(Program.sql, sqlConnection);SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();BindingSource bindingSource = new BindingSource();bindingSource.DataSource = sqlDataReader;dataGridView1.DataSource = bindingSource;}catch{}finally{sqlConnection.Close();}}this.studentTableAdapter.Fill(this.sinformationDataSet.Student);Program.sql = "";}private void toolStripMenuItem1_Click(object sender, EventArgs e){information Form = new information();//新建一個主窗體Form.Show();//顯示主窗體}private void 修改信息ToolStripMenuItem_Click(object sender, EventArgs e){Updateinformation Form = new Updateinformation();//新建一個主窗體Form.Show();//顯示主窗體}private void 修改密碼ToolStripMenuItem_Click(object sender, EventArgs e){changepassword Form = new changepassword();Form.Show();}private void 操作日志ToolStripMenuItem_Click(object sender, EventArgs e){Log Form = new Log();Form.Show();}private void 退出登錄ToolStripMenuItem_Click(object sender, EventArgs e){this.DialogResult = DialogResult.OK;this.Close();}} }

AddForm

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;namespace WindowsFormsApp1 {public partial class AddForm : System.Windows.Forms.Form{public AddForm(){InitializeComponent();}private void button2_Click(object sender, EventArgs e){this.Close();}private void button1_Click(object sender, EventArgs e){string StuName = textBox2.Text.Trim();string StuNum = textBox1.Text.Trim();string StuSex = comboBox1.Text;string StuAge = textBox3.Text.Trim();string StuDept = comboBox2.Text;SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = Sinformation; Persist Security Info = True; User ID = sa; Password = ***********");try{con.Open();string ComStr = "INSERT INTO Student(Sno, Sname, Ssex, Sage, Sdept)" + "VALUES ('" + StuNum + "','" + StuName + "','" + StuSex + "'," + StuAge + ",'" + StuDept + "')";SqlCommand cmd = new SqlCommand(ComStr, con);cmd.ExecuteNonQuery();MessageBox.Show("添加成功", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);this.Close();}catch{MessageBox.Show("請正確輸入數據(各項不能為空,學號不能重復)", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);}finally{con.Close();}}} }

UpdateForm

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;namespace WindowsFormsApp1 {public partial class UpdateForm : Form{private string select_id;public UpdateForm(){InitializeComponent();}public UpdateForm(string select_id){this.select_id = select_id;InitializeComponent();textBox1.Text = select_id;}private void Form1_Load(object sender, EventArgs e){}private void button1_Click(object sender, EventArgs e){string StuName = textBox2.Text.Trim();string StuNum = textBox1.Text.Trim();string StuSex = comboBox1.Text;string StuAge = textBox3.Text.Trim();string StuDept = comboBox2.Text;SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = Sinformation; Persist Security Info = True; User ID = sa; Password = *********");try{con.Open();string ComStr = "UPDATE Student SET Sname='" + StuName + "',Ssex='" + StuSex + "',Sage=" + StuAge + ",Sdept='" + StuDept + "'"+" WHERE Sno='"+StuNum+"'";SqlCommand cmd = new SqlCommand(ComStr, con);cmd.ExecuteNonQuery();MessageBox.Show("修改成功", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);this.Close();}catch{MessageBox.Show("請正確輸入數據", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);}finally{con.Close();}}private void button2_Click(object sender, EventArgs e){this.Close();}} }

實際上刪除并非是SQLForm的一個子窗體,但是由于其功能與其他幾項權重相等,所以才單獨標識在流程圖中。刪除的代碼已經放入了SQLForm的窗體代碼中。

下面是數據庫中連接的幾張表的截圖
Student(學生信息表)

SysLog(操作日志)

usertable(賬戶信息)

最后給出Student與SysLog觸發(fā)器的代碼

ALTER TRIGGER [dbo].[regist_recorder] ON [dbo].[Student] 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)

總結

以上是生活随笔為你收集整理的学生信息管理系统——代码展示的全部內容,希望文章能夠幫你解決所遇到的問題。

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