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