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

VB.NET SQL语句执行时出现数据不匹配错误,附相关代码

排查并解决VB.NET SQL数据不匹配错误

Hey there, let's break down the data mismatch error you're facing when running that DELETE statement in VB.NET. Here are the key issues and fixes:

1. 核心问题:数据类型不匹配

The most likely culprit is that your user_id (or post_id) column in the LIKES table is a numeric type (like INT), but you're wrapping the Session("user_id") value in single quotes ('), treating it as a string. When the database tries to compare a string to a numeric value, it throws a data mismatch error.

2. 严重隐患:SQL注入风险

Directly concatenating user input (or session values) into your SQL string is a huge security risk—it leaves your database open to SQL injection attacks. Plus, it's a common source of type-related bugs like the one you're seeing.

修复后的代码(参数化查询)

Here's how to rewrite your code correctly using parameterized queries, which fixes both the type mismatch and security issues:

Protected Sub unlikebtn_Click(sender As Object, e As EventArgs) Handles unlikebtn.Click
    ' First, validate the session value exists to avoid null issues
    If Session("user_id") Is Nothing Then
        ' Handle missing user ID (e.g., redirect to login)
        Response.Redirect("login.aspx")
        Return
    End If

    ' Use using statements to auto-dispose connections/commands (best practice)
    Using MemberDataConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("database/Database.mdb"))
        Dim strSQL As String = "DELETE FROM LIKES WHERE user_id = ? AND post_id = ?"
        Using MemberCommand As New OleDbCommand(strSQL, MemberDataConn)
            ' Add parameters with matching data types
            ' Replace OleDbType.Integer with the actual type of your user_id/post_id columns
            MemberCommand.Parameters.Add("@user_id", OleDbType.Integer).Value = Convert.ToInt32(Session("user_id"))
            ' Make sure to add the post_id value here (you had a truncated 'post_i...' in your code)
            MemberCommand.Parameters.Add("@post_id", OleDbType.Integer).Value = ' Your post ID value (e.g., from a control or session)

            MemberDataConn.Open()
            Dim rowsAffected As Integer = MemberCommand.ExecuteNonQuery()
            ' Optional: Check if any rows were deleted to confirm success
            If rowsAffected > 0 Then
                ' Handle successful delete (e.g., update UI)
            Else
                ' No matching record found
            End If
        End Using
    End Using
End Sub

额外检查点

  • Verify column data types: Double-check that the user_id and post_id columns in your LIKES table match the data type you're passing via parameters (e.g., if they're TEXT columns, use OleDbType.VarChar instead of Integer).
  • Validate session values: Ensure Session("user_id") contains a valid value of the correct type (e.g., a number, not a string like "abc"). You can add a check with Integer.TryParse to avoid conversion errors.
  • Complete your SQL statement: Your original code had post_i...—make sure you finish that part to include the post_id condition (it's likely required to target the correct like record).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:23:40