如何编辑访问数据库?

How to edit an access database?

我制作了一个抽认卡应用程序,将用户的抽认卡存储到访问数据库中,他们可以从中进行修改。

这是创建抽认卡的部分代码

 command = " insert into Flashcards ([Front],[Back],[Difficulty]) values ('" & txtFront.Text & "','" & txtBack.Text & "' ,'" & 3 & "')"

这将抽认卡存储在以下数据库中 Access database

这是修改抽认卡的部分代码

Private Sub btnEasy_Click(sender As Object, e As EventArgs) Handles btnEasy.Click
        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 btnGood_Click(sender As Object, e As EventArgs) Handles btnGood.Click
        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

    Private Sub btnHard_Click(sender As Object, e As EventArgs) Handles btnHard.Click
        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

现在 btnEasy 、 btngood 和 btnhard 都做同样的事情,但我想知道一种方法,例如按 btnEasy 会将数据库中的难度从 3 更改为 1。

另外,我怎么才能得到它,所以按 btneasy 只会随机 select 难度为 1 的抽认卡,而不仅仅是随机 select 来自整个数据库的抽认卡。

不好意思,问题有点长,纠结了好久

您需要将数据表(代码中的 dt)过滤为 select 仅与给定按钮的“难度”相关的记录。从您的图片看来,Easy = 1,Good = 2,Hard = 3?如果您不熟悉 select 从 DataTable 中获取数据子集,它在您的代码中应该看起来像这样:

    ' Use the Select method to find all rows matching the filter.
    hardRows = dt.Select("Difficulty = 3")

然后您可以从该子集或行中随机 select(而不是从 dt.Rows.Count)。

此外,有关更多信息,请查看 Microsoft 的文档:https://docs.microsoft.com/en-us/dotnet/api/system.data.datatable.select