⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 学生信息查询管理.cs

📁 简单的学生管理系统
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace 第四次实验
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            fillData();
        }

        private void showData()
        {
            myClass mC = new myClass();
            OleDbConnection con = mC.getConnect();
            using (con)//连接数据库表,并取得表中的一个字段
            {
                DataTable dt = new DataTable();        //内存中数据的一个表
                OleDbDataAdapter da = new OleDbDataAdapter("select * from 学生 order by 学号", con);//数据适配器
                da.Fill(dt);                           //用数据适配器da去填写内存中的那个表dt
                DataView dv = new DataView(dt);        //用已有的数据去填写数据视图
                this.dataGridView1.DataSource = dv;    //数据控件就等于dv

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            showData();
        }

        private void fillData()
        {
            try
            {
                this.numberTextBox.Text = this.dataGridView1[0, this.dataGridView1.CurrentCell.RowIndex].Value.ToString();
                this.nameTextBox.Text = this.dataGridView1[1, this.dataGridView1.CurrentCell.RowIndex].Value.ToString();
                this.sexTextBox.Text = this.dataGridView1[2, this.dataGridView1.CurrentCell.RowIndex].Value.ToString();
                this.ageTextBox.Text = this.dataGridView1[3, this.dataGridView1.CurrentCell.RowIndex].Value.ToString();
                this.acaTextBox.Text = this.dataGridView1[4, this.dataGridView1.CurrentCell.RowIndex].Value.ToString();
                this.speTextBox.Text = this.dataGridView1[5, this.dataGridView1.CurrentCell.RowIndex].Value.ToString();
            }
            catch { }
        }

        private void add(object sender, EventArgs e)
        {
            if (IsSameRecord())
            {
                return;
            }
            else
            {
                try
                {
                    StringBuilder strOLE = new StringBuilder();
                    strOLE.Append("insert into 学生(学号,姓名,性别,年龄,学院,专业)");
                    strOLE.Append("values('" + this.numberTextBox.Text.Trim().ToString() + "','" + this.nameTextBox.Text.Trim().ToString() + "',");
                    strOLE.Append("'" + this.sexTextBox.Text.Trim().ToString() + "','" + this.ageTextBox.Text.Trim().ToString() + "',");
                    strOLE.Append("'" + this.acaTextBox.Text.Trim().ToString() + "','" + this.speTextBox.Text.Trim().ToString() + "')");
                    myClass mC = new myClass();
                    mC.getCommon(strOLE.ToString()); 
                    strOLE.Remove(0, strOLE.Length);
                    MessageBox.Show("添加成功");
                    showData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                finally
                {
                }
            }
        }

        private bool IsSameRecord()
        {
            string Str_condition = "";
            Str_condition = this.numberTextBox.Text.Trim();

            StringBuilder strOLE = new StringBuilder();
            strOLE.Append("select * from 学生 ");
            strOLE.Append("where 学号='" + Str_condition + "'");

            myClass mC = new myClass();
            DataSet myDS = mC.getDS(strOLE.ToString(), "学生");
            strOLE.Remove(0, strOLE.Length);

            if (myDS.Tables["学生"].Rows.Count > 0)
            {
                MessageBox.Show("已存在相同的员工号,请重新更改员工号!");
                return true;             
            }
            else
            {                
                return false;
            }
        }

        private void del(object sender, EventArgs e)
        { 

            if (this.numberTextBox.Text == "")
            {
                MessageBox.Show("请选择你要删除的学生");
                return;
            }
            else
            {
                try
                {
                    StringBuilder strOLE = new StringBuilder();
                    strOLE.Append("delete from 学生 where 学号='"+ this.numberTextBox.Text.Trim().ToString() +"'");
                    myClass mC = new myClass();
                    mC.getCommon(strOLE.ToString());
                    strOLE.Remove(0, strOLE.Length);
                    MessageBox.Show("删除成功");
                    showData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                finally
                {
                }
            }
        }

        private void edit(object sender, EventArgs e)
        {
            if (this.nameTextBox.Text != "")
            {
                string currentDataStr = "";
                currentDataStr = this.dataGridView1[0, this.dataGridView1.CurrentCell.RowIndex].Value.ToString();

                StringBuilder sBuilder = new StringBuilder();
                sBuilder.Append("update 学生 set 学号='" + this.numberTextBox.Text + "',");
                sBuilder.Append("姓名='" + this.nameTextBox.Text + "', 性别='" + this.sexTextBox.Text + "',");
                sBuilder.Append("年龄='" + this.ageTextBox.Text + "', 学院='" + this.acaTextBox.Text + "',");
                sBuilder.Append("专业='" + this.speTextBox.Text + "' where 学号='" + currentDataStr + "'");

                try
                {
                    myClass mC = new myClass();
                    mC.getCommon(sBuilder.ToString());
                    MessageBox.Show("恭喜,修改成功!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                showData();
            }
            else
            {
                MessageBox.Show("请输入学号!"); 
            }
        }

        private void clean(object sender, EventArgs e)
        {
            this.nameTextBox.Text = "";
            this.numberTextBox.Text = "";
            this.sexTextBox.Text = "";
            this.ageTextBox.Text = "";
            this.acaTextBox.Text = "";
            this.speTextBox.Text = "";
        }

        private void find(object sender, EventArgs e)
        {
            string findStr = this.searchTextBox.Text;
            if (findStr == "")
            {
                MessageBox.Show("请输入查询条件!");
            }
            else
            {
                StringBuilder sBuilder = new StringBuilder();
                sBuilder.Append("select * from 学生 where 学号='"+ this.searchTextBox.Text +"' ");
                sBuilder.Append("or 姓名='"+ this.searchTextBox.Text +"' or 学院='"+ this.searchTextBox.Text +"'");

                try
                {
                    myClass mC = new myClass();
                    OleDbDataReader dr = mC.getRead(sBuilder.ToString());
                    if (dr.Read())
                    {
                        this.numberTextBox.Text = dr.GetString(0);
                        this.nameTextBox.Text = dr.GetString(1);
                        this.sexTextBox.Text = dr.GetString(2);
                        this.ageTextBox.Text = dr.GetString(3);
                        this.acaTextBox.Text = dr.GetString(4);
                        this.speTextBox.Text = dr.GetString(5);
                        MessageBox.Show("查找成功!");
                    }
                    else
                    {
                        MessageBox.Show("不存在你要查找的结果!请重新输入!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -