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

📄 setkaochang.cs

📁 程序设计语言上机考试系统
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1.KaowuManage
{
    public partial class SetKaochang : Form
    {
        baseclass.OperateAndValidate opAndvalidate = new baseclass.OperateAndValidate();
        SqlConnection cn = new SqlConnection("SERVER=(local);UID=sa;PWD=sa;Trusted_Connection=True;DATABASE=test1");
        baseclass.BaseOperate boperate = new baseclass.BaseOperate();
        protected string M_str_sql = "select Kname as 考场名称,Address as 考场地址,Capcity as 考场容量  from kaochang ";
        protected string M_str_table = "kaochang";
        int capcity; //考场容量
        public SetKaochang()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SetKaochang_Load(object sender, EventArgs e)
        {
            DataSet myds = boperate.getds(M_str_sql, M_str_table);
            dataGridView1.DataSource = myds.Tables[0];
            if (myds.Tables[0].Rows.Count <= 0)
            {
                dataGridView1.AllowUserToAddRows = true;
            }
            if (myds.Tables[0].Rows.Count > 0)
                btnDelete.Enabled = true;
            else
                btnDelete.Enabled = false;
        }
        /// <summary>
        /// 添加考场信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (!opAndvalidate.validateNum(txtnum.Text.Trim()))
                    errorPnum.SetError(txtnum, "请输入正整数");
                if (txtname.Text.Trim() == "" || txtaddress.Text.Trim() == "" || txtnum.Text.Trim() == "")
                    MessageBox.Show("请填写完整的信息", "提示");
                else
                {
                    errorPnum.Clear();
                    //判断输入的字符串长度是否超过数据库中定义的长度
                    if (Ifenogh())
                    {
                        if (cn.State == ConnectionState.Closed)
                            cn.Open();
                        capcity = Convert.ToInt32(txtnum.Text.Trim()); //获取考场容量
                        SqlCommand cmd = new SqlCommand("select * from kaochang where Kname ='" + txtname.Text.ToString().Trim() + "'", cn);
                        //判断数据库中是否存在和要添加的考场名相同的考场
                        if (null != cmd.ExecuteScalar())
                        {
                            MessageBox.Show("已存在该考场名", "提示");
                            if (cn.State == ConnectionState.Open)
                                cn.Close();
                        }
                        else
                        {
                            //添加考场
                            string kname = txtname.Text.ToString().Trim().Replace("''","'"); //考场名称
                            string address = txtaddress.Text.Trim().Replace("''", "'"); //考场容量
                            boperate.getcom("insert into kaochang(Kname,Address,Capcity) values('" + kname + "','" + address + "','" + capcity + "')");
                            MessageBox.Show("信息添加成功", "提示");
                            //加载考场表
                            SetKaochang_Load(sender, e);
                            if (cn.State == ConnectionState.Open)
                                cn.Close();
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region  判断输入的字符串长度是否超过数据库中定义的长度
        public bool Ifenogh()
        {
            if (boperate.IfLong(txtname.Text.Trim(), 50))
            {
                MessageBox.Show("考场名称的长度超过数据库定义的50个字节!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (boperate.IfLong(txtaddress.Text.Trim(), 100))
            {
                MessageBox.Show("考场地址的长度超过数据库定义的100个字节!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            else
                return true;
        }
        #endregion
        
        
        /// <summary>
        /// 删除考场信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("确定要删除吗?", "删除确定", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    boperate.getcom("delete from kaochang where Kname='" + Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim()+ "'");
                    SetKaochang_Load(sender, e);
                    MessageBox.Show("删除数据成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// 修改考场信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdata_Click(object sender, EventArgs e)
        {
            if (cn.State == ConnectionState.Closed)
                cn.Open();
            string kname;
            bool canup = true;
            if (txtname.Text.Trim() == "" || txtaddress.Text.Trim() == "" || txtnum.Text.Trim() == "")
                MessageBox.Show("请填写完整的信息", "提示");
            else
            {
                if (Ifenogh())
                {
                    if (!opAndvalidate.validateNum(txtnum.Text.Trim()))
                        errorPnum.SetError(txtnum, "请输入正整数");
                    else
                    {
                        errorPnum.Clear();
                        //将考场容量转化为整数类型
                        capcity = Convert.ToInt32(txtnum.Text.Trim());
                        //选中的考场的名字
                        kname = Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim();
                        //选择项在dataGridView1中的索引值
                        int currindex = Convert.ToInt32(dataGridView1.CurrentCell.RowIndex);
                        //检查数据库中是否有和将要修改的名字相同的考场
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            if (i != currindex)
                            {
                                if (string.Compare(dataGridView1.Rows[i].Cells[0].Value.ToString().Trim(), kname, true) == 0)
                                {
                                    MessageBox.Show("已存在该考场名", "提示");
                                    canup = false;
                                    break;
                                }
                            }
                        }
                        //如果canup值为true,则可以更新
                        if (canup)
                        {
                            boperate.getcom("update kaochang set Kname ='" + txtname.Text.Trim() + "',Address ='" + txtaddress.Text.Trim() + "',Capcity ='" + capcity + "' where Kname ='" + kname + "'");
                            SetKaochang_Load(sender, e);
                            MessageBox.Show("信息修改成功", "提示");
                        }
                    }
                }
            }
            if (cn.State == ConnectionState.Open)
               cn.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
                dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.White;
            dataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.LightSlateGray;
            dataGridView1.CurrentRow.Selected = true;
            txtname.Text = Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim();
            txtaddress.Text = Convert.ToString(dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value).Trim();
            txtnum.Text = Convert.ToString(dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value).Trim();
        }

        #region 在datagridview中实现鼠标右键单击删除的功能
        private void tSMItemDelete_Click(object sender, EventArgs e)
        {
            //删除选中的多条数据
            boperate.delseldata(dataGridView1, "delete from kaochang where kname = ");
            SetKaochang_Load(sender, e);
            MessageBox.Show("删除数据成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        #endregion
    }
}

⌨️ 快捷键说明

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