如果找不到数据库中的实体,如何防止错误
How to prevent an error if an entity in a database is not found
我制作了一个抽认卡应用程序,用户可以在其中编辑每张抽认卡的难度。
Private Sub btnHard_Click(sender As Object, e As EventArgs) Handles btnHard.Click
Dim sqlstring As String = "select * from flashcards where Difficulty = 3" 'Select from flashcard table where difficulty = 3
dataadapter = New OleDb.OleDbDataAdapter(sqlstring, connection)
dt.Clear() 'Clears datatable
dataadapter.Fill(dt) 'Fills datatable
Dim index = rand.Next(dt.Rows.Count) ' generates index in the range 0 .. Count - 1
If txtBack.Visible = True Then
txtFront.Text = dt.Rows(index)(2).ToString()
txtBack.Visible = False
txtBack.Text = dt.Rows(index)(3).ToString()
Else
MsgBox("Please first reveal the back of the flashcard")
End If
End Sub
此按钮选择所有难度等于 3 的抽认卡,但如果没有记录,系统会产生错误。那么,如果没有具有该难度的记录,我将如何获得它以便系统生成一条消息?
Private Sub btnHard_Click(sender As Object, e As EventArgs) Handles btnHard.Click
Dim sqlstring As String = "select * from flashcards where Difficulty = 3" 'Select from flashcard table where difficulty = 3
dataadapter = New OleDb.OleDbDataAdapter(sqlstring, connection)
dt.Clear() 'Clears datatable
dataadapter.Fill(dt) 'Fills datatable
If dt.Rows.Count = 0 Then 'If record is not found in the database
MsgBox("There are no more flashcards inside this deck")
Exit Sub 'Code continus running if record is found
End If
Dim index = rand.Next(dt.Rows.Count) ' generates index in the range 0 .. Count - 1
If txtBack.Visible = True Then 'If the back of the flashcard is shown
txtFront.Text = dt.Rows(index)(2).ToString() 'Displays a random record in the third column (front of flashcard)
txtBack.Visible = False 'Does not display the back of the flashcard
txtBack.Text = dt.Rows(index)(3).ToString() 'Displays a random record in the fourth column ()
Else 'If the user has not pressed the reveal button before
MsgBox("Please first reveal the back of the flashcard")
End If
End Sub
我制作了一个抽认卡应用程序,用户可以在其中编辑每张抽认卡的难度。
Private Sub btnHard_Click(sender As Object, e As EventArgs) Handles btnHard.Click
Dim sqlstring As String = "select * from flashcards where Difficulty = 3" 'Select from flashcard table where difficulty = 3
dataadapter = New OleDb.OleDbDataAdapter(sqlstring, connection)
dt.Clear() 'Clears datatable
dataadapter.Fill(dt) 'Fills datatable
Dim index = rand.Next(dt.Rows.Count) ' generates index in the range 0 .. Count - 1
If txtBack.Visible = True Then
txtFront.Text = dt.Rows(index)(2).ToString()
txtBack.Visible = False
txtBack.Text = dt.Rows(index)(3).ToString()
Else
MsgBox("Please first reveal the back of the flashcard")
End If
End Sub
此按钮选择所有难度等于 3 的抽认卡,但如果没有记录,系统会产生错误。那么,如果没有具有该难度的记录,我将如何获得它以便系统生成一条消息?
Private Sub btnHard_Click(sender As Object, e As EventArgs) Handles btnHard.Click
Dim sqlstring As String = "select * from flashcards where Difficulty = 3" 'Select from flashcard table where difficulty = 3
dataadapter = New OleDb.OleDbDataAdapter(sqlstring, connection)
dt.Clear() 'Clears datatable
dataadapter.Fill(dt) 'Fills datatable
If dt.Rows.Count = 0 Then 'If record is not found in the database
MsgBox("There are no more flashcards inside this deck")
Exit Sub 'Code continus running if record is found
End If
Dim index = rand.Next(dt.Rows.Count) ' generates index in the range 0 .. Count - 1
If txtBack.Visible = True Then 'If the back of the flashcard is shown
txtFront.Text = dt.Rows(index)(2).ToString() 'Displays a random record in the third column (front of flashcard)
txtBack.Visible = False 'Does not display the back of the flashcard
txtBack.Text = dt.Rows(index)(3).ToString() 'Displays a random record in the fourth column ()
Else 'If the user has not pressed the reveal button before
MsgBox("Please first reveal the back of the flashcard")
End If
End Sub