1. 程式人生 > >c#從100到題中隨機抽出20到題並進行判斷對錯連線mysql資料庫

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 = "抱歉,回答錯誤";
                }

            }
        }