数据库综合实验代码展示
生活随笔
收集整理的這篇文章主要介紹了
数据库综合实验代码展示
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
登錄界面
登錄界面代碼
namespace StudentManagementSystem {public partial class LoginForm : Form{public LoginForm(){InitializeComponent();}public string code;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 username = UserNametext.Text.Trim(); //取出賬號string password = EncryptWithMD5(Passwordtext.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&&comboBox1.SelectedIndex==0){common.id = UserNametext.Text.Trim();MessageBox.Show("歡迎使用!"); //登錄成功StudentForm form2 = new StudentForm();form2.Show();this.Hide();}else if (sqlDataReader.HasRows && textBox3.Text == code && comboBox1.SelectedIndex == 1){common.id = UserNametext.Text.Trim();MessageBox.Show("歡迎使用!"); //登錄成功ManagerForm form2 = new ManagerForm();form2.Show();this.Hide();}else{MessageBox.Show("登錄失敗!");code = null;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();}linkLabel1.Text = code;return;}sqlDataReader.Close();sql = "insert into SysLog values ( '" + username + "' , '" + DateTime.Now + "' , '" + "Login" + "')"; //編寫SQL命令sqlCommand = new SqlCommand(sql, sqlConnection);sqlCommand.ExecuteNonQuery();sqlConnection.Close();}private void button2_Click(object sender, EventArgs e){Application.Exit();}private void LoginForm_Load(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();}linkLabel1.Text = code;}//點擊驗證碼可以刷新private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e){code = null;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();}linkLabel1.Text = code;}//點擊注冊按鈕轉到注冊界面private void button3_Click(object sender, EventArgs e){SignForm signform = new SignForm();signform.Show();}} } public static class common // static 不是必須 {public static string id; }在使用學生賬號登錄時,打算用一個全局變量來存儲其學號信息,但是經過查詢資料,c#語言不支持全局變量的定義,所以在本次綜合實驗中,我使用的時定義一個新的common類來起到全局變量的作用。
public static class common // static 不是必須 {public static string id; }注冊界面
注冊界面代碼
namespace StudentManagementSystem {public partial class SignForm : Form{public SignForm(){InitializeComponent();}private void button2_Click(object sender, EventArgs e){this.Close();}public Byte[] mybyte = new byte[0];private void button3_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);pictureBox2.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 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);try{string connString = "Data Source=.;Initial Catalog=curricula_variable_system;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("注冊成功");}catch (Exception ex){MessageBox.Show(ex.Message);}this.Close();}} }學生端口
學生端口代碼
namespace StudentManagementSystem {public partial class StudentForm : Form{public StudentForm(){InitializeComponent();}private void ShowForm_Load(object sender, EventArgs e){String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";SqlConnection con = new SqlConnection(connectionString);con.Open();string sql = "select UserPhoto from SysUser where UserID = '" + common.id + "'";SqlCommand command = new SqlCommand(sql, con);SqlDataAdapter dataAdapter = new SqlDataAdapter(command);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){LoginForm Form = new LoginForm();Form.Show();this.Close();}private void button3_Click(object sender, EventArgs e){CourseForStudent cfs = new CourseForStudent();cfs.Show();}} }學生信息查詢
namespace StudentManagementSystem {public partial class CourseForStudent : Form{public CourseForStudent(){InitializeComponent();}private void CourseForStudent_Load(object sender, EventArgs e){String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password= ";SqlConnection con = new SqlConnection(connectionString);try{con.Open();String select_by_id = "select * from SC where Sno=" + common.id;SqlCommand sqlCommand = new SqlCommand(select_by_id, con);SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();BindingSource bindingSource = new BindingSource();bindingSource.DataSource = sqlDataReader;dataGridView1.DataSource = bindingSource;}catch{MessageBox.Show("查詢語句有誤,請認真檢查SQL語句!");}finally{con.Close();}// TODO: 這行代碼將數據加載到表“curricula_variable_systemDataSet.SC”中。您可以根據需要移動或刪除它。//this.sCTableAdapter.Fill(this.curricula_variable_systemDataSet.SC);}private void button1_Click(object sender, EventArgs e){this.Close();}} }管理員端口
管理員端口代碼
namespace StudentManagementSystem {public partial class ManagerForm : Form{public ManagerForm(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){LoginForm Form = new LoginForm();Form.Show();this.Close();}private void Manager_Load(object sender, EventArgs e){}private void button2_Click(object sender, EventArgs e){SysLog sys = new SysLog();sys.Show();}private void button3_Click(object sender, EventArgs e){SC sc = new SC();sc.Show();}private void button4_Click(object sender, EventArgs e){Course course = new Course();course.Show();}private void button5_Click(object sender, EventArgs e){Student student = new Student();student.Show();}} }查看日志
namespace StudentManagementSystem {public partial class SysLog : Form{public SysLog(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){this.Close();}private void SysLog_Load(object sender, EventArgs e){// TODO: 這行代碼將數據加載到表“curricula_variable_systemDataSet.SysLog”中。您可以根據需要移動或刪除它。this.sysLogTableAdapter.Fill(this.curricula_variable_systemDataSet.SysLog);}} }課程開設
namespace StudentManagementSystem {public partial class Course : Form{public Course(){InitializeComponent();}private void Course_Load(object sender, EventArgs e){// TODO: 這行代碼將數據加載到表“curricula_variable_systemDataSet.Course”中。您可以根據需要移動或刪除它。this.courseTableAdapter.Fill(this.curricula_variable_systemDataSet.Course);}private void button5_Click(object sender, EventArgs e){this.Close();}private void button1_Click(object sender, EventArgs e){String cno = Cno.Text;String cname = Cname.Text;String cpno = Cpno.Text;String credit = Credit.Text;String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";SqlConnection con = new SqlConnection(connectionString);try{con.Open();string insertStr = "INSERT INTO Course (Cno,Cname,Cpno,Ccredit) " +"VALUES ('" + cno + "','" + cname + "','" + cpno + "','"+ credit + "')";SqlCommand cmd = new SqlCommand(insertStr, con);cmd.ExecuteNonQuery();}catch{MessageBox.Show("輸入數據違反要求");}finally{con.Dispose();}this.courseTableAdapter.Fill(this.curricula_variable_systemDataSet.Course);}private void button2_Click(object sender, EventArgs e){String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";SqlConnection con = new SqlConnection(connectionString);try{con.Open();string cno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//選擇的當前行第一列的值,也就是IDstring delete_by_id = "delete from Course where Cno=" + cno;//sql刪除語句SqlCommand cmd = new SqlCommand(delete_by_id, con);cmd.ExecuteNonQuery();}catch{MessageBox.Show("請正確選擇行!");}finally{con.Dispose();}this.courseTableAdapter.Fill(this.curricula_variable_systemDataSet.Course);}private void button3_Click(object sender, EventArgs e){String cno = Cno.Text.Trim();String cname = Cname.Text.Trim();String cpno = Cpno.Text.Trim();String credit = Credit.Text.Trim();String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";SqlConnection con = new SqlConnection(connectionString);try{con.Open();string insertStr = "UPDATE Course SET Cname = " + cname +",Cpno= "+cpno+",Ccredit="+credit +" WHERE Cno = '" + cno + "'";SqlCommand cmd = new SqlCommand(insertStr, con);cmd.ExecuteNonQuery();}catch{MessageBox.Show("輸入數據違反要求!");}finally{con.Dispose();}this.courseTableAdapter.Fill(this.curricula_variable_systemDataSet.Course);}private void button4_Click(object sender, EventArgs e){String cno = Cno.Text.Trim();String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";SqlConnection con = new SqlConnection(connectionString);try{con.Open();String select_by_id = "select * from Course where Cno='" + cno + "'";SqlCommand sqlCommand = new SqlCommand(select_by_id, con);SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();BindingSource bindingSource = new BindingSource();bindingSource.DataSource = sqlDataReader;dataGridView1.DataSource = bindingSource;}catch{MessageBox.Show("查詢語句有誤,請認真檢查SQL語句!");}finally{con.Close();}}} }選課關系
namespace StudentManagementSystem {public partial class SC : Form{public SC(){InitializeComponent();}private void SC_Load(object sender, EventArgs e){// TODO: 這行代碼將數據加載到表“curricula_variable_systemDataSet.SC”中。您可以根據需要移動或刪除它。this.sCTableAdapter.Fill(this.curricula_variable_systemDataSet.SC);}private void button5_Click(object sender, EventArgs e){this.Close();}private void button1_Click(object sender, EventArgs e){String Sno = SnoText.Text;String Cno = CnoText.Text;String Grade = GradeText.Text;String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";SqlConnection con = new SqlConnection(connectionString);try{con.Open();string insertStr = "INSERT INTO SC (Sno,Cno,Grade) " +"VALUES ('" + Sno + "','" + Cno + "','" + Grade + "')";SqlCommand cmd = new SqlCommand(insertStr, con);cmd.ExecuteNonQuery();}catch{MessageBox.Show("輸入數據違反要求,該學生可能不在數據庫中");}finally{con.Dispose();}this.sCTableAdapter.Fill(this.curricula_variable_systemDataSet.SC);}private void button2_Click(object sender, EventArgs e){String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";SqlConnection con = new SqlConnection(connectionString);try{con.Open();string sno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//選擇的當前行第一列的值,也就是IDstring cno = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();string delete_by_id = "delete from SC where Sno=" + sno+"and Cno="+cno;//sql刪除語句SqlCommand cmd = new SqlCommand(delete_by_id, con);cmd.ExecuteNonQuery();}catch{MessageBox.Show("請正確選擇行!");}finally{con.Dispose();}this.sCTableAdapter.Fill(this.curricula_variable_systemDataSet.SC);}private void button3_Click(object sender, EventArgs e){String sno = SnoText.Text.Trim();String cno = CnoText.Text.Trim();String grade = GradeText.Text.Trim();String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";SqlConnection con = new SqlConnection(connectionString);try{con.Open();string insertStr = "UPDATE SC SET Grade = " + grade + " WHERE Sno = '" + sno + "'and Cno='"+cno+"'";SqlCommand cmd = new SqlCommand(insertStr, con);cmd.ExecuteNonQuery();}catch{MessageBox.Show("輸入數據違反要求!");}finally{con.Dispose();}this.sCTableAdapter.Fill(this.curricula_variable_systemDataSet.SC);}private void button4_Click(object sender, EventArgs e){String sno = SnoText.Text.Trim();String cno = CnoText.Text.Trim();String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";SqlConnection con = new SqlConnection(connectionString);try{con.Open();String select_by_id = "select * from SC where Sno='" + sno + "'"+"and Cno='"+cno+"'";SqlCommand sqlCommand = new SqlCommand(select_by_id, con);SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();BindingSource bindingSource = new BindingSource();bindingSource.DataSource = sqlDataReader;dataGridView1.DataSource = bindingSource;}catch{MessageBox.Show("查詢語句有誤,請認真檢查SQL語句!");}finally{con.Close();}}} }學生信息
namespace StudentManagementSystem {public partial class Student : Form{public Student(){InitializeComponent();}private void Student_Load(object sender, EventArgs e){// TODO: 這行代碼將數據加載到表“curricula_variable_systemDataSet.Student”中。您可以根據需要移動或刪除它。this.studentTableAdapter.Fill(this.curricula_variable_systemDataSet.Student);}private void button5_Click(object sender, EventArgs e){this.Close();}private void button1_Click(object sender, EventArgs e){String sno = Sno.Text;String sname = Sname.Text;String ssex = Ssex.Text;String sage = Sage.Text;String sdept = Sdept.Text;String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";SqlConnection con = new SqlConnection(connectionString);try{con.Open();string insertStr = "INSERT INTO Student (Sno,Sname,Ssex,Sage,Sdept) " +"VALUES (" + sno + "," + sname + ",'" + ssex + "'," +sage+ ",'" + sdept + "')";SqlCommand cmd = new SqlCommand(insertStr, con);cmd.ExecuteNonQuery();}catch{MessageBox.Show("輸入數據違反要求");}finally{con.Dispose();}this.studentTableAdapter.Fill(this.curricula_variable_systemDataSet.Student);}private void button2_Click(object sender, EventArgs e){String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";SqlConnection con = new SqlConnection(connectionString);try{con.Open();string sno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//選擇的當前行第一列的值,也就是IDstring delete_by_id = "delete from SC where Sno=" + sno+"delete from Student where Sno=" + sno ;//sql刪除語句SqlCommand cmd = new SqlCommand(delete_by_id, con);cmd.ExecuteNonQuery();}catch{MessageBox.Show("請正確選擇行!");}finally{con.Dispose();}this.studentTableAdapter.Fill(this.curricula_variable_systemDataSet.Student);}private void button3_Click(object sender, EventArgs e){String sno = Sno.Text.Trim();String sname = Sname.Text.Trim();String ssex = Ssex.Text.Trim();String sage = Sage.Text.Trim();String sdept = Sdept.Text.Trim();String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";SqlConnection con = new SqlConnection(connectionString);try{con.Open();string insertStr = "UPDATE Student SET Sname = " + sname + ",Ssex= '" + ssex + "',Sage=" + sage + ",Sdept='"+sdept+"' WHERE Sno = " + sno ;SqlCommand cmd = new SqlCommand(insertStr, con);cmd.ExecuteNonQuery();}catch{MessageBox.Show("輸入數據違反要求!");}finally{con.Dispose();}this.studentTableAdapter.Fill(this.curricula_variable_systemDataSet.Student);}private void button4_Click(object sender, EventArgs e){String sno = Sno.Text.Trim();String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";SqlConnection con = new SqlConnection(connectionString);try{con.Open();String select_by_id = "select * from Student where Sno='" + sno + "'";SqlCommand sqlCommand = new SqlCommand(select_by_id, con);SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();BindingSource bindingSource = new BindingSource();bindingSource.DataSource = sqlDataReader;dataGridView1.DataSource = bindingSource;}catch{MessageBox.Show("查詢語句有誤,請認真檢查SQL語句!");}finally{con.Close();}}} }總結
以上是生活随笔為你收集整理的数据库综合实验代码展示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 树莓派蓝牙与手机APP通信
- 下一篇: 便签。9.24.2020