addexamscoreform.cs

来自「一个不错的文档」· CS 代码 · 共 202 行

CS
202
字号
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.ExamInfo
{
    public partial class AddExamScoreForm : Form
    {
        public AddExamScoreForm()
        {
            InitializeComponent();
        }
        //获取学生编号信息添加到下拉框
        public void getStuId()
        {
            //数据库连接
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = DBHelper.con;
            string sql = "select stuId from student";
            cmd.CommandText = sql;
            DBHelper.con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                string str = reader["stuId"].ToString();
                cmbStuID.Items.Add(str);
            }
            cmbStuID.SelectedIndex = 0;
            cmbStuID.DropDownStyle = ComboBoxStyle.DropDownList;
            reader.Close();
            DBHelper.con.Close();

        }
        //获取课程信息添加到下拉框
        public void getCourse()
        {
            try
            {
                DBHelper.con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = DBHelper.con;
                cmd.CommandType = CommandType.Text;
                string sql = "select * from course";
                cmd.CommandText = sql;
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    examInfo ei = new examInfo();
                    ei.CourseId = reader["courseId"].ToString();
                    ei.CourseName = reader["courseName"].ToString();
                    //下拉框显示时会自动调用classInfo对象的ToString()方法
                    cmbcourse.Items.Add(ei);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelper.con.Close();
            }
            cmbcourse.DropDownStyle = ComboBoxStyle.DropDownList;
            cmbcourse.SelectedIndex = 0;
        }
        
        //判断
        public void pd(exam e)
        {
            try
            {
                DBHelper.con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = DBHelper.con;
                cmd.CommandType = CommandType.Text;
                string sql = "select count(*) from exam where courseId='" + e.CourseID + "' and stuId='" + e.StuID + "'";
                cmd.CommandText = sql;
                int b = (int)cmd.ExecuteScalar();
                if (b < 1)
                {
                    addExam(getInteface());
                }
                else
                {
                    MessageBox.Show("该生已有本门课程");
                }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelper.con.Close();       
            }
        }

        //把考试信息提交到数据库
        public void addExam(exam e)
        {
            try
            {
                DBHelper.con.Close();
                DBHelper.con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = DBHelper.con;
                cmd.CommandType = CommandType.Text;
                string sql = string.Format("insert into exam values('{0}','{1}',{2},{3})",
                e.StuID, e.CourseID, e.WrittenScore, e.LadScore);
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
                

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show("此行只能输入数字");
            }
            finally
            {
                DBHelper.con.Close();
            }
        }
        //获取界面数据
        public exam getInteface()
        {
            exam e = new exam();
            e.StuID = cmbStuID.Text;
            e.CourseID = ((examInfo)cmbcourse.SelectedItem).CourseId; 
            e.WrittenScore = int.Parse(txtWrittenScore.Text.Trim());
            e.LadScore = Convert.ToInt32(txtLabScore.Text.Trim());
            return e;
        }
        //设置初始化信息
        private void AddExamScoreForm_Load(object sender, EventArgs e)
        {
            getStuId();
            getCourse();
        }
        //提交信息
        private void button3_Click(object sender, EventArgs e)
        {
            pd(getInteface());
            this.Close();
        }
        //关闭窗体
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //设置笔试成绩属性
        private void txtWrittenScore_KeyPress(object sender, KeyPressEventArgs e)
        {
            
            if (e.KeyChar < '0' && e.KeyChar != '.' || e.KeyChar > '9' && e.KeyChar != '.')
            {
                e.Handled = true;
            }
        }
        //设置机试成绩属性
        private void txtLabScore_KeyPress(object sender, KeyPressEventArgs e)
        {
           
            if (e.KeyChar < '0' && e.KeyChar != '.' || e.KeyChar > '9' && e.KeyChar != '.')
            {
                e.Handled = true;
            }

        }

        private void txtWrittenScore_TextChanged(object sender, EventArgs e)
        {
            if (Convert.ToDouble(txtWrittenScore.Text) > 100)
            {
                txtWrittenScore.Text = "100";
            }

        }

        private void txtLabScore_TextChanged(object sender, EventArgs e)
        {
            if (Convert.ToDouble(txtLabScore.Text) > 100)
            {
                txtLabScore.Text = "100";
            }
        }

            
        
    }
}

⌨️ 快捷键说明

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