c#从100到题中随机抽出20到题并进行判断对错连接mysql数据库

工具:vs2012
数据库mysql
c#连接mysql需要下载mysql.data.dll文件
在这里插入图片描述

private void Form1_Load(object sender, EventArgs e)
        {
 MySqlConnection myconn = null;
            MySqlCommand mycom = null;
            myconn = new MySqlConnection("Host =localhost;Database=test;Username=root;Password=root");
            myconn.Open();
            mycom = myconn.CreateCommand();
            mycom.CommandText = "SELECT *FROM test ORDER BY  RAND() LIMIT 20";
            MySqlDataAdapter adap = new MySqlDataAdapter(mycom);
            DataSet ds = new DataSet();
            adap.Fill(ds,"test");
            dataGridView1.DataSource = ds.Tables[0].DefaultView;
            textBox2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["id"].Value.ToString();
            textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["title"].Value.ToString();
            }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["id"].Value.ToString();
            textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["title"].Value.ToString();
        }
private void button1_Click(object sender, EventArgs e)
        {
            string str = "T";
            string str1 = "F";
            if (textBox2.Text.Equals(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["id"].Value.ToString()) && textBox1.Text.Equals( dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["title"].Value.ToString())){
                if (radioButton1.Checked == true && str.Equals(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["boolean"].Value.ToString()))
                {
                    label2.ForeColor = Color.Red;
                    label2.Text = "恭喜你,回答正确";
                }
                else if (radioButton2.Checked == true && str1.Equals(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["boolean"].Value.ToString()))
                {
                    label2.ForeColor = Color.Red;
                    label2.Text = "恭喜你,回答正确";
                }
                else
                {
                    label2.Text = "抱歉,回答错误";
                }

            }
        }

猜你喜欢

转载自blog.csdn.net/wth_97/article/details/84570439