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

📄 formstudentupdate.cs

📁 学生管理系统Csharp2005 学生管理系统Csharp2005 学生管理系统Csharp2005
💻 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.SqlClient;

using StudentManager.DbLayer;

namespace StudentManager
{
    public partial class FormStudentUpdate : Form
    {
        public FormStudentUpdate()
        {
            InitializeComponent();
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        /// <summary>
        /// 初始化窗体中控件的数据
        /// </summary>
        void InitData()
        {

            //1 查询数据表Department中的院系数据,并绑定到“院系”下拉框中
            //构造查询SQL
            string sql = "";
            sql += "select * from [Department]";
            //查询
            Database db = new Database();
            SqlDataReader dr = db.GetDataReader(sql);
            //绑定到下拉框中
            comboBoxDepartment.Items.Clear();
            while (dr.Read())
            {
                comboBoxDepartment.Items.Add(GetSafeData.ValidateDataReader_S(dr, "DepartmentName"));
            }
        }

        /// <summary>
        /// 根据用户输入的学号,查询学生的详细信息,并显示在各个输入框中
        /// </summary>
        void LoadOldValues()
        {
            if (textBoxSNo.Text == "")
                return;

            //构造查询SQL
            string sql = "";
            sql += "select SNo,SName,SAge,SGender,DepartmentName,SEnterYear ";
            sql += "from [Student],[Department] where [Student].SNo = ";
            sql += SqlStringConstructor.GetQuotedString(textBoxSNo.Text);
            sql += " and [Student].SDepartmentId = [Department].DepartmentId";

            //查询
            Database db = new Database();
            DataTable dt = db.GetDataTable(sql);

            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("数据库中没有学号为" + textBoxSNo.Text + "的数据");
                return;
            }

            //绑定到DataGriw
            this.dataGridViewPreView.DataSource = dt;

            //绑定到各个输入框
            textBoxSName.Text = GetSafeData.ValidateDataRow_S(dt.Rows[0], "SName");//姓名
            textBoxSAge.Text = GetSafeData.ValidateDataRow_S(dt.Rows[0], "SAge");//年龄

            //性别
            if (GetSafeData.ValidateDataRow_S(dt.Rows[0], "SGender").Trim() == "男")
                radioButtonMale.Checked = true;
            else
                radioButtonFemale.Checked = true;

            //院系
            int i = 0;
            foreach (string departmentName in comboBoxDepartment.Items)
            {
                if (comboBoxDepartment.Items[i].ToString() == GetSafeData.ValidateDataRow_S(dt.Rows[0], "DepartmentName"))
                {
                    comboBoxDepartment.SelectedIndex = i;
                    break;
                }
                i++;
            }

            //入学年份
            i = 0;
            foreach (string enterYear in comboBoxEnterYear.Items)
            {
                if (comboBoxEnterYear.Items[i].ToString() == GetSafeData.ValidateDataRow_S(dt.Rows[0], "SEnterYear"))
                {
                    comboBoxEnterYear.SelectedIndex = i;
                    break;
                }
                i++;
            }
        }

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormUpdateStudents_Load(object sender, EventArgs e)
        {
            this.InitData();
        }

        /// <summary>
        /// “修改”按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            //用户输入检查
            if (textBoxSName.Text == "")
            {
                MessageBox.Show("姓名不能为空!");
                return;
            }

            string sql = "";
            Database db = new Database();
            int departmentId = 0;

            //从Department表中,检索DepartmentId
            sql = "select DepartmentId from [Department] where DepartmentName = ";
            sql += SqlStringConstructor.GetQuotedString(comboBoxDepartment.SelectedItem.ToString());
            DataRow row = db.GetDataRow(sql);
            departmentId = GetSafeData.ValidateDataRow_N(row, "DepartmentId");

            //构造Update语句        
            sql = "";
            sql += "Update [student] set ";
            sql += "SNo = " + SqlStringConstructor.GetQuotedString(textBoxSNo.Text) + ",";//学号
            sql += "SName = " + SqlStringConstructor.GetQuotedString(textBoxSName.Text) + ",";//姓名
            sql += "SAge = " + SqlStringConstructor.GetQuotedString(textBoxSAge.Text) + ",";//年龄

            //性别
            if (radioButtonMale.Checked)
                sql += "SGender = " + SqlStringConstructor.GetQuotedString(radioButtonMale.Text) + ",";
            else
                sql += "SGender = " + SqlStringConstructor.GetQuotedString(radioButtonFemale.Text) + ",";

            sql += "SDepartmentId = " + departmentId + ",";//院系
            sql += "SEnterYear = " + SqlStringConstructor.GetQuotedString(comboBoxEnterYear.SelectedItem.ToString());//入学年份

            //条件子句
            sql += " Where Sno = " + SqlStringConstructor.GetQuotedString(textBoxSNo.Text);//学号

            db.ExecuteSQL(sql);

            this.LoadOldValues();
            MessageBox.Show("修改成功!");
        }

        /// <summary>
        /// 重置用户的输入框
        /// </summary>
        void ResetInput()
        {
            textBoxSNo.Text = "";
            textBoxSName.Text = "";
            textBoxSAge.Text = "";

            textBoxSNo.Focus();
        }

        /// <summary>
        /// “重置”按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonReset_Click(object sender, EventArgs e)
        {
            ResetInput();
        }

        /// <summary>
        /// “关闭”按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 光标离开“学号”文本框时,自动加载该学号的学生信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxSNo_Leave(object sender, EventArgs e)
        {
            this.LoadOldValues();
        }
    }
}

⌨️ 快捷键说明

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