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

📄 courseeditform.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.SqlClient;
using MySchoolPrj.Entity;
using MySchoolPrj.DAO;


namespace MySchoolPrj.CourseInfo
{
    public partial class CourseEditForm : Form
    {
        public CourseEditForm()
        {
            InitializeComponent();
        }
        public bool flag=true;
        //State枚举
        private State state;
        public State State
        {
            get { return state; }
            set { state = value; }
        }
        //Course类
        private course cour;
        public course Cour
        {
            get { return cour; }
            set { cour = value; }
        }
        //增加
        //把学生信息提交到数据库
        public void addCourse(course s)
        {

            try
            {
                //连接数据库
                //打开数据库
                DBHelper.con.Open();
                //创建Command对象
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = DBHelper.con;
                cmd.CommandType = CommandType.Text;
                //增加用的SqL语句
                string sql = string.Format("insert into course values('{0}','{1}','{2}')", s.CourseId, s.CourseName, s.Memo);
                cmd.CommandText = sql;
                //执行命令
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                //操作出现异常
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //关闭数据库
                DBHelper.con.Close();
            }
        }
        //获取界面数据
        public course getInteface()
        {

            course s = new course();
            s.CourseId = txtCourseId.Text.Trim();
            s.CourseName = txtCourseName.Text.Trim();
            s.Memo = txtCourseMemo.Text.Trim();
            return s;
        }
        //修改课程信息
        public void UpdateTea(course s)
        {
            try
            {
                DBHelper.con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = DBHelper.con;
                cmd.CommandType = CommandType.Text;
                //修改用的Sqly语句
                string sql = string.Format("update course set courseId='{0}',courseName='{1}'," +
                    "Memo='{2}'where courseId='{3}'", s.CourseId, s.CourseName, s.Memo, cour.CourseId);
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();//执行命令
                txtCourseName.Focus();//焦点放在CourseName中
            }
            catch (Exception ex)
            {// 错误操作
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //关闭数据库
                DBHelper.con.Close();
            }
        }
        //把对象的数据显示到界面
        public void showData()
        {
            txtCourseId.Text = cour.CourseId;
            txtCourseName.Text = cour.CourseName;
            txtCourseMemo.Text = cour.Memo;

        }
        //增加窗体校验
        public bool validCourseisNull()
        {

            if (txtCourseName.Text == "")
            {
                MessageBox.Show("课程名不能为空");
                return false;

            }
            else
            {
                return true;
            }


        }
         //保存事件
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (flag)
            {
                if (state == State.dsAdd)
                {
                    if (validCourseisNull())
                    {
                        addCourse(getInteface());
                    }
                    else { return; }
                }
                else
                {
                    UpdateTea(getInteface());
                }
                this.Close();
            }
            else {
                MessageBox.Show("出错了!看到有提示还点确定!");
            
            }
        }

        //重置 事件  
        //清空界面
        private void btnReset_Click(object sender, EventArgs e)
        {
            if (state == State.dsAdd)
            {
                txtCourseId.Text = "";
                txtCourseName.Text = "";
                txtCourseMemo.Text = "";
                label5.Text = "";
            }
            else
            {//修改时CourseId不重置
                txtCourseName.Text = "";
                txtCourseMemo.Text = "";
                label5.Text = "";
            }

        }

        //退出事件
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //增加时CourseId不能重复
        //在Txtbox 后加上提示
        private void txtCourseId_Leave(object sender, EventArgs e)
        {
            string s = txtCourseId.Text;
            if ("".Equals(s))
            {
                label5.Text = "课程编号不能为空!";
                flag = false;
                return;
            }
            if (s.Length != 4)
            {
                label5.Text = "编号必须为4位,例如C008";
                flag = false;
                return;
            }
            if (s[0] != 'C')
            {
                label5.Text = "课程编号首字母必须为C";
                flag = false;
                return;
            }
            int i = 1;
            for (i = 1; i < s.Length; i++)
            {
                if (s[i] < '0' || s[i] > '9')
                {
                    label5.Text = "课程编号剩余3位必须为纯数字";
                    flag = false;
                    return;
                }
            }

            if (state == State.dsAdd)
            {
                // 获取界面CourseId数据
                string id = txtCourseId.Text;
                //从数据库提取数据
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = DBHelper.con;
                cmd.CommandType = CommandType.Text;
                //查询数据库是否有相同的课程编号SQL语句
                string sql = string.Format("select courseId from course where courseId='{0}'", id);
                cmd.CommandText = sql;
                DBHelper.con.Open();
                SqlDataReader dataRaeader = cmd.ExecuteReader();//查询查询用户命令
                dataRaeader.Read();
                //用HasRows属性来判断查询结果是否存在
                if (dataRaeader.HasRows)
                {
                    label5.Text = "该Id已存在,重新输入";
                    txtCourseId.Focus();//放置焦点
                    //选定文本框中所有对象
                    txtCourseId.SelectAll();
                    flag = false;
                }
                else
                {
                    label5.Text = "该Id可以使用";
                    flag = true;
                }
                //关闭数据
                dataRaeader.Close();
                DBHelper.con.Close();
            }

        }
        //修改时用的窗体初始化
        private void CourseEditForm_Load_1(object sender, EventArgs e)
        {
            if (state == State.dsUpdate)
            {
                //关闭该控件
                txtCourseId.Enabled = false;
                //把对象的数据显示到界面
                showData();
            }
        }
    }

}
       
 
        
        
      
     
  

⌨️ 快捷键说明

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