生活随笔
收集整理的這篇文章主要介紹了
学生信息管理系统——代码展示
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
整體程序流程圖
下面從登錄窗口開始,逐個給出相應(yīng)的窗體截圖和對應(yīng)代碼
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"));}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
+ "'"; 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 = '教師'"; 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();}}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
;if(picturePath
!=""){FileStream fs
= new FileStream(picturePath
, FileMode
.Open
, FileAccess
.Read
);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"));}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("學(xué)號或教職工號不能為空", "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
);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
+ "'"; SqlCommand sqlCommand
= new SqlCommand(sql
, sqlConnection
);SqlDataReader sqlDataReader
= sqlCommand
.ExecuteReader();if(sqlDataReader
.HasRows
){label8
.Text
= "*該用戶名已經(jīng)存在";}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 regex
= new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");if (regex
.IsMatch(textBox2
.Text
)){label9
.Text
= "";}else{label9
.Text
= "*至少包含數(shù)字、大小寫字母各一個。且不超過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
){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(); read
.Read();textBox3
.Text
= read
["userid"].ToString();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
);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(); read
.Read();textBox3
.Text
= read
["userid"].ToString();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
);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
;if (picturePath
!= ""){FileStream fs
= new FileStream(picturePath
, FileMode
.Open
, FileAccess
.Read
);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("學(xué)號或教職工號不能為空", "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
);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 regex
= new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");if (regex
.IsMatch(textBox2
.Text
)){label4
.Text
= "";}else{label4
.Text
= "*至少包含數(shù)字、大小寫字母各一個。且不超過20個字符";}}else{label4
.Text
= "*密碼不能為空";}}private void textBox3_Leave(object sender
, EventArgs e
){if (textBox2
.Text
.Trim() != ""){Regex regex
= new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");if (regex
.IsMatch(textBox2
.Text
)){label5
.Text
= "";}else{label5
.Text
= "*至少包含數(shù)字、大小寫字母各一個。且不超過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"));}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
+ "'"; 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
){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();string delete_by_id
= "delete from Student where Sno=" + select_id
;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();UpdateForm 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("請正確輸入數(shù)據(jù)(各項不能為空,學(xué)號不能重復(fù))", "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("請正確輸入數(shù)據(jù)", "notice", MessageBoxButtons
.OK
, MessageBoxIcon
.Error
);}finally{con
.Close();}}private void button2_Click(object sender
, EventArgs e
){this.Close();}}
}
實際上刪除并非是SQLForm的一個子窗體,但是由于其功能與其他幾項權(quán)重相等,所以才單獨標(biāo)識在流程圖中。刪除的代碼已經(jīng)放入了SQLForm的窗體代碼中。
下面是數(shù)據(jù)庫中連接的幾張表的截圖
Student(學(xué)生信息表)
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)
總結(jié)
以上是生活随笔為你收集整理的学生信息管理系统——代码展示的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。