Vbnet SQL中的重复条目

加入

我一直输入相同的内容username,但仍保存到DB中。我的代码有什么问题?

sql = "INSERT INTO testing_mysql_vb(id,user_name) VALUES(NULL,'" & TextBox1.Text & "')"

    Try
        dbcomm = New MySqlCommand(sql, dbconn)
        dbread = dbcomm.ExecuteReader()
        dbread.Close()

        If dbread.HasRows Then
            sql = "select from testing_mysql_vb where user_name = '" & TextBox1.Text & "'"
            MsgBox("Duplicate record!")

        End If

    Catch ex As Exception
        MsgBox("Error in saving to Database. Error is :" & ex.Message)
        dbread.Close()
        Exit Sub
    End Try
    MsgBox("The User Name was saved.")
    TextBox1.Text = ""
劳尔

我看到的可能是以下两个问题中的任何一个:

  1. 用户名不是主键或唯一约束,因此可以保存。或者
  2. 您首先要插入数据库,然后检查该用户名是否存在于数据库中。它应该是相反的方式。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章