You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编辑Access数据库修改闪卡难度并按难度筛选抽取闪卡

解决方案

实现思路

首先需要解决两个核心问题:

  • 当前没有存储正在展示的闪卡的主键ID,无法定位要更新的数据库记录
  • 原代码是从全量闪卡数据中随机抽取,需要增加按难度字段筛选的逻辑
    同时可以把重复的抽卡、更新逻辑封装成通用方法,减少冗余代码。

步骤实现

1. 新增窗体全局变量

在窗体类的顶部添加两个私有变量,用于存储当前闪卡ID和复用随机数实例:

' 存储当前正在展示的闪卡主键ID
Private currentFlashcardId As Integer
' 随机数实例全局复用,避免重复初始化导致随机规律重复
Private ReadOnly rand As New Random()

2. 封装闪卡难度更新方法

新增通用方法实现Access数据库的难度更新,使用参数化查询避免SQL注入风险:

''' <summary>
''' 更新指定闪卡的难度值
''' </summary>
''' <param name="cardId">闪卡主键ID</param>
''' <param name="newDifficulty">新难度值:1=简单,2=中等,3=困难</param>
Private Sub UpdateCardDifficulty(cardId As Integer, newDifficulty As Integer)
    ' 替换为你自己的Access数据库连接字符串
    Dim connStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=你的数据库路径.accdb;"
    Using conn As New OleDbConnection(connStr)
        conn.Open()
        Dim updateCmd As New OleDbCommand("UPDATE Flashcards SET Difficulty = @diff WHERE ID = @id", conn)
        updateCmd.Parameters.AddWithValue("@diff", newDifficulty)
        updateCmd.Parameters.AddWithValue("@id", cardId)
        updateCmd.ExecuteNonQuery()
    End Using
End Sub

3. 封装按难度抽卡的通用方法

把原来三个按钮重复的抽卡逻辑抽成通用方法,支持按指定难度筛选:

''' <summary>
''' 随机抽取闪卡并展示,可指定难度筛选
''' </summary>
''' <param name="targetDifficulty">要抽取的闪卡难度,不传则抽取全部闪卡</param>
Private Sub LoadRandomCard(Optional targetDifficulty As Integer = Nothing)
    Dim filteredRows As DataRow()
    ' 按难度过滤闪卡
    If targetDifficulty > 0 Then
        filteredRows = dt.Select($"Difficulty = {targetDifficulty}")
    Else
        filteredRows = dt.Select()
    End If
    
    ' 处理无对应难度闪卡的边界情况
    If filteredRows.Length = 0 Then
        MsgBox("当前难度下暂无可用闪卡")
        Return
    End If
    
    ' 随机抽卡并展示
    Dim index = rand.Next(filteredRows.Length)
    Dim selectedRow = filteredRows(index)
    ' 保存当前闪卡ID,后续更新难度用
    currentFlashcardId = CInt(selectedRow(0))
    txtFront.Text = selectedRow(2).ToString()
    txtBack.Text = selectedRow(3).ToString()
    txtBack.Visible = False
End Sub

4. 修改三个按钮的点击事件

现在三个按钮只需要调用封装好的方法即可,逻辑清晰无冗余:

Private Sub btnEasy_Click(sender As Object, e As EventArgs) Handles btnEasy.Click
    If txtBack.Visible = True Then
        ' 更新当前闪卡难度为1
        UpdateCardDifficulty(currentFlashcardId, 1)
        ' 仅从难度为1的闪卡中抽下一张
        LoadRandomCard(1)
    Else
        MsgBox("请先翻开闪卡背面")
    End If
End Sub

Private Sub btnGood_Click(sender As Object, e As EventArgs) Handles btnGood.Click
    If txtBack.Visible = True Then
        UpdateCardDifficulty(currentFlashcardId, 2)
        LoadRandomCard(2)
    Else
        MsgBox("请先翻开闪卡背面")
    End If
End Sub

Private Sub btnHard_Click(sender As Object, e As EventArgs) Handles btnHard.Click
    If txtBack.Visible = True Then
        UpdateCardDifficulty(currentFlashcardId, 3)
        LoadRandomCard(3)
    Else
        MsgBox("请先翻开闪卡背面")
    End If
End Sub

注意事项

  • 你原来的闪卡插入逻辑用了字符串拼接,存在SQL注入风险,建议也改成参数化查询的写法,和上面更新逻辑的写法一致即可。
  • 每次更新数据库难度后,建议同步更新本地dt里对应行的Difficulty字段值,或者重新加载一次dt的全量数据,避免本地缓存和数据库数据不一致导致筛选错误。

内容的提问来源于stack exchange,提问作者Idk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 00:45:04