在选中的单选按钮上匹配sqlserver数据库中的两列。 [英] Match two columns in a sqlserver database on a radio button checked.

查看:107
本文介绍了在选中的单选按钮上匹配sqlserver数据库中的两列。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在开发一个从数据库中提取问题的考试软件,在数据库中我有这些列Exam_ID,Question,Option1,Option2,Option3,Option4,Answer,问题显示在WinForm的文本框中,而从选项1到选项4显示在单选按钮上,完成了下一个和上一个按钮,但我现在需要的是当用户点击下一个时,它会将点击的选项与数据库中的正确答案相匹配。





Am developing an exam software which fetch out the questions from a database, In the database i have these columns Exam_ID, Question, Option1, Option2, Option3, Option4, Answer, the question is displayed in a textbox on the WinForm, while from option1 to Option4 are displayed on a radio button, am done with the next and previous buttons, but what i need now is when the user clicks next, it would match clicked Option with the right answer in the db.


namespace WpfAppEEXam
{
    /// <summary>
    /// Interaction logic for Questions.xaml
    /// </summary>
    public partial class Questions : Window
    {
        int rowIndex = 0;
        SqlDataReader rdr = null;
        DataTable dtable = new DataTable();
        SqlConnection con = null;
        SqlDataAdapter adp;
        DataSet ds = new DataSet();
        SqlCommand cmd = null;
        DataTable dt = new DataTable();
        //string cs = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\MrKuzz\Desktop\My Work\AdminPanel\AdminPanel\EExamDB.mdf;Integrated Security=True;User Instance=True";
        /// <summary>
        /// 
        /// </summary>
        private int time = 30;
        private DispatcherTimer Timer;

        public Questions()
        {
            InitializeComponent();
            Timer = new DispatcherTimer();
            Timer.Interval = new TimeSpan(0, 0, 1);
            Timer.Tick += new EventHandler(Timer_Tick);
            Timer.Start();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dt = GetData();
                if(dt.Rows.Count > 0)
                {
                    textBox1.Text = dt.Rows[0]["question"].ToString();

                    radioButton1.Content = dt.Rows[0]["option1"];
                    radioButton2.Content = dt.Rows[0]["option2"];
                    radioButton3.Content = dt.Rows[0]["option3"];
                    radioButton4.Content = dt.Rows[0]["option4"];
                    radioButton5.Content = dt.Rows[0]["option5"];
                }

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }


        void Timer_Tick(object sender, EventArgs e)
        {
            if (time > 0)
            {
                if(time <=600)
                {
                    
                    if (time % 2 == 0)
                    {
                        textBlockTime.Foreground = Brushes.Red;
                        textBlockwarning.Text = "Running out of Time";
                        textBlockwarning.Foreground = Brushes.OrangeRed;
                    }
                    else
                    {
                        textBlockTime.Foreground = Brushes.Black;
                    }
                    time--;
                    textBlockTime.Text = string.Format("{0:00h} " + ":" + " {1:00m} " + ":" + " {2:00s}", time / 3600, (time % 3600) / 60, time % 60);
                }
                else
                {
                    time--;
                    textBlockTime.Text = string.Format("{0:00h} " + ":" + " {1:00m} " + ":" + " {2:00s}", time / 3600, (time % 3600) / 60, time % 60);

                }
            }
            else
            {
                Timer.Stop();
                MainWindow mw = new MainWindow();
                mw.Show();
                this.Hide();

            }
        }

        
        
        //Buton that fetch next record
        private void button3_Click(object sender, RoutedEventArgs e)
        {
            if (rowIndex < dt.Rows.Count - 1)
            {
                rowIndex++;
                textBox1.Text = dt.Rows[rowIndex]["question"].ToString();

                radioButton1.Content = dt.Rows[rowIndex]["option1"];
                radioButton2.Content = dt.Rows[rowIndex]["option2"];
                radioButton3.Content = dt.Rows[rowIndex]["option3"];
                radioButton4.Content = dt.Rows[rowIndex]["option4"];
                radioButton5.Content = dt.Rows[rowIndex]["option5"];
            }
            else
            {
                
            }
        }
        //Buton that fetch previous record
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (rowIndex == dt.Rows.Count - 1 || rowIndex != 0)
            {
                rowIndex--;
                textBox1.Text = dt.Rows[rowIndex]["question"].ToString();

                radioButton1.Content = dt.Rows[rowIndex]["option1"];
                radioButton2.Content = dt.Rows[rowIndex]["option2"];
                radioButton3.Content = dt.Rows[rowIndex]["option3"];
                radioButton4.Content = dt.Rows[rowIndex]["option4"];
                radioButton5.Content = dt.Rows[rowIndex]["option5"];
            }
            else
            {
                
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                e.Cancel = true;
                //base.OnClosing(e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private DataTable GetData()
        {
            DataTable dt = new DataTable();
            SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\MrKuzz\Desktop\My Work\ExamProject\Project DataBase File For Exam\EExamDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            try
            {
                connection.Open();
                SqlCommand sqlCmd = new SqlCommand("Select * From Questions WHERE classid='"+UserClass.CurrentClass+"' ", connection);
                SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

                sqlDa.Fill(dt);
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Fetch Error:";
                msg += ex.Message;
                throw new Exception(msg);

            }
            finally
            {
                connection.Close();
            }
            return dt;
        }
         
    }
}

推荐答案

DamithSL at 19 mins ago
Ok now you want us to write that part of your application?




Member 11197984 at 2 mins ago
if possible





我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!


这篇关于在选中的单选按钮上匹配sqlserver数据库中的两列。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆