www.pudn.com > kaoshi.rar > AddQuestionForm.cs, change:2007-12-26,size:6480b


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; 
 
namespace MySchool 
{ 
    /// <summary> 
    /// 添加试题窗体 
    /// </summary> 
    public partial class AddQuestionForm : Form 
    { 
        public AddQuestionForm() 
        { 
            InitializeComponent(); 
        } 
 
        // 关闭窗体 
        private void btnClose_Click(object sender, EventArgs e) 
        { 
            this.Close(); 
        } 
 
        // 窗体加载时,将科目读出添加到科目组合框中 
        private void AddQuestionForm_Load(object sender, EventArgs e) 
        { 
            // 查询科目的 SQL 语句 
            string sql = "SELECT SubjectName FROM Subject"; 
             
            try 
            { 
                // 创建Command 对象 
                SqlCommand command = new SqlCommand(sql, DBHelper.connection); 
                DBHelper.connection.Open(); 
                // 执行查询 
                SqlDataReader dataReader = command.ExecuteReader(); 
 
                // 循环读取记录 
                while (dataReader.Read()) 
                { 
                    cboSubject.Items.Add(dataReader["SubjectName"].ToString()); 
                } 
                dataReader.Close();  // 关闭 DataReader 对象 
            } 
            catch (Exception ex) 
            { 
                Console.WriteLine(ex.Message); 
            } 
            finally 
            { 
                DBHelper.connection.Close(); 
            } 
        } 
 
        // 单击“增加”按钮时,向数据库增加试题 
        private void btnAdd_Click(object sender, EventArgs e) 
        { 
            // 输入验证 
            if (ValidateInput()) 
            { 
                // 查询科目 id 的 SQL 语句 
                string sql = string.Format("SELECT SubjectID FROM Subject WHERE SubjectName='{0}'", 
                    cboSubject.Text.Trim()); 
 
                try 
                { 
                    // 创建 Command 对象 
                    SqlCommand command = new SqlCommand(sql, DBHelper.connection); 
                    // 打开数据库连接 
                    DBHelper.connection.Open(); 
                    // 执行查询 
                    SqlDataReader dataReader = command.ExecuteReader(); 
                    int difficulty = 0;  // 难度 
                    int subjectId = 0;   // 科目编号 
                    string answer = "";  // 答案 
 
                    // 获得科目编号 
                    if (dataReader.Read()) 
                    { 
                        subjectId = (int)dataReader["SubjectID"]; 
                    } 
                    dataReader.Close(); 
 
                    // 确定难度 
                    if (rdoDifficult.Checked) 
                    { 
                        difficulty = Convert.ToInt32(rdoDifficult.Text); 
                    } 
                    else if (rdoNormal.Checked) 
                    { 
                        difficulty = Convert.ToInt32(rdoNormal.Text); 
                    } 
                    else if (rdoEasy.Checked) 
                    { 
                        difficulty = Convert.ToInt32(rdoEasy.Text); 
                    } 
 
                    // 确定答案 
                    if (rdoA.Checked)  
                    { 
                        answer = rdoA.Text; 
                    } 
                    else if (rdoB.Checked) 
                    { 
                        answer = rdoB.Text; 
                    } 
                    else if (rdoC.Checked) 
                    { 
                        answer = rdoC.Text; 
                    } 
                    else if (rdoD.Checked) 
                    { 
                        answer = rdoD.Text; 
                    } 
 
                    // 插入记录的 SQL 语句 
                    sql = string.Format( 
                        "INSERT INTO Question (Question,OptionA,OptionB,OptionC,OptionD,Answer,Difficulty,SubjectID) values ('{0}','{1}','{2}','{3}','{4}','{5}',{6},{7})", 
                        txtQuestion.Text.Trim(), txtOptionA.Text.Trim(), txtOptionB.Text.Trim(), txtOptionC.Text.Trim(), txtOptionD.Text.Trim(), answer, difficulty, subjectId); 
                    command.CommandText = sql;// 重新指定SQL 命令 
                    int result = command.ExecuteNonQuery();  // 执行命令 
 
                    if (result == 1) 
                    { 
                        MessageBox.Show("增加记录成功", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                         
                    } 
                    else 
                    { 
                        MessageBox.Show("增加记录失败", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                    } 
                } 
                catch (Exception ex) 
                { 
                    Console.WriteLine(ex.Message); 
                } 
                finally 
                { 
                    DBHelper.connection.Close(); 
                } 
                // 清空试题和选项中的内容 
                txtQuestion.Text = ""; 
                txtOptionA.Text = ""; 
                txtOptionB.Text = ""; 
                txtOptionC.Text = ""; 
                txtOptionD.Text = ""; 
            }             
        } 
 
        /// <summary> 
        /// 用户输入验证 
        /// </summary> 
        /// <returns>验证通过返回True,失败返回False</returns>  
        private bool ValidateInput() 
        { 
            if (txtQuestion.Text.Trim() == "") 
            { 
                MessageBox.Show("请输入题目!","输入提示",MessageBoxButtons.OK,MessageBoxIcon.Information); 
                txtQuestion.Focus(); 
                return false; 
            }             
            else if(!rdoDifficult.Checked && !rdoNormal.Checked && !rdoEasy.Checked) 
            { 
                MessageBox.Show("请选择难度级别!", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                return false; 
            } 
            else if(cboSubject.Text.Trim() == "") 
            { 
                MessageBox.Show("请选择科目!", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                cboSubject.Focus(); 
                return false; 
            }             
            else 
            { 
                return true; 
            } 
        } 
    } 
}