www.pudn.com > kaoshi.rar > SearchQuestionForm.cs, change:2007-12-13,size:8023b


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 SearchQuestionForm : Form 
    { 
        public SearchQuestionForm() 
        { 
            InitializeComponent(); 
        } 
 
        // 将文本框中的信息,添加到 ListView 中显示 
        private void btnSearch_Click(object sender, EventArgs e) 
        { 
            // 调用查询试题的方法,查询试题    
            SearchQuestionByDifficulty(); 
        } 
         
        // 点击删除菜单项,删除选中的记录 
        private void tsmiDelete_Click(object sender, EventArgs e) 
        { 
            if (lvQuestion.SelectedItems.Count <= 0) 
            { 
                MessageBox.Show("请选择一项", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
            } 
            else if(MessageBox.Show("确实要删除这条记录吗?","操作提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)  
            { 
                // 选中的记录的 id  
                int questionId = Convert.ToInt32(lvQuestion.SelectedItems[0].Tag); 
                // 创建删除用 sql 语句 
                string sql = string.Format("DELETE FROM Question WHERE QuestionID={0}", questionId); 
                int result = -1;  // 执行删除操作的结果 
                try 
                { 
                    // 创建 Command 对象 
                    SqlCommand command = new SqlCommand(sql, DBHelper.connection); 
                    // 打开数据库连接 
                    DBHelper.connection.Open();   
                    // 执行查询 
                    result = command.ExecuteNonQuery(); 
                } 
                catch (Exception ex) 
                { 
                    Console.WriteLine(ex.Message); 
                } 
                finally 
                { 
                    DBHelper.connection.Close(); 
                } 
 
                if (result != 1) 
                { 
                    MessageBox.Show("删除失败", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
                } 
                else 
                { 
                    MessageBox.Show("删除成功", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                    SearchQuestionByDifficulty();  // 重新查询填充 ListView 
                } 
            } 
        } 
 
        // 将选择的试题难度修改为相应的难度值 
        private void tsmiDifficulty_Click(object sender, EventArgs e) 
        { 
            if (lvQuestion.SelectedItems.Count <= 0) 
            { 
                MessageBox.Show("请选择一项", "操作提示", 
                    MessageBoxButtons.OK, MessageBoxIcon.Information); 
            } 
            else 
            { 
                // 获得选中的菜单项的Tag值,其中记录着难度值 
                int difficulty = Convert.ToInt32(((ToolStripMenuItem)sender).Tag); 
 
                // 调用方法,修改难度 
                UpdateDifficulty(difficulty); 
 
                // 重新查询结果显示 
                SearchQuestionByDifficulty();  // 该方法在第2阶段实现 
            }            
        }    
 
        // 关闭窗体 
        private void btnClose_Click(object sender, EventArgs e) 
        { 
            this.Close(); 
        } 
 
        // 双击 ListView 时,弹出详情窗体 
        private void lvQuestion_MouseDoubleClick(object sender, MouseEventArgs e) 
        { 
            if (lvQuestion.SelectedItems.Count > 0) 
            { 
                // 创建窗体对象 
                DetailsForm detailsForm = new DetailsForm(); 
                // 设置 MDI 父窗体 
                detailsForm.MdiParent = this.MdiParent; 
 
                // 将选中的试题的 Id 传递给详情窗体 
                detailsForm.questionId = Convert.ToInt32(lvQuestion.SelectedItems[0].Tag); 
                // 显示窗体 
                detailsForm.Show(); 
            }             
        } 
 
        /// <summary> 
        /// 根据选择的难度,查询试题显示 
        /// </summary>  
        private void SearchQuestionByDifficulty() 
        { 
            // 获取难度组合框的值 
            string difficulty = cboDifficulty.Text.Trim(); 
 
            if (difficulty == "") // 如果为空,给出提示 
            { 
                MessageBox.Show("请选择难度级别", "查询提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
            } 
            else 
            { 
                // 清空 ListView 中的数据 
                lvQuestion.Items.Clear(); 
 
                string sql = string.Format("SELECT QuestionID,Question,OptionA,OptionB,OptionC,OptionD,Answer,Difficulty FROM Question WHERE Difficulty={0}", 
                    int.Parse(difficulty)); 
 
                try 
                { 
                    // 创建 Command 对象 
                    SqlCommand command = new SqlCommand(sql, DBHelper.connection); 
                    // 打开数据库连接 
                    DBHelper.connection.Open(); 
 
                    // 执行查询 
                    SqlDataReader dataReader = command.ExecuteReader();  // 执行查询 
                    // 逐行读取记录 
                    while (dataReader.Read()) 
                    { 
                        // 创建一个列表视图项 
                        ListViewItem item = new ListViewItem(dataReader["Question"].ToString()); 
                        item.Tag = (int)dataReader["QuestionID"]; 
                        // 将列表视图项添加到列表视图中 
                        lvQuestion.Items.Add(item); 
                        // 为列表视图项添加子项 
                        item.SubItems.AddRange(new string[]{ 
                            dataReader["OptionA"].ToString(), 
                            dataReader["OptionB"].ToString(), 
                            dataReader["OptionC"].ToString(), 
                            dataReader["OptionD"].ToString(), 
                            dataReader["Answer"].ToString(), 
                            dataReader["Difficulty"].ToString()}); 
                    } 
                    // 关闭 DataReader 对象 
                    dataReader.Close(); 
                } 
                catch (Exception ex) 
                { 
                    Console.WriteLine(ex.Message); 
                } 
                finally 
                { 
                    DBHelper.connection.Close(); 
                } 
            } 
        } 
 
        /// <summary> 
        /// 修改试题的难度 
        /// </summary> 
        /// <param name="difficulty">试题难度</param>  
        private void UpdateDifficulty(int difficulty) 
        { 
            // 获取选中的记录对应的问题 Id 号 
            int questionId = Convert.ToInt32(lvQuestion.SelectedItems[0].Tag); 
 
            // 构建 SQL 语句 
            string sql = string.Format("UPDATE Question SET Difficulty={0} WHERE QuestionId={1}", 
                difficulty, questionId); 
 
            try 
            { 
                // 创建 Command 对象 
                SqlCommand command = new SqlCommand(sql, DBHelper.connection); 
                // 打开数据库连接 
                DBHelper.connection.Open(); 
 
                // 执行命令 
                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(); // 关闭数据库连接 
            } 
        } 
    } 
}