我正在嘗試制作一個多項選擇修訂程式,但是在用戶回答第一個生成的問題后,我在如何讓我的程式生成另一個問題(我已存盤在資料庫中)方面遇到了一些問題?有人可以向我展示代碼示例或類似內容嗎?
Imports System.Data.OleDb
Public Class QUI
'Define all the variables which are meant to be in use'
Dim ConnectString As String = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=projectdatabase.accdb")
Dim DataReader As OleDbDataReader
Dim cmd As New OleDbCommand
Dim connection As New OleDbConnection
Dim noofq As Integer
Dim q(noofq) As String
Dim questionnum As Integer
'Asks the user to input a number to d
Private Sub QUI_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Asks the user to input a number to decide how many questions they want to answer'
noofq = InputBox("Input the number of questions you want. You can choose from 1 to 10.")
'A counter starting from one to the number of questions the user has inputted'
For i = 1 To noofq
Next
'Open the connection to the database'
connection.ConnectionString = ConnectString
connection.Open()
'Sets the question number to one'
questionnum = 1
Label1.Text = "Question " & questionnum & " of " & noofq
'Retrieves the question and answers from the database'
cmd.CommandText = "SELECT question, Answer1, Answer2, Answer3 FROM 1Questions"
cmd.Connection = connection
DataReader = cmd.ExecuteReader
'Assigns the values from the database to each of the labels and buttons'
If DataReader.HasRows Then
DataReader.Read()
Lblquestion.Text = DataReader.Item("Question")
RadioButtonA1.Text = DataReader.Item("Answer1")
RadioButtonA2.Text = DataReader.Item("Answer2")
RadioButtonA3.Text = DataReader.Item("Answer3")
DataReader.Close()
End If
connection.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
If RadioButtonA1.Checked And questionnum = 1 Then
MsgBox("Correct Answer")
Else
MsgBox("Incorrect Answer")
End If
End Sub
End Class
uj5u.com熱心網友回復:
在問題表中,如果添加一列代表問題代碼,那么您可以生成亂數以從問題中進行選擇。還可以添加諸如不重復相同問題的功能。那就是如果我很好地理解了你的問題
uj5u.com熱心網友回復:
顯然您沒有接受我的建議并打開 Option Strict。請現在就這樣做。
這就是你的資料庫表應該是什么樣子。本QuestionID應該是一個自動數列與CorrectAnswer列應該包含的單選按鈕將包含正確答案的名稱。

您的主要問題是您已經從 1Questions 表中檢索了所有資料,但您只讀取了第一條記錄。您必須再次連接到資料庫才能檢索另一個問題。這是一個壞主意。
我們將檢索您需要的所有資料并將其放入DataTable類似于記憶體表的 a 中。啟動資料庫物件將在 Using 塊中宣告和處理。這是必要的,因此他們的Dispose方法將被呼叫。您不會在使用它們的方法之外宣告這些物件。您可以將連接字串直接傳遞給 的建構式,Connection并將CommandText和Connection直接傳遞給 的建構式Command。
我已更改 sql 字串以包含表的主鍵。我不得不猜測欄位名稱,因此將其更改為真實名稱。我還為正確答案添加了一個欄位。這可能需要添加到表中。sql 字串的最后一部分有一個Order By子句,每次運行時都會回傳隨機問題。
這里的重點是關閉并釋放資料庫連接。我們測驗所需的所有資料都在DataTable.
我使用了 aBindingSource以便我們可以顯示資料并輕松瀏覽問題。
Private ConStr As String = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=projectdatabase.accdb")
Private intNumOfQuestions As Integer
Private questionnum As Integer
Private BndScr As BindingSource
Private Sub QUI_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim strnoofq = InputBox("Input the number of questions you want. You can choose from 1 to 10.")
If Integer.TryParse(strnoofq, intNumOfQuestions) AndAlso intNumOfQuestions > 0 AndAlso intNumOfQuestions < 11 Then
Dim dt As New DataTable
Using cn As New OleDbConnection(ConStr),
cmd As New OleDbCommand($"SELECT Top {intNumOfQuestions} QuestionID, Question, Answer1, Answer2, Answer3, CorrectAnswer
FROM 1Questions
ORDER BY Rnd(-Timer()*[QuestionID])", cn)
cn.Open()
Using reader = cmd.ExecuteReader
dt.Load(reader)
End Using
End Using
questionnum = 1
Label1.Text = $"Question {questionnum} of {strnoofq}"
BndScr = New BindingSource()
BndScr.DataSource = dt
Lblquestion.DataBindings.Add("Text", BndScr, "Question")
Lblquestion.DataBindings.Add("Tag", BndScr, "CorrectAnswer")
RadioButtonA1.DataBindings.Add("Text", BndScr, "Answer1")
RadioButtonA2.DataBindings.Add("Text", BndScr, "Answer2")
RadioButtonA3.DataBindings.Add("Text", BndScr, "Answer3")
Else
MessageBox.Show("Please enter valid number Of questions.")
End If
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
If GetSelectedRadioButton(Me).Name = Lblquestion.Tag.ToString Then
'Correct Answer
Else
'Wrong Answer
End If
If intNumOfQuestions = questionnum Then
'End of quiz
Else
BndScr.MoveNext()
questionnum = 1
End If
End Sub
Public Function GetSelectedRadioButton(Container As Control) As RadioButton
Dim rb = Container.Controls.OfType(Of RadioButton)().FirstOrDefault(Function(r) r.Checked = True)
Return rb
End Function
如何保持分數取決于您。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/351522.html
標籤:网络
上一篇:使用Google登錄-我們如何在.net中驗證GoogleID令牌服務器端?缺少代碼示例,庫似乎已棄用
下一篇:如何讓輸入框只顯示一次
