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

📄 formstudentsearch.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 FormStudentSearch : Form
    {
        public FormStudentSearch()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化窗体中控件的数据
        /// </summary>
        void InitData()
        {
            //查询数据表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"));
            }
            if (comboBoxDepartment.Items.Count > 0)
                comboBoxDepartment.SelectedIndex = 0;

            //“入学年份”下拉框数据
            comboBoxEnterYear.SelectedIndex = 0;

            //“性别”按钮
            radioButtonMale.Checked = true;
        }

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

        /// <summary>
        /// 查询数据,并绑定到DataGridView中
        /// </summary>
        void Query()
        {
            Database db = new Database();

            string sql = "";
            bool hasCond = false;

            sql += "select SNo,SName,SAge,SGender,DepartmentName,SEnterYear from [Student],[Department]";

            //学号
            if (textBoxSNo.Text != "")
            {
                if (!hasCond)
                {
                    sql += " where ";
                    hasCond = true;
                }
                else
                {
                    sql += " and ";
                }
                sql += " SNo = " + SqlStringConstructor.GetQuotedString(textBoxSNo.Text);
            }

            //姓名
            if (textBoxSName.Text != "")
            {
                if (!hasCond)
                {
                    sql += " where ";
                    hasCond = true;
                }
                else
                {
                    sql += " and ";
                }
                sql += " SName = " + SqlStringConstructor.GetQuotedString(textBoxSName.Text);
            }

            //年龄
            if (textBoxSAge.Text != "")
            {
                if (!hasCond)
                {
                    sql += " where ";
                    hasCond = true;
                }
                else
                {
                    sql += " and ";
                }
                sql += " SAge = " + textBoxSAge.Text;
            }

            //性别
            if (radioButtonAll.Checked == false)
            {
                if (!hasCond)
                {
                    sql += " where ";
                    hasCond = true;
                }
                else
                {
                    sql += " and ";
                }
                if (radioButtonMale.Checked)
                    sql += " SGender = " + SqlStringConstructor.GetQuotedString("男");
                else
                    sql += " SGender = " + SqlStringConstructor.GetQuotedString("女");
            }

            //院系
            if (comboBoxDepartment.SelectedItem.ToString() != "")
            {
                if (!hasCond)
                {
                    sql += " where ";
                    hasCond = true;
                }
                else
                {
                    sql += " and ";
                }
                sql += " [Department].DepartmentName = " + SqlStringConstructor.GetQuotedString(comboBoxDepartment.SelectedItem.ToString());
            }

            //入学年份
            if (comboBoxEnterYear.SelectedItem.ToString() != "")
            {
                if (!hasCond)
                {
                    sql += " where ";
                    hasCond = true;
                }
                else
                {
                    sql += " and ";
                }
                sql += " SEnterYear = " + comboBoxEnterYear.SelectedItem.ToString();
            }

            if (!hasCond)
            {
                sql += " where ";
                hasCond = true;
            }
            else
            {
                sql += " and ";
            }
            sql += " [Student].SDepartmentId = [Department].DepartmentId";

            DataTable dt = db.GetDataTable(sql);
            this.dataGridViewPreView.DataSource = dt;
        }

        /// <summary>
        /// “查询”按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonSearch_Click(object sender, EventArgs e)
        {
            this.Query();
        }

        /// <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();
        }
    }
}

⌨️ 快捷键说明

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