C# mysql 学生信息管理系统

C# winform mysql完成学员信息智能管理系统

该程序流程主要是根据对C#文本框的DataGridView控件的单元格开展修改,完成对mysql数据库的删改查改等实际操作。

另附C#应用MySql.Data.MySqlClient;类名的方式:

C# 应用Mysql.Data类名

编译程序自然环境:

Windows VS2019

运作实际效果:

逐渐页面

是用2个文本框中的信息去配对数据分析表中內容,

配对取得成功则进到操作面板。

点一下深蓝色的申请注册文本,自动跳转到申请注册页面。

----------------------------

申请注册页面

可根据在文本框中键入帐号和登陆密码在相匹配数据分析表中插进一个账户信息。

认证成功则再次回到登录界面。

-----------------------------

操作面板

编码将数据分析表中的学员信息添充到了DataGridView控件中。

可根据双击鼠标DataGridView控件的单元格,修改在其中的內容,间接性对数据库系统中信息的开展修改。

提升学员信息,只需修改页面上最终一行的单元格开展修改,就可以加上。

点一下删掉按键,现阶段所选定的单元格的一行信息,将被删掉。

此外,我增加了一个文本框,可根据在文本框中键入sql语句再点一下实行,对数据信息信息开展实际操作。但dql句子并不会有功效。

全部数据库查询中的学员信息或变更数据信息后表明,或手动式更新。

DataGridView控件自身给予了根据点击列名,对数据资料实现排列表明的作用。

也有大量程序流程关键点在编码中由此可见。

--------------------

编码:

Form1登陆对话框:

using System;using System.Collections.Generic;using System.ComponentModel;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 Form1 : Form { MySqlConnection conn; //用以连接数据库目标 MySqlCom ** nd cmd; //用以数据库查询目标1 MySqlDataReader DataReader; //用以数据库查询目标2 String connetStr = //连接数据库用的字符串数组 "server=localhost;port=3306;user=root;password=123456; database=studentsql;"; //程序流程通道 //对话框类构造方法 public Form1() { InitializeComponent(); //建立对话框 //设定对话框表明部位垂直居中 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; } //封装形式数据库查询函数公式,主要参数为要查看的数据分析表 void sqlinquire(String sql) { //点一下登陆按键时,数据库查询的账户密码表 //当文本框中的数据信息相当于相匹配账户密码信息时,进到下一页面 //每一次应用句子以前再次连接数据库 // server=localhost 意味着远程服务器,端口port默认设置是3306可以不写, //user登录名,password密码,database要应用的数据库查询 conn = new MySqlConnection(connetStr); conn.Open(); cmd = new MySqlCom ** nd(sql, conn); DataReader = cmd.ExecuteReader(); } //点一下登陆按键事情 private void button1_Click(object sender, EventArgs e) { try { //将账户和登陆密码字符串数组与数据库系统中的相匹配字段名开展配对,匹配取得成功则进到管理方法页面 sqlinquire("select * from account"); //Read原始数据库索引是-1,实行载入下一行数据信息,沒有下一行回到false //根据解析xml表格中全部数据信息明确相应的关联 bool sign = false; //标识是不是登录成功 while (DataReader.Read()) //假如账户和登陆密码与数据库系统中account超级管理员数据分析表中的信息相匹配,则登录成功 if ((textBox1.Text == DataReader.GetString("id")) && (textBox2.Text == DataReader.GetString("password"))) { sign = true; break; //有一次配对取得成功则意味着寻找取得成功跳出循环 } else sign = false; if (!sign) { MessageBox.Show("登录名或密码错误!"); return; } //账户密码配对则进到管理方法页面 Form3 form3 = new Form3(); form3.Show(); this.Hide(); //掩藏现阶段对话框 } catch { MessageBox.Show("意想不到的不正确!"); return; } } //立即注册按键事情 private void label5_Click(object sender, EventArgs e) { //表明申请注册对话框 Form2 form2 = new Form2(); form2.Show(); //掩藏现阶段对话框 this.Hide(); } }}

Form2申请注册对话框:

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; } //查验2次登陆密码键入是不是一致 if(textBox2.Text != textBox3.Text) { MessageBox.Show("2次登陆密码键入不一致!"); return; } try { //将box1中的文字和box2中的文本插进到管理人员数据分析表中 MySqlConnection conn = new MySqlConnection(connetStr); conn.Open(); String sql = "INSERT INTO account VALUE(" textBox1.Text "," textBox3.Text "); "; MySqlCom ** nd cmd = new MySqlCom ** nd(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; //操作数据库使用的对象 MySqlCom ** nd 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 MySqlCom ** nd(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"; else timetemp = 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 MySqlCom ** nd(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 MySqlCom ** nd(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信息,用于更改后在数据库中查找对应id nameid = 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 MySqlCom ** nd(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 MySqlCom ** nd(sql, conn); cmd.ExecuteNonQuery(); //执行sql语句 MessageBox.Show("执行成功!"); } catch { MessageBox.Show("sql语法错误!"); } finally { queryop(); //刷新表格 } } }}

不足之处:

由于我目前对数据库和C# winform的知识了解的还比较少,所以整个程序也仅仅是实现了功能而已,其中必有诸多纰漏之处。

比如,修改出生日期数据时的格式不对导致的错误,我就还暂时没有办法给出相应的提示。

还请大家多多包含。

感谢大家的支持。

扫码免费用

源码支持二开

申请免费使用

在线咨询