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


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 DetailsForm : Form 
    { 
        public int questionId = 0;  // 试题的唯一Id号 
 
        public DetailsForm() 
        { 
            InitializeComponent(); 
        } 
 
        // 关闭窗体 
        private void btnClose_Click(object sender, EventArgs e) 
        { 
            this.Close(); 
        } 
 
        // 窗体加载时,显示题目详情 
        private void DetailsForm_Load(object sender, EventArgs e) 
        { 
            int subjectId = 0; // 科目编号 
            string answer = "";// 答案 
            // 查询用的 sql 语句 
            string sql = string.Format("SELECT * FROM Question WHERE QuestionId={0}",questionId); 
 
            try 
            { 
                // 创建 Command 对象 
                SqlCommand command = new SqlCommand(sql, DBHelper.connection); 
                DBHelper.connection.Open(); 
                // 执行查询命令 
                SqlDataReader dataReader = command.ExecuteReader(); 
 
                // 读取题目详细信息 
                if (dataReader.Read()) 
                { 
                    txtQuestion.Text = dataReader["Question"].ToString();// 题目 
                    txtOptionA.Text = dataReader["OptionA"].ToString(); // 选项A 
                    txtOptionB.Text = dataReader["OptionB"].ToString(); // 选项B 
                    txtOptionC.Text = dataReader["OptionC"].ToString(); // 选项C 
                    txtOptionD.Text = dataReader["OptionD"].ToString(); // 选项D 
                    answer = (string)dataReader["Answer"];        // 答案 
                     
                    // 确定哪个答案单选按钮被选中 
                    switch (answer) 
                    {  
                        case "A": 
                            rdoA.Checked = true; 
                            break; 
                        case "B": 
                            rdoB.Checked = true; 
                            break; 
                        case "C": 
                            rdoC.Checked = true; 
                            break; 
                        case "D": 
                            rdoD.Checked = true; 
                            break; 
                        default: 
                            break; 
                    } 
 
                    int difficulty = (int)dataReader["Difficulty"]; // 难度 
                     
                    // 确定哪个难度单选按钮被选中 
                    switch (difficulty) 
                    { 
                        case 1:  // 简单 
                            rdoEasy.Checked = true; 
                            break; 
                        case 2:  // 一般 
                            rdoNormal.Checked = true; 
                            break; 
                        case 3:  // 难 
                            rdoDifficult.Checked = true; 
                            break; 
                        default: 
                            break; 
                    } 
 
                    subjectId = (int)dataReader["SubjectId"]; // 科目编号                     
                } 
                dataReader.Close(); 
 
                // 处理科目组合框,将科目读取出来显示在组合框中 
                sql = "SELECT SubjectId, SubjectName FROM Subject"; 
                command.CommandText = sql; 
                dataReader = command.ExecuteReader(); 
                while (dataReader.Read()) 
                { 
                    cboSubject.Items.Add((string)dataReader["SubjectName"]); 
 
                    if (subjectId == (int)dataReader["SubjectId"]) 
                    { 
                        cboSubject.Text = (string)dataReader["SubjectName"]; 
                    } 
                } 
                dataReader.Close(); 
            } 
            catch (Exception ex) 
            { 
                Console.WriteLine(ex.Message); 
            } 
            finally 
            { 
                DBHelper.connection.Close(); 
            } 
        } 
 
        // 修改试题功能 
        private void btnModify_Click(object sender, EventArgs e) 
        { 
            int difficulty = 1; // 难度 
            string answer = ""; // 答案 
 
            // 确定难度 
            if (rdoDifficult.Checked) 
            { 
               difficulty = 3; 
            } 
            else if (rdoNormal.Checked) 
            { 
                difficulty = 2; 
            } 
            else 
            { 
                difficulty = 1; 
            }    
 
            // 确定答案 
            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; 
            }                     
 
            // 首先获得科目的编号 
            int subjectId = 0; 
 
            // 查询科目编号 
            string sql = string.Format("SELECT SubjectId FROM Subject WHERE SubjectName = '{0}'",cboSubject.Text); 
 
            try 
            { 
                // 创建Command 对象 
                SqlCommand command = new SqlCommand(sql, DBHelper.connection); 
                DBHelper.connection.Open(); 
                SqlDataReader dataReader = command.ExecuteReader(); 
                if (dataReader.Read()) 
                { 
                    subjectId = (int)dataReader["SubjectId"]; 
                } 
                dataReader.Close();  // 关闭 DataReader 对象                 
 
                // 修改试题 
                // 修改用的 SQL 语句 
                sql = string.Format( 
                    "UPDATE Question SET Question='{0}', OptionA='{1}',OptionB='{2}',OptionC='{3}',OptionD='{4}',Answer='{5}',Difficulty={6},SubjectId={7} WHERE QuestionId={8}", 
                    txtQuestion.Text.Trim(),txtOptionA.Text.Trim(),txtOptionB.Text.Trim(),txtOptionC.Text.Trim(),txtOptionD.Text.Trim(),answer,difficulty,subjectId,questionId); 
                // 创建 Command 对象 
                command.CommandText = 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(); 
            } 
            this.Close(); 
        } 
    } 
}