C# winform mysql实现学生信息管理系统

该程序主要是通过对C#窗体的DataGridView控件的单元格进行修改,实现对mysql数据库的增删查改等操作。

附上C#使用MySql.Data.MySqlClient;命名空间的方法:

C# 使用Mysql.Data命名空间

编译环境:

Windows VS2019

运行效果:

开始界面

是用两个文本框中的内容去匹配数据表中内容,

匹配成功则进入操作界面。

点击蓝色的注册文字,跳转到注册界面。

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

注册界面

可通过在文本框中输入账号和密码在对应数据表中插入一个账号信息。

注册成功则重新返回登录界面。

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

操作界面

代码将数据表中的学生信息填充到了DataGridView控件中。

可通过双击DataGridView控件的单元格,修改其中的内容,间接对数据库中信息的进行修改。

增加学生信息,只需修改界面上最后一行的单元格进行修改,即可添加。

点击删除按钮,当前所选中的单元格的一行信息,将被删除。

另外,我添加了一个文本框,可通过在文本框中输入sql语句再点击执行,对数据信息进行操作。但dql语句并不会有作用。

所有数据库中的学生信息或更改数据后显示,或手动刷新。

DataGridView控件本身提供了通过单击字段名,对数据进行排序显示的功能。

还有更多程序细节在代码中可见。

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

代码:

Form1登录窗口:

using 

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;}//检查两次密码输入是否一致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 学生信息管理系统相关推荐

  1. java制作管理系统视频_阶段1:手把手快速做一个Java swing mysql学生信息管理系统附带完整源码及视频开发教程【猿来入此自营】...

    <p> <span style="color:#666666;font-family:"font-size:16px;background-color:#FFFF ...

  2. Java Swing Mysql学生信息管理系统

    通过对Swing的理解 此篇为大家推荐基于JAVA Swing Mysql学生信息管理系统 本视频教程一共分为四个阶段,每个阶段都会是上一个阶段的扩展,每一个阶段的系统都可独立作为一个完整的系统 第一 ...

  3. python:pyqt5+mysql=学生信息管理系统(图文并茂,超详细)——登录,注册及找回密码篇

    python:pyqt5+mysql=学生信息管理系统(图文并茂,超详细)--登录,注册及找回密码篇 前言 一.pyqt5是什么? 二.代码方面 1.引入库及效果展示 2.首先介绍一下登录,注册及密码 ...

  4. python:pyqt5+mysql=学生信息管理系统(图文并茂,超详细, 附源码)——增删改查篇

    python:pyqt5+mysql=学生信息管理系统(图文并茂,超详细, 附源码)--增删改查篇 前言 一.主界面的样式 二.学生信息的增,删,改,查 1.增加学生信息 2.删除学生信息 3.更改学 ...

  5. Java+Swing+mysql学生信息管理系统

    Java+Swing+mysql学生信息管理系统 一.系统介绍 二.功能展示 1.管理员登陆 2.学生信息查询 3.学生信息添加 4.学生信息修改 5.删除 三.系统实现 1.StudentFrame ...

  6. C#Mysql学生信息管理系统

    C#学生信息管理系统连接到Mysql数据库 登錄界面 重要的數據庫連接知識 關於Mysql的安裝: 环境变量的配置 Mysql 启程! Funsql类 总结 登錄界面 代码:` using Syste ...

  7. 基于PHP+MySQL学生信息管理系统的开发与设计

    一直以来我国领导人提倡以人为本的治国方案,而大学是未来人才的培养基地,如何能够更好的对学生信息进行管理,是很多高校一直在研究的一个问题,只有更加科学的对学生信息进行管理,才能够更加积极的培养国家的栋梁 ...

  8. PHP+MYSQL学生信息管理系统

    一.安装phpstudy+Dreamweaver软件并测试 1.安装PHP(已完成) 2.安装Dreamweaver(之前已安装好了) 3.测试并创建了第一个PHP页面,为加强巩固做多了几个练习 二. ...

  9. 添加老师信息的php学生信息管理,PHP+MySQL学生信息管理系统的开发与设计

    一直以来我国领导人提倡以人为本的治国方案,而大学是未来人才的培养基地,如何能够更好的对学生信息进行管理,是很多高校一直在研究的一个问题,只有更加科学的对学生信息进行管理,才能够更加积极的培养国家的栋梁 ...

最新文章

  1. Keep 再融资以后
  2. NHibernate one-to-one
  3. mysql linux 使用索引_正确使用MySQL索引
  4. 【知识图谱】知识表示:知识图谱如何表示结构化的知识?
  5. 机器学习算法(3)——线性回归与逻辑回归
  6. laravel event
  7. vs2019配置opencv4.3
  8. leetcode探索动态规划(二)
  9. Unity: 打飞碟简单版
  10. oracle英文怎么转中文,ORACLE英文字符集转中文
  11. 英伟达P8显卡_英特尔独显芯片上市 AMD 英伟达 英特尔显卡芯片三方对战_笔记本新闻...
  12. oracle中的或者是什么,oracle中=是什么意思呢?
  13. 今天终于把爬虫的Ajax请求搞懂了
  14. IDEA Maven遇到的问题 wating for maven import completionomitted for duplicate jar
  15. Jeecg-Boot简介
  16. c语言循环题兔子第三个月生,C语言上机习题
  17. 证券投资学原理(韩德宗 朱晋)知识点
  18. reports builder 自动产生编号
  19. 生成keytab脚本
  20. 汤晓丹的第四版计算机操作系统--第八章总结概述

热门文章

  1. oracle中存在函数吗,Oracle中的函数
  2. dw超链接标签_小花园DW学习笔记
  3. C语言 memset()函数(内存初始化函数)
  4. 【编译原理】让我们来构建一个简单的解释器(Let’s Build A Simple Interpreter. Part 6.)(python/c/c++版)(笔记)
  5. 深入浅出python机器学习_9.1_数据预处理_sklearn.preprocessing.StandardScaler MinMaxScaler RobustScaler Normalizer
  6. 半编译半解释的Java语言和C++、Python等语言的区别
  7. AcWing算法基础课 Level-2 第二讲 数据结构
  8. java工具配置_Java™ 教程(配置实用工具)
  9. 5页面如何切图_如何让你的设计稿做到95%还原?
  10. 每天学一点儿shell:shell脚本的异步执行