C#Mysql学生信息管理系统 - Go语言中文社区

C#Mysql学生信息管理系统


登錄界面


代码:`

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.MySqlClient;

namespace STUMG
{
    public partial class Loginform : Form
    {
        public Loginform()
        {
            InitializeComponent();
        }
        //窗体加载
        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("欢迎使用学生信息管理系统~", "提示");
        }
        //登录按钮功能
        private void btn_login_Click(object sender, EventArgs e)
        {
            string username = this.txt_usr.Text;
            string userpassword = this.txt_pwd.Text;
            if (username.Equals("") || userpassword.Equals(""))
            {
                MessageBox.Show("用户名或密码不能为空!");
            }
            else//验证是否和数据库密码对应
            {   //连接到要用的学生信息管理数据库
                string strcon = "server = localhost;port=3306;database = student;user=root;password=159753abcDEF";
                MySqlConnection con = new MySqlConnection(strcon);//一个参数连接数据库的
                try
                {
                    con.Open();//打开通道
                    string sqlSel = "Select * from student.user where userid='" + username + "' and userpwd='"+userpassword+"';";
                    MySqlCommand com = new MySqlCommand(sqlSel,con);//用来执行查询命令
                    
                    MySqlDataAdapter check = new MySqlDataAdapter(com);//数据适配器
                 
                    DataSet datacheck = new DataSet();  //建立虚拟数据库
                    int n = check.Fill(datacheck,"user");//在student.user 中执行查询命令返回值
                   // MySqlDataReader sqldr = com.ExecuteReader();//数据读取进行比对
                    if (n!=0)//说明的确存在
                    { 
                        MessageBox.Show("登陆成功!");
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("用户名或或密码错误,请重新输入!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString() + "打开数据库失败");
                }

            }
        }



        //密码显示方式
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                txt_pwd.PasswordChar = new char();
            }           else
                txt_pwd.PasswordChar = '*';
        }
        //回车键登陆


        private void txt_pwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.Focus();
                btn_login_Click(this, new EventArgs());
            }
        }

        private void btn_bk_Click(object sender, EventArgs e)
        {
            MessageBox.Show("使用愉快~");
           this.Close();
            
            
        }
        //注册账号
        private void register_Click(object sender, EventArgs e)
        {
            //如果点击注册账号则,弹出注册页面。
            Form3 register = new Form3();
            register.Show();
        }
    }
}
        

登录界面主要实现了登陆和注册两个功能;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.MySqlClient;
namespace STUMG
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        //点击确认注册
        private void btn_rg_Click(object sender, EventArgs e)
        {
            //取出数据
            string userid, userpwd, userRpwd;
            userid = this.txt_uid.Text;
            userpwd = this.txt_pd.Text;
            userRpwd = this.txt_rpd.Text;
            if (userid.Equals("" )|| userpwd.Equals("") || userRpwd.Equals(""))
                MessageBox.Show("请将信息填写完整");
            else
            {
                if ((userid.Length > 10 || userid.Length < 4)||(userpwd.Length>10||userpwd.Length<4))
                {
                    MessageBox.Show("用户名和密码长度在4到10之间");
                    this.txt_pd.Text = "";
                    this.txt_rpd.Text = "";
                    this.txt_uid.Text = "";
  
                }
                if(!userpwd.Equals(userRpwd))//两次输入密码不一致
                {
                    this.txt_pd.Text = "";
                    this.txt_rpd.Text = "";
                    MessageBox.Show("两次输入密码不一致!");
                }
                //建立到数据库的链接
                string strcon = "server = localhost;port=3306;database = student;user=root;password=159753abcDEF";
                MySqlConnection con = new MySqlConnection(strcon);
                try
                {
                    con.Open();//打开通道
                    string strcheck = "select userid from student.user where userid='" + userid + "';";
                    MySqlCommand cmd = new MySqlCommand(strcheck, con);//新建命令行   
                    MySqlDataAdapter check = new MySqlDataAdapter();//数据适配器 
                    DataSet datacheck = new DataSet();//虚拟表
                    check.SelectCommand = cmd;//让适配器执行select语句
                    int n = check.Fill(datacheck, "user");//要填充的虚拟表对象和数据库表名


                    if (n != 0)//就是找到了和要创建的用户名一样的数据并放在了虚拟表里所以N不为零
                    {
                        MessageBox.Show("用户名已存在");
                    }
                    else
                    {
                        string strin = "insert into student.user(userid,userpwd) values ('" + userid + "','" + userpwd + "');";
                        //插入语句
                        FunSql ist = new FunSql();
                        int x = ist.ExecuteUpdate(strin);//调用函数执行插入语句
                        if (x != 0)
                        {
                            MessageBox.Show("注册成功");
                            this.Dispose();
                        }
                        else
                            MessageBox.Show("注册失败,请联系管理员");
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString() + "打开数据库失败");
                }

            }
        }
    }
}

主界面
在这里插入图片描述佈局根據自己喜好佈局,主要用了PANEL來分割,但不是很理想。流佈局可能效果好一點,不過我還不太會用呢。

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.MySqlClient;

namespace STUMG
{
    /// <summary>
    /// 主界面
    /// </summary>
    public partial class Mainform : Form
    {
        public Mainform()
        {
            InitializeComponent();
        }
        // 时间显示小事件
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.TMN.Text = DateTime.Now.ToString();
        }
        private int OK = 0;
     
        //主窗体退出提示
        private void Mainform_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("确认退出吗?", "退出程序",
             MessageBoxButtons.OKCancel, MessageBoxIcon.Information);//这个重载中可多个按钮
            if (result == DialogResult.OK)
            {
                Dispose();
                Application.Exit();//停止所有消息并且关闭所有应用窗口
            }
            else
                e.Cancel = true;
        }
        //主窗体加载
        private void Mainform_Load(object sender, EventArgs e)
        {
            //调用编写的功能类
            FunSql Dog = new FunSql();//这只狗就可以用来增删查改,现在查询,用来显示数据
            String sqlsel = "select * from student.stuinfo;";
            dataGridView1.DataSource = Dog.ExecuteQuery(sqlsel);//调用类方法查询!绑定datagridview1数据源
        }
        //查询功能
        private void btn_src_Click(object sender, EventArgs e)
        {
            string tbname, fdname, fdid;
            tbname = this.txttab.Text;
            fdname = this.txtnm.Text;
            fdid = this.txtid.Text;
            FunSql Dog = new FunSql();
             string sqlsel;//用于保存Mysql语句

            //默认点击查询显示Stuinfo表格信息
            //模糊查询
            if(tbname.Equals(""))
            {
                if (!fdname.Equals("") && !fdid.Equals(""))
                    sqlsel = "select * from stuinfo where 学号 like'%" + fdid + "%' and 姓名 like'%" + fdname + "%';";
                else if (!fdid.Equals(""))
                    sqlsel = "select * from stuinfo where 学号 like'%" + fdid + "%';";
                else if (!fdname.Equals(""))
                    sqlsel = "select * from stuinfo where 姓名 like'%" + fdname + "%';";
                else
                    sqlsel = "select * from stuinfo;";
                this.dataGridView1.DataSource = Dog.ExecuteQuery(sqlsel);

            }
            else
            {
                if (!fdname.Equals("") && !fdid.Equals(""))
                    sqlsel = "select * from "+tbname+" where 学号 like'%" + fdid + "%' and 姓名 like'%" + fdname + "%';";
                else if (!fdid.Equals(""))
                    sqlsel = "select * from "+tbname+" where 学号 like'%" + fdid + "%';";
                else if (!fdname.Equals(""))
                    sqlsel = "select * from "+tbname+" where 姓名 like'%" + fdname + "%';";
                else
                    sqlsel = "select * from "+tbname+";";
                this.dataGridView1.DataSource = Dog.ExecuteQuery(sqlsel);
            }
            

        }
        //添加数据  
        private void btn_add_Click(object sender, EventArgs e)
        {
            string tbname, fdname, fdid,fdold,fdsex,fdtel,fdqq;//信息表数据
            string fmath, fch, feg, fph;//成绩表数据 
            string sqlinsert;//要插入数据
            tbname = this.txttab.Text;
            fdname = this.txtnm.Text;
            fdid = this.txtid.Text;
            fdold = this.txtod.Text;
            fdsex = this.txtsex.Text;
            fdtel = this.txttel.Text;
            fdqq = this.txtqq.Text;//
            ////////////分割      
            fmath = this.txtmath.Text;
            fch = this.txtch.Text;
            feg = this.txteg.Text;
            fph = this.txtph.Text;
            //////
            
            FunSql Dog = new FunSql();
            FunSql Cat = new FunSql();//更新数据用
            string messup = "select * from student." + tbname + ";";//用于更新数据显示
            if (tbname.Equals(""))
                MessageBox.Show("请输入要操作的表!");
            else
            {
                //处理未知表
                if(!tbname.Equals("exam")&&!tbname.Equals("stuinfo"))
                {
                    MessageBox.Show("该表不可用");
                    this.txtnm.Text = "";
                }
                
                if(tbname.Equals("exam"))
                {
                    //插入数据
                    if (fdid.Equals("") || fdname.Equals("") || fmath.Equals("") || feg.Equals("") || fph.Equals("")||fch.Equals(""))
                        MessageBox.Show("亲~数据要全部填满哦~");
                    else
                    {
                        sqlinsert = "insert into student.exam values(" + fdid + ",'" + fdname + "'," + "'" + fmath + "'," + fch + ",'" + feg + "'," + fph + ");";
                        int i = Dog.ExecuteUpdate(sqlinsert);
                        this.dataGridView1.DataSource = Cat.ExecuteQuery(messup);
                        MessageBox.Show("更新成功,影响了{0}条数据", i.ToString());
                    }
                    
                }
                else if (tbname.Equals("stuinfo"))
                {
                    if (fdid.Equals("") || fdold.Equals("") || fdsex.Equals("") || fdtel.Equals("") || fdqq.Equals("")||fdname.Equals(""))
                        MessageBox.Show("亲~数据要全部填满哦~");
                    else
                    {
                        sqlinsert = "insert into student.stuinfo values(" + fdid + ",'" + fdname + "'," + "'" + fdsex + "'," + fdold + ",'" + fdtel + "'," + fdqq + ");";
                        int i = Dog.ExecuteUpdate(sqlinsert);
                        this.dataGridView1.DataSource = Cat.ExecuteQuery(messup);
                        MessageBox.Show("更新成功,影响了{0}条数据", i.ToString());
                    }
                    
                }
            }
               
        }
        //////删除数据
        private void btn_del_Click(object sender, EventArgs e)
        {
            string tbname, delid,delname;
            tbname = txttab.Text;
            delid = txtid.Text;
            delname = txtnm.Text;
            FunSql Dog = new FunSql();
            FunSql Cat = new FunSql();
            string sqldel;
            string messup = "select * from student." + tbname + ";";//用于更新数据显示
        

            if (!tbname.Equals("") &&(delid.Equals("") || delname.Equals(" "))||tbname.Equals(""))
                MessageBox.Show("请务必填写完整表名(学号或姓名至少一项)");
            else
            {
                //处理未知表
                if (!tbname.Equals("exam") && !tbname.Equals("stuinfo"))
                {
                    MessageBox.Show("该表不可用");
                    this.txtnm.Text = "";
                }
                if (tbname.Equals("stuinfo"))
                {
                    if(!delid.Equals("")&&delname.Equals(""))
                        sqldel = "delete from student.stuinfo where 学号='" + delid + "';";
                    else if(delid.Equals("") && !delname.Equals(""))
                        sqldel = "delete from student.stuinfo where 姓名='" + delname + "';";
                    else
                        sqldel = "delete from student.stuinfo where 姓名='" + delname + "' and 学号='"+delid+"';";
                    int i = Dog.ExecuteUpdate(sqldel);
                    this.dataGridView1.DataSource = Cat.ExecuteQuery(messup);
                    MessageBox.Show("更新成功,影响了{0}条数据", i.ToString());
                }
                else if(tbname.Equals("exam"))
                {
                    if (!delid.Equals("") && delname.Equals(""))
                        sqldel = "delete from student.stuinfo where 学号='" + delid + "';";
                    else if (delid.Equals("") && !delname.Equals(""))
                        sqldel = "delete from student.stuinfo where 姓名='" + delname + "';";
                    else
                        sqldel = "delete from student.stuinfo where 姓名='" + delname + "' and 学号='" + delid + "';";
                    int i = Dog.ExecuteUpdate(sqldel);
                    this.dataGridView1.DataSource = Cat.ExecuteQuery(messup);
                    MessageBox.Show("更新成功,影响了{0}条数据", i.ToString());
                }
                        
            }

        }
        ///成绩和学号以及年龄排序 
        private void button1_Click(object sender, EventArgs e)
        {
            string tbname;
            string method;
            string sqlarr;//排序语句
            tbname = txttab.Text;
            method = txtmd.Text;
            FunSql Dog = new FunSql();
            //用来调用查询函数
            if (tbname.Equals(""))
                MessageBox.Show("请务必确定排序哪个表中数据");
            else
            {   
                if(tbname.Equals("stuinfo"))
                {
                    if (method.Equals("数学") || method.Equals("语文") || method.Equals("英语")
                    || method.Equals("物理"))
                        MessageBox.Show("学生信息表中只能按学号或年龄排序");
                    else
                    {

                        if (OK == 1)//OK为1的时候升序排序
                        {
                            sqlarr = "select * from " + tbname + " order by " + method + " ASC";
                            this.dataGridView1.DataSource = Dog.ExecuteQuery(sqlarr);
                        }
                        else
                        {
                            sqlarr = "select * from " + tbname + " order by " + method + " DESC";
                            this.dataGridView1.DataSource = Dog.ExecuteQuery(sqlarr);
                        }
                    }    
                    
                }
                ///多加表之后可以在这里更改代码
                else if(tbname.Equals("exam"))
                {
                    if (!method.Equals("年龄") && !method.Equals("学号") && !method.Equals("数学") && !method.Equals("语文") && !method.Equals("英语")
                    && !method.Equals("物理"))
                    {
                        MessageBox.Show("只有学号、姓名、年龄和成绩才是关键词哦!");
                        this.txtmd.Text = "";
                    }
                    else
                    {
                        if (OK == 1)//OK为1的时候升序排序
                        {
                            sqlarr = "select * from " + tbname + " order by " + method + " ASC";
                            this.dataGridView1.DataSource = Dog.ExecuteQuery(sqlarr);
                        }
                        else
                        {
                            sqlarr = "select * from " + tbname + " order by " + method + " DESC";
                            this.dataGridView1.DataSource = Dog.ExecuteQuery(sqlarr);
                        }

                    }
                }
                    
            }
                    
        }
        //选择排序方式
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
                OK = 1;
            else
                OK = 0;
        }
        //修改功能  
        private void btn_rvs_Click(object sender, EventArgs e)
        {
            string check;
            string sqlrvs;
            string tbnm, id, old, sex, nm, qq, tel, mth, eg, ch, ph;
            tbnm = this.txttab.Text;
            id = this.txtid.Text;
            old = this.txtod.Text;
            sex = this.txtsex.Text;
            nm = this.txtnm.Text;
            qq = this.txtqq.Text;
            tel = this.txttel.Text;
            mth = this.txtmath.Text;
            eg = this.txteg.Text;
            ch = this.txtch.Text;
            ph = this.txtph.Text;
            check = this.txt_rvs.Text;
            string messup = "select * from student." + tbnm + ";";//用于更新数据显示
            FunSql Dog = new FunSql();
            FunSql Cat = new FunSql();
            if (tbnm.Equals(""))
                MessageBox.Show("请务必填写完整表名");
            else
            {
                if (!tbnm.Equals("exam") && !tbnm.Equals("stuinfo"))
                {
                    MessageBox.Show("该表不可用");
                    this.txtnm.Text = "";
                }
                if(id.Equals(""))
                {
                    MessageBox.Show("学号为必填项,其余根据表选");
                }
                else
                {
                    if(tbnm.Equals("stuinfo"))
                    {
                        if (!nm.Equals("") && !old.Equals("") && !sex.Equals("") && !tel.Equals("") && !qq.Equals(""))
                        {
                            sqlrvs = "update stuinfo" +
                               " set 学号='" + id + "',姓名='" + nm + "',性别='"
                               + sex + "',年龄='" + old + "',电话='" + tel + "',QQ='"
                               + qq + "' where 学号='" + check + "';";
                            int i = Dog.ExecuteUpdate(sqlrvs);
                            this.dataGridView1.DataSource = Cat.ExecuteQuery(messup);
                            MessageBox.Show("更新成功,影响了{0}条数据", i.ToString());

                        }
                        else
                            MessageBox.Show("请填满表中所有信息,谢谢");
                    }
                    else
                    {
                        if (!nm.Equals("") && !id.Equals("") && !mth.Equals("") && !eg.Equals("") && !ch.Equals("") && !ph.Equals(""))
                        {

                            sqlrvs = "update stuinfo" +
                                " set 学号='" + id + "',姓名='" + nm + "',数学='"
                                + mth + "',物理='" + ph + "',英语='" + eg + "',语文='"
                                + ch + "' where 学号='" + check + "';";
                            int i = Dog.ExecuteUpdate(sqlrvs);
                            this.dataGridView1.DataSource = Cat.ExecuteQuery(messup);
                            MessageBox.Show("更新成功,影响了{0}条数据", i.ToString());
                        }
                        else
                            MessageBox.Show("请填满表中所有信息,谢谢");
                    }
                    
                }
            }

        }
        //批量删除
        private void button2_Click(object sender, EventArgs e)
        {
            string tbnm = this.txttab.Text;
            string one, two, three, four;
            string sqldel,messup;
            string sqldel1, sqldel2, sqldel3;
            one = this.txt_id1.Text;two = this.txt_id2.Text;three = this.txt_id3.Text;four = this.txt_id4.Text;
            messup = "select * from student." + tbnm + ";";
            FunSql Dog = new FunSql();
            FunSql Cat = new FunSql();
            if (tbnm.Equals(""))
                MessageBox.Show("请务必填写完整表名");
            else
            {
                if (!tbnm.Equals("exam") && !tbnm.Equals("stuinfo"))
                {
                    MessageBox.Show("该表不可用");
                    this.txtnm.Text = "";
                }
                //缺少至少两项的判断情况
                if (one.Equals("") && two.Equals("") && three.Equals("") && four.Equals(""))
                    MessageBox.Show("批量删除请至少删除一项");
                else
                {
                    if(!one.Equals("")&&two.Equals("")&&!three.Equals("")&&!four.Equals(""))
                    {
                        sqldel = "delete from " + tbnm + " where 学号='" + one + "';";
                        Dog.ExecuteUpdate(sqldel);
                        sqldel1 = "delete from " + tbnm + " where 学号='" + two + "';";
                        Dog.ExecuteUpdate(sqldel1);
                        sqldel2 = "delete from " + tbnm + " where 学号='" + three + "';";
                        Dog.ExecuteUpdate(sqldel2);
                        sqldel3 = "delete from " + tbnm + " where 学号='" + four + "';";
                        Dog.ExecuteUpdate(sqldel3);
                        this.dataGridView1.DataSource = Cat.ExecuteQuery(messup);
                        MessageBox.Show("更新成功,删除了四条数据");
                    }
                    else if(!one.Equals("") && !two.Equals("") && three.Equals("") && four.Equals(""))//1和2
                    {
                        sqldel = "delete from " + tbnm + " where 学号='" + one + "';";
                        Dog.ExecuteUpdate(sqldel);
                        sqldel1 = "delete from " + tbnm + " where 学号='" + two + "';";
                        Dog.ExecuteUpdate(sqldel1);
                        this.dataGridView1.DataSource = Cat.ExecuteQuery(messup);
                        MessageBox.Show("更新成功,删除了两条数据");
                    }
                    else if(!one.Equals("") && two.Equals("") && !three.Equals("") && four.Equals(""))//1 3
                    {
                        sqldel = "delete from " + tbnm + " where 学号='" + one + "';";
                        Dog.ExecuteUpdate(sqldel);
                        sqldel2 = "delete from " + tbnm + " where 学号='" + three + "';";
                        Dog.ExecuteUpdate(sqldel2);
                        this.dataGridView1.DataSource = Cat.ExecuteQuery(messup);
                        MessageBox.Show("更新成功,删除了两条数据");
                    }
                    else if(!one.Equals("") && two.Equals("") && three.Equals("") && !four.Equals(""))// 1 4
                    {
                        sqldel = "delete from " + tbnm + " where 学号='" + one + "';";
                        Dog.ExecuteUpdate(sqldel);
                        sqldel3 = "delete from " + tbnm + " where 学号='" + four + "';";
                        Dog.ExecuteUpdate(sqldel3);
                        this.dataGridView1.DataSource = Cat.ExecuteQuery(messup);
                        MessageBox.Show("更新成功,删除了两条数据");
                    }
                    else if(one.Equals("") && !two.Equals("") && !three.Equals("") && four.Equals(""))// 2 3
                    {
                        sqldel1 = "delete from " + tbnm + " where 学号='" + two + "';";
                        Dog.ExecuteUpdate(sqldel1);
                        sqldel2 = "delete from " + tbnm + " where 学号='" + three + "';";
                        Dog.ExecuteUpdate(sqldel2);
                        this.dataGridView1.DataSource = Cat.ExecuteQuery(messup);
                        MessageBox.Show("更新成功,删除了两条数据");
                    }
                    else if(one.Equals("") && !two.Equals("") && three.Equals("") && !four.Equals(""))// 2 4
                    {
                        sqldel1 = "delete from " + tbnm + " where 学号='" + two + "';";
                        Dog.ExecuteUpdate(sqldel1);
                        sqldel3 = "delete from " + tbnm + " where 学号='" + four + "';";
                        Dog.ExecuteUpdate(sqldel3);
                        this.dataGridView1.DataSource = Cat.ExecuteQuery(messup);
                        MessageBox.Show("更新成功,删除了两条数据");
                    }
                    else if(one.Equals("") && two.Equals("") && !three.Equals("") && !four.Equals(""))//3 4
                    {
                        sqldel2 = "delete from " + tbnm + " where 学号='" + three + "';";
                        Dog.ExecuteUpdate(sqldel2);
                        sqldel3 = "delete from " + tbnm + " where 学号='" + four + "';";
                        Dog.ExecuteUpdate(sqldel3);
                        this.dataGridView1.DataSource = Cat.ExecuteQuery(messup);
                        MessageBox.Show("更新成功,删除了两条数据");
                    }
                    else if(!one.Equals("") && !two.Equals("") && !three.Equals("") && four.Equals(""))//1 2 3
                    {
                        sqldel = "delete from " + tbnm + " where 学号='" + one + "';";
                        Dog.ExecuteUpdate(sqldel);
                        sqldel1 = "delete from " + tbnm + " where 学号='" + two + "';";
                        Dog.ExecuteUpdate(sqldel1);
                        sqldel2 = "delete from " + tbnm + " where 学号='" + three + "';";
                        Dog.ExecuteUpdate(sqldel2);
                        this.dataGridView1.DataSource = Cat.ExecuteQuery(messup);
                        MessageBox.Show("更新成功,删除了三条数据");
                    }
                    else if(!one.Equals("") && !two.Equals("") && three.Equals("") && !four.Equals(""))//1 2 4
                    {
                        sqldel = "delete from " + tbnm + " where 学号='" + one + "';";
                        Dog.ExecuteUpdate(sqldel);
                        sqldel1 = "delete from " + tbnm + " where 学号='" + two + "';";
                        Dog.ExecuteUpdate(sqldel1);
                        sqldel3 = "delete from " + tbnm + " where 学号='" + four + "';";
                        Dog.ExecuteUpdate(sqldel3);
                        this.dataGridView1.DataSource = Cat.ExecuteQuery(messup);
                        MessageBox.Show("更新成功,删除了三条数据");
                    }
                    else if(!one.Equals("") && two.Equals("") && !three.Equals("") && !four.Equals(""))//134
                    {
                        sqldel = "delete from " + tbnm + " where 学号='" + one + "';";
                        Dog.ExecuteUpdate(sqldel);
                        sqldel2 = "delete from " + tbnm + " where 学号='" + three + "';";
                        Dog.ExecuteUpdate(sqldel2);
                        sqldel3 = "delete from " + tbnm + " where 学号='" + four + "';";
                        Dog.ExecuteUpdate(sqldel3);
                        this.dataGridView1.DataSource = Cat.ExecuteQuery(messup);
                        MessageBox.Show("更新成功,删除了三条数据");
                    }
                    else if(one.Equals("") && !two.Equals("") && !three.Equals("") && !four.Equals(""))//234
                    {
                        sqldel1 = "delete from " + tbnm + " where 学号='" + two + "';";
                        Dog.ExecuteUpdate(sqldel1);
                        sqldel2 = "delete from " + tbnm + " where 学号='" + three + "';";
                        Dog.ExecuteUpdate(sqldel2);
                        sqldel3 = "delete from " + tbnm + " where 学号='" + four + "';";
                        Dog.ExecuteUpdate(sqldel3);
                        this.dataGridView1.DataSource = Cat.ExecuteQuery(messup);
                        MessageBox.Show("更新成功,删除了三条数据");
                    }
                    
                }
            }

            }

        private void button3_Click(object sender, EventArgs e)
        {
            this.txt_id1.Text = "";
            this.txt_id2.Text = "";
            this.txt_id3.Text = "";
            this.txt_id4.Text = "";
            this.txt_rvs.Text = "";
            this.txtqq.Text = "";
            this.txtsex.Text = "";
            this.txttab.Text = "";
            this.txttel.Text = "";
            this.txtph.Text = "";
            this.txtod.Text = "";
            this.txtnm.Text = "";
            this.txtmd.Text = "";
            this.txtmath.Text = "";
            this.txtid.Text = "";
            this.txteg.Text = "";
            this.txtch.Text = "";
        }
    } 
 }


這裏就是主要功能了,因爲時間原因,我的數據庫表只用3張彪,兩張關於學生信息的一張是程序賬號的。交互性還有很大提升空間,功能上也有一些不盡人意的地方,有些小瑕疵,跟學校的比起來天壤之別。路還很長,且行且勉勵

重要的數據庫連接知識

小學期的課設計不合理,這對一個吹學者來説真的很不友好,C#還沒掌握多少,卻被告知實驗内容用到數據庫知識。
但是那種每天擠出時間去自學知識的過程真的很愉悅,大學生活的平淡帶來的無趣感業被衝淡了不少。這裏就寫下一
些基本的數據庫操作吧,還有連接VS的方法。

關於Mysql的安裝:

其實我們要在VS中調用Mysql需要的是它的動態鏈接庫Mysql.dll。网上的安装包也很多,不过去官网下还是最好的,这里推荐
去官网下载安装包文件,而不是现成的压缩包(可能存在环境变量配置问题)。
附上链接[官网的页面](https://dev.mysql.com/downloads/windows/installer/8.0.html)
安装就不详说了,没必要,中间有一个设置超级用户密码的过程,自己设置好记住就行,后面要用。

环境变量的配置

安装完后,或者压缩包解压后
这个也挺简单,右键你的电脑,找搞基(嘿嘿)系统设置,找到环境变量设置,然后看path有没有自动添加Mysql
没有的话要把路径设置为Mysql的bin文件;有上下两个栏,直接在下面编辑(新建)就OK,与Java配置差不多啦。

Mysql 启程!

在这里插入图片描述Command Line Client是直接打開Mysql命令行,進入界面后就可以自己開啓Mysql旅程啦!
下面的自然是应用程序了,相比命令行当然界面好多了,不过就和MATLAB一样操作终归还是在Command Line里进行的。
博主在挣扎学习的时候,用的是命令行,现在感觉心情美美哒。
至于你问我Mysql语法?呃,中国大学MOOC欢迎您,同学!
基本语法会在下篇单独写。

Funsql类

在明白C#代码如何进行数据库操作的原理后,我产生了将功能函数(方法)封装的想法,毕竟进行增删查改等操作需要先联通数据库。这个过程中也是磕磕绊绊,说多了都是泪,进入不少误区。下面分享一下代码,也是借鉴CSDN多位博主代码综合出来的,至于多到什么程度呢…多到我已经想不起来看了多少篇博文:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Data;
//包含连接数据库,增删查改等方法的功能类
namespace STUMG
{
    class FunSql
    {

      // private string url = "server = localhost;port=3306;database = student;user=root;password=159753abcDEF";
       
        //查询
        public DataTable ExecuteQuery(string sqlstr)
        {
            //这个就是命令行执行
            string url = "server = localhost;port=3306;database = student;user=root;password=159753abcDEF";
            MySqlCommand cmd;
            //这个是用来建立数据库连接的类    
            MySqlConnection con;
            //包含sql语句执行的结果,并提供一个方法从结果中阅读一行
            MySqlDataAdapter msda;
            //建立虚拟表
            DataTable dt = new DataTable();
            //上面两个配合使用,先建立连接后通过执行sqlstr语句
            //得到msda数据,然后这个数据填充在虚拟表dt中,最终返回数据
            con = new MySqlConnection(url);
            con.Open();//接通数据库
            cmd = new MySqlCommand(sqlstr, con);//执行查询语句,sqlstr从点击中来
            cmd.CommandType = CommandType.Text;//用文本形式解释命令行字符串;
            msda = new MySqlDataAdapter(cmd);//SELECT命令作为数据更新参数
            msda.Fill(dt);
            con.Close();//关闭链接

            return dt;
        }
        //增删改
        public int ExecuteUpdate(string sqlstr)
        {
            
            string url = "server = localhost;port=3306;database = student;user=root;password=159753abcDEF";
           

 
            MySqlConnection con = new MySqlConnection(url);
            con.Open();//接通数据库
            MySqlCommand cmd = new MySqlCommand(sqlstr, con);//cmd命令在这里得到相应命令
           // cmd.CommandType = CommandType.Text;//用文本形式解释命令行字符串;
            int exeresult = 0;
            exeresult = cmd.ExecuteNonQuery();//直接对数据库中数据动手
           
            return exeresult;
            //返回的是处理数据个数
        }

    }
}

这里的两个类方法想想其实挺有意思的,尤其是当查到Mysqlcomm 类方法executenonquery时,真的爽死了,这也太强了叭,反正本萌新被震慑到了,hhhh

总结

收获挺多,继续努力。然后这个就是自己无聊,然后写出来记录离校前的时光的,天气好热啊,又没有知心人解闷。还是创建对象玩好了… 咳咳,代码只是提供一个思路,里面还是有很多bug的,而且局限性真的很大,说白了就是交作业,嘿嘿…我要回家。
感兴趣的可以CSDN发消息找我唠啊,虽然不知道能不能发。

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_44102479/article/details/95447514
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢