c#如何跳出一个函数_C# mysql 学生信息管理系统
C# winform mysql實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)
該程序主要是通過對(duì)C#窗體的DataGridView控件的單元格進(jìn)行修改,實(shí)現(xiàn)對(duì)mysql數(shù)據(jù)庫的增刪查改等操作。
附上C#使用MySql.Data.MySqlClient;命名空間的方法:
C# 使用Mysql.Data命名空間
編譯環(huán)境:
Windows VS2019
運(yùn)行效果:
開始界面
是用兩個(gè)文本框中的內(nèi)容去匹配數(shù)據(jù)表中內(nèi)容,
匹配成功則進(jìn)入操作界面。
點(diǎn)擊藍(lán)色的注冊(cè)文字,跳轉(zhuǎn)到注冊(cè)界面。
----------------------------
注冊(cè)界面
可通過在文本框中輸入賬號(hào)和密碼在對(duì)應(yīng)數(shù)據(jù)表中插入一個(gè)賬號(hào)信息。
注冊(cè)成功則重新返回登錄界面。
-----------------------------
操作界面
代碼將數(shù)據(jù)表中的學(xué)生信息填充到了DataGridView控件中。
可通過雙擊DataGridView控件的單元格,修改其中的內(nèi)容,間接對(duì)數(shù)據(jù)庫中信息的進(jìn)行修改。
增加學(xué)生信息,只需修改界面上最后一行的單元格進(jìn)行修改,即可添加。
點(diǎn)擊刪除按鈕,當(dāng)前所選中的單元格的一行信息,將被刪除。
另外,我添加了一個(gè)文本框,可通過在文本框中輸入sql語句再點(diǎn)擊執(zhí)行,對(duì)數(shù)據(jù)信息進(jìn)行操作。但dql語句并不會(huì)有作用。
所有數(shù)據(jù)庫中的學(xué)生信息或更改數(shù)據(jù)后顯示,或手動(dòng)刷新。
DataGridView控件本身提供了通過單擊字段名,對(duì)數(shù)據(jù)進(jìn)行排序顯示的功能。
還有更多程序細(xì)節(jié)在代碼中可見。
--------------------
代碼:
Form1登錄窗口:
usingForm2注冊(cè)窗口:
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; using MySql.Data; using MySql.Data.MySqlClient;namespace C_sharp學(xué)生信息管理系統(tǒng) {public partial class Form2 : Form{String connetStr = //連接數(shù)據(jù)庫字符串"server=localhost;port=3306;user=root;password=123456; database=studentsql;";public Form2(){InitializeComponent();//設(shè)置窗口顯示位置居中this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;}//注冊(cè)按鈕點(diǎn)擊事件private void button1_Click(object sender, EventArgs e){//檢查注冊(cè)的用戶名和密碼是否為空if (textBox1.Text.Length < 4){MessageBox.Show("用戶名不可少于4位!");return;}if ((textBox2.Text.Length < 4) || (textBox3.Text.Length < 4)){MessageBox.Show("注冊(cè)密碼不可少于4位!");return;}//檢查兩次密碼輸入是否一致if(textBox2.Text != textBox3.Text){MessageBox.Show("兩次密碼輸入不一致!");return;}try{//將box1中的文本和box2中的文本插入到管理員數(shù)據(jù)表中MySqlConnection conn = new MySqlConnection(connetStr);conn.Open();String sql ="INSERT INTO account VALUE(" + textBox1.Text + "," + textBox3.Text + "); ";MySqlCommand cmd = new MySqlCommand(sql, conn);cmd.ExecuteNonQuery();//彈出提示框MessageBox.Show("注冊(cè)成功!");//進(jìn)入操作界面Form1 form1 = new Form1();form1.Show();this.Hide(); //隱藏當(dāng)前窗口}catch{MessageBox.Show("用戶名已存在或其他錯(cuò)誤!");return;}}//窗口退出之后事件,該窗口退出后程序退出private void Form2_FormClosed(object sender, FormClosedEventArgs e){Application.Exit();}//隱藏密碼單選框被選中時(shí),密碼框設(shè)置掩碼private void radioButton1_CheckedChanged(object sender, EventArgs e){textBox2.PasswordChar = '#';textBox3.PasswordChar = '#';}} }Form3操作窗口:
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; using MySql.Data; using MySql.Data.MySqlClient;namespace C_sharp學(xué)生信息管理系統(tǒng) {public partial class Form3 : Form{String connetStr = //連接數(shù)據(jù)庫字符串"server=localhost;port=3306;user=root;password=123456; database=studentsql;";MySqlConnection conn; //操作數(shù)據(jù)庫使用的對(duì)象MySqlCommand cmd;String nameid; //臨時(shí)記錄數(shù)據(jù)表id列字段int index_y; //記錄選中當(dāng)前行索引int index_x; //記錄選中當(dāng)前列索引String sql; //執(zhí)行的sql語句字符串//構(gòu)造public Form3(){InitializeComponent();//設(shè)置窗口顯示位置居中this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;queryop();}//該窗體關(guān)閉后,程序退出private void Form3_FormClosing(object sender, FormClosingEventArgs e){Application.Exit();}//刷新:查詢并將結(jié)果填充到表格private void queryop(){try{//將數(shù)據(jù)表中的信息填充到表格控件中conn = new MySqlConnection(connetStr);conn.Open();sql = "select * from studentmess;";cmd = new MySqlCommand(sql, conn);MySqlDataAdapter adapter = new MySqlDataAdapter(sql, conn);DataTable dataTable = new DataTable();adapter.Fill(dataTable); //將adapter中的信息填充到表格中dataGridView1.DataSource = dataTable;dataGridView1.AutoGenerateColumns = true;dataGridView1.DataSource = dataTable;}catch{MessageBox.Show("意料之外的錯(cuò)誤!");return;}}//點(diǎn)擊查詢按鈕執(zhí)行查詢private void button1_Click(object sender, EventArgs e){queryop();}//限定日期添加規(guī)則函數(shù)private String addruletime(){//如果當(dāng)前列不是日期列返回默認(rèn)日期字符串//否則不變String timetemp;if (index_x != 2)timetemp = "2020/1/1";elsetimetemp = dataGridView1.Rows[index_y].Cells[2].Value.ToString();return timetemp;}//限定成績添加函數(shù)private String addrulescore(){//如果當(dāng)前列不是三個(gè)成績列,則返回默認(rèn)成績,否則不變String tempscore;int tempx = 0; //臨時(shí)記錄列if (index_x != 5 || index_x != 6 || index_x != 7){tempscore = "0";return tempscore; //不是成績列直接返回}//是成績列判斷是那一列,然后根據(jù)列返回switch (index_x){case 5:tempx = 5;break;case 6:tempx = 6;break;case 7:tempx = 7;break;}tempscore = dataGridView1.Rows[index_y].Cells[tempx].Value.ToString();return tempscore;}//當(dāng)前所選內(nèi)容更改時(shí)發(fā)生private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e){//更改內(nèi)容包括更改和添加新行//首先嘗試添加新行,但如果要添加新行,此時(shí)更改的行坐標(biāo)對(duì)應(yīng)的姓名等信息不存在,所以程序拋出異常//這時(shí)轉(zhuǎn)為在數(shù)據(jù)庫中插入新行操作try{//獲取當(dāng)前行的索引index_y = dataGridView1.CurrentRow.Index;//連接數(shù)據(jù)庫服務(wù)conn = new MySqlConnection(connetStr); //conn new新對(duì)象時(shí),會(huì)自動(dòng)調(diào)用關(guān)閉數(shù)據(jù)庫連接函數(shù)conn.Open();//獲取當(dāng)前選中行的每一個(gè)數(shù)據(jù),并修改到數(shù)據(jù)庫//執(zhí)行的sql語句sql ="UPDATE studentmess SET " +"id = " + dataGridView1.Rows[index_y].Cells[0].Value.ToString() + "," +"`name` = '" + dataGridView1.Rows[index_y].Cells[1].Value.ToString() + "'," +"birthday = '" + dataGridView1.Rows[index_y].Cells[2].Value.ToString() + "'," +"idcardnum = '" + dataGridView1.Rows[index_y].Cells[3].Value.ToString() + "'," +"contact = '" + dataGridView1.Rows[index_y].Cells[4].Value.ToString() + "'," +"`C# score` = " + dataGridView1.Rows[index_y].Cells[5].Value.ToString() + "," +"` C++ score` = " + dataGridView1.Rows[index_y].Cells[6].Value.ToString() + "," +"`Data structure score` = " + dataGridView1.Rows[index_y].Cells[7].Value.ToString() +" WHERE id = '" + nameid + "';";cmd = new MySqlCommand(sql, conn);cmd.ExecuteNonQuery(); //執(zhí)行sql語句}catch{//嘗試添加新的行//如果再次出現(xiàn)異常代表,沒有從id(主鍵)列開始添加行,彈出提示框try{//連接數(shù)據(jù)庫conn = new MySqlConnection(connetStr);conn.Open();//根據(jù)規(guī)則執(zhí)行sql ="INSERT INTO `studentmess` VALUES(" +dataGridView1.Rows[index_y].Cells[0].Value.ToString() + "," +"'" + dataGridView1.Rows[index_y].Cells[1].Value.ToString() + "'," +"'" + addruletime() + "'," +"'" + dataGridView1.Rows[index_y].Cells[3].Value.ToString() + "'," +"'" + dataGridView1.Rows[index_y].Cells[4].Value.ToString() + "'," +"'" + addrulescore() + "'," +"'" + addrulescore() + "'," +"'" + addrulescore() + "'" +"); ";cmd = new MySqlCommand(sql, conn);cmd.ExecuteNonQuery(); //執(zhí)行sql語句}catch{MessageBox.Show("t操作無效!n沒有在id列開始添加新行或其他錯(cuò)誤");return;}}}//單元格編輯模式啟動(dòng)時(shí)發(fā)生private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e){//獲取編輯單元格前的單元格行索引index_y = dataGridView1.CurrentRow.Index;//列索引index_x = dataGridView1.ColumnCount;//記錄開始的id信息,用于更改后在數(shù)據(jù)庫中查找對(duì)應(yīng)idnameid = dataGridView1.Rows[index_y].Cells[0].Value.ToString();}//刪除按鈕點(diǎn)擊事件private void button2_Click(object sender, EventArgs e){try{//獲取當(dāng)前行列索引index_y = dataGridView1.CurrentRow.Index;conn = new MySqlConnection(connetStr);conn.Open();//sql語句sql ="DELETE FROM studentmess WHERE id = "+ dataGridView1.Rows[index_y].Cells[0].Value.ToString() + ";";cmd = new MySqlCommand(sql, conn);cmd.ExecuteNonQuery(); //執(zhí)行sql語句}catch (Exception){MessageBox.Show("意料之外的錯(cuò)誤!");}finally{queryop(); //刷新數(shù)據(jù)}}//單擊文本框事件private void textBox1_Click(object sender, EventArgs e){//將文本內(nèi)容清除textBox1.Text = "";}//執(zhí)行按鈕點(diǎn)擊事件private void button3_Click(object sender, EventArgs e){try{//輸入執(zhí)行語句功能,不能顯示查詢語句conn = new MySqlConnection(connetStr);conn.Open();sql = textBox1.Text;cmd = new MySqlCommand(sql, conn);cmd.ExecuteNonQuery(); //執(zhí)行sql語句MessageBox.Show("執(zhí)行成功!");}catch{MessageBox.Show("sql語法錯(cuò)誤!");}finally{queryop(); //刷新表格}}} }不足之處:
由于我目前對(duì)數(shù)據(jù)庫和C# winform的知識(shí)了解的還比較少,所以整個(gè)程序也僅僅是實(shí)現(xiàn)了功能而已,其中必有諸多紕漏之處。
比如,修改出生日期數(shù)據(jù)時(shí)的格式不對(duì)導(dǎo)致的錯(cuò)誤,我就還暫時(shí)沒有辦法給出相應(yīng)的提示。
還請(qǐng)大家多多包含。
感謝大家的支持。
總結(jié)
以上是生活随笔為你收集整理的c#如何跳出一个函数_C# mysql 学生信息管理系统的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js解析二维码_最新最全阿里巴巴,今日头
- 下一篇: seaborn 画堆叠柱状图_Seabo