Mysql 防止数据库中出现重复条目

Mysql 防止数据库中出现重复条目,mysql,vb.net,validation,Mysql,Vb.net,Validation,我想使用vb.net和MySQL作为数据库,防止重复输入我的库存表单,以下是我的代码: Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim myCommand As New MySqlCommand Dim conn As MySqlConnection Dim i As String con

我想使用vb.net和MySQL作为数据库,防止重复输入我的库存表单,以下是我的代码:

 Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Dim myCommand As New MySqlCommand
    Dim conn As MySqlConnection
    Dim i As String
    conn = New MySqlConnection
    conn.ConnectionString = "server = localhost;username= root;password= a;database= secret"
    Try
        conn.Open()
    Catch mali As MySqlException
        MsgBox("connot establish connection")
    End Try

    Dim intReturn As Integer
    Dim strSql As String = " select * from personnel where pcode = @pcode"

    Dim sqlcmd As New MySqlCommand(strSql, conn)
    With sqlcmd.Parameters
        .AddWithValue("@pcode", CType(pcode.Text, String))
    End With

    intReturn = sqlcmd.ExecuteScalar

    If (intReturn > 0) Then
        cmd = New MySqlCommand("Insert into personnel values('" & pcode.Text & "','" & lname.Text & "','" & fname.Text & "','" & office.Text & "','" & designation.Text & "')")
        i = cmd.ExecuteNonQuery


        If pcode.Text <> "" Then
        ElseIf i > 0 Then
            MsgBox("Save Successfully!", MessageBoxIcon.Information, "Success")
            mrClean()
            ListView1.Tag = ""
            Call objLocker(False)
            Call LVWloader()
            Call calldaw()
        Else
            MsgBox("Save Failed!", MessageBoxIcon.Error, "Error!")
        End If
    Else
        MsgBox("Personnel ID Already Exist!", MessageBoxIcon.Error, "Error!")

    End If
Private子按钮4\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮4。单击
将myCommand设置为新的MySqlCommand
Dim conn作为MySqlConnection
我像绳子一样暗
conn=新的MySQL连接
conn.ConnectionString=“server=localhost;username=root;password=a;database=secret”
尝试
康涅狄格州公开赛
将mali捕获为MySqlException
MsgBox(“无法建立连接”)
结束尝试
以整数形式返回
Dim strSql As String=“从人员中选择*,其中pcode=@pcode”
Dim sqlcmd作为新的MySqlCommand(strSql,conn)
使用sqlcmd.Parameters
.AddWithValue(“@pcode”,CType(pcode.Text,String))
以
intReturn=sqlcmd.ExecuteScalar
如果(intReturn>0),则
cmd=New MySqlCommand(“插入到人员值中(“&pcode.Text&“,”&lname.Text&“,”&fname.Text&“,”,“&fname.Text&“,”,“&office.Text&“,”,“&designation.Text&“))
i=cmd.ExecuteNonQuery
如果是pcode.Text“”,则
如果i>0,那么
MsgBox(“保存成功!”,MessageBoxIcon.Information,“成功”)
mrClean()
ListView1.Tag=“”
呼叫objLocker(错误)
调用LVWloader()
调用calldaw()
其他的
MsgBox(“保存失败!”,MessageBoxIcon.Error,“Error!”)
如果结束
其他的
MsgBox(“人员ID已存在!”,MessageBoxIcon.Error,“Error!”)
如果结束
端接头

我在搜索答案时发现了这个问题,但当我尝试运行它时,它不会读取insert命令,而是直接转到msbox“Personal ID已存在”,即使没有相同的人员ID

有人能检查一下为什么它没有读到插页吗

我的数据库表值:

pcode=主键

lname=长文本

fname=长文本

办公室=长文本

名称=长文本


任何帮助都将不胜感激,谢谢,

很抱歉,这是错误的方法

数据库有一个内置的系统来防止数据被复制。这是通过主键或唯一键约束实现的。在您的情况下,您已经创建了一个主键。因此,您完全不需要这样做
selectcount(*)
query

相反,当pcode已经存在时,直接插入到表中并捕获完整性错误

Try
    cmd = New MySqlCommand("Insert into personnel values('" & pcode.Text & "','" & lname.Text & "','" & fname.Text & "','" & office.Text & "','" & designation.Text & "')")

    i = cmd.ExecuteNonQuery


    If pcode.Text <> "" Then
    ElseIf i > 0 Then
        MsgBox("Save Successfully!", MessageBoxIcon.Information, "Success")
        mrClean()
        ListView1.Tag = ""
        Call objLocker(False)
        Call LVWloader()
        Call calldaw()
    Else
        MsgBox("Save Failed!", MessageBoxIcon.Error, "Error!")
    End If
Catch ex As MySqlException
    MsgBox("Personnel ID Already Exist!", MessageBoxIcon.Error, "Error!")
End Try
试试看
cmd=New MySqlCommand(“插入到人员值中(“&pcode.Text&“,”&lname.Text&“,”&fname.Text&“,”,“&fname.Text&“,”,“&office.Text&“,”,“&designation.Text&“))
i=cmd.ExecuteNonQuery
如果是pcode.Text“”,则
如果i>0,那么
MsgBox(“保存成功!”,MessageBoxIcon.Information,“成功”)
mrClean()
ListView1.Tag=“”
呼叫objLocker(错误)
调用LVWloader()
调用calldaw()
其他的
MsgBox(“保存失败!”,MessageBoxIcon.Error,“Error!”)
如果结束
Catch ex作为MySqlException
MsgBox(“人员ID已存在!”,MessageBoxIcon.Error,“Error!”)
结束尝试

另请参阅MySQL手册页面

很抱歉,这是错误的方法

数据库有一个内置的系统来防止数据被复制。这是通过主键或唯一键约束实现的。在您的情况下,您已经创建了一个主键。因此,您完全不需要这样做
selectcount(*)
query

相反,当pcode已经存在时,直接插入到表中并捕获完整性错误

Try
    cmd = New MySqlCommand("Insert into personnel values('" & pcode.Text & "','" & lname.Text & "','" & fname.Text & "','" & office.Text & "','" & designation.Text & "')")

    i = cmd.ExecuteNonQuery


    If pcode.Text <> "" Then
    ElseIf i > 0 Then
        MsgBox("Save Successfully!", MessageBoxIcon.Information, "Success")
        mrClean()
        ListView1.Tag = ""
        Call objLocker(False)
        Call LVWloader()
        Call calldaw()
    Else
        MsgBox("Save Failed!", MessageBoxIcon.Error, "Error!")
    End If
Catch ex As MySqlException
    MsgBox("Personnel ID Already Exist!", MessageBoxIcon.Error, "Error!")
End Try
试试看
cmd=New MySqlCommand(“插入到人员值中(“&pcode.Text&“,”&lname.Text&“,”&fname.Text&“,”,“&fname.Text&“,”,“&office.Text&“,”,“&designation.Text&“))
i=cmd.ExecuteNonQuery
如果是pcode.Text“”,则
如果i>0,那么
MsgBox(“保存成功!”,MessageBoxIcon.Information,“成功”)
mrClean()
ListView1.Tag=“”
呼叫objLocker(错误)
调用LVWloader()
调用calldaw()
其他的
MsgBox(“保存失败!”,MessageBoxIcon.Error,“Error!”)
如果结束
Catch ex作为MySqlException
MsgBox(“人员ID已存在!”,MessageBoxIcon.Error,“Error!”)
结束尝试

另请参阅MySQL手册页面

应该有以下方法:

1) 在插入之前写入触发器,并检查是否存在任何类似的行。

2) 将唯一索引放在列上

应该有以下方法:

1) 在插入之前写入触发器,并检查是否存在任何类似的行。

2) 将唯一索引放在列上

找到了答案,正如@e4c5所说的,这是一种错误的方法,因此我重新构建了代码,并最终使其正常工作,只是想分享答案,也许它会帮助其他人

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim myCommand As New MySqlCommand
Dim conn As MySqlConnection
Dim i As String
conn = New MySqlConnection
conn.ConnectionString = "server = localhost;username= root;password= a;database= secret"
Try
    conn.Open()
Catch mali As MySqlException
    MsgBox("connot establish connection")
End Try
    Dim retval As String
    Select Button4.Tag
            Case "ADD"
    with myCommand
       .Connection = conn
       .CommandText = "Select pcode from personnel where pcode = '" & pcode.Text & "'"
        retval = .ExecuteScalar
    If retval Is Nothing Then
      .CommandText = "Insert into personnel values ('" & pcode.Text & "','" & lname.Text & "','" & fname.Text & "','" & office.text & "','" & designation.Text & "')"
      .ExecuteNonQuery()
  Else
  MsgBox("Personnel ID Already Exist!", MessageBoxIcon.Error, "Error")
 End If
End With
End Sub

找到了答案,正如@e4c5所说的,这是一个错误的方法,所以我重新构造了我的代码,并最终使其工作,只是想分享答案,也许它会帮助其他人

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim myCommand As New MySqlCommand
Dim conn As MySqlConnection
Dim i As String
conn = New MySqlConnection
conn.ConnectionString = "server = localhost;username= root;password= a;database= secret"
Try
    conn.Open()
Catch mali As MySqlException
    MsgBox("connot establish connection")
End Try
    Dim retval As String
    Select Button4.Tag
            Case "ADD"
    with myCommand
       .Connection = conn
       .CommandText = "Select pcode from personnel where pcode = '" & pcode.Text & "'"
        retval = .ExecuteScalar
    If retval Is Nothing Then
      .CommandText = "Insert into personnel values ('" & pcode.Text & "','" & lname.Text & "','" & fname.Text & "','" & office.text & "','" & designation.Text & "')"
      .ExecuteNonQuery()
  Else
  MsgBox("Personnel ID Already Exist!", MessageBoxIcon.Error, "Error")
 End If
End With
End Sub

我认为您的查询应该是来自人员的
select count(*),其中pcode=@pcode
,因为您似乎想检查是否有命令返回的行试图使用count,但我在intReturn=sqlcmd.ExecuteScalar声明中遇到了一个新错误,我认为您的查询应该是
select count(*)从人员那里,pcode=@pcode
因为看起来您想检查您的命令是否返回了行,所以尝试使用count,但我在intReturn=sqlcmd.ExecuteScalar声明中发现了一个新错误,绝对不需要此触发器。绝对不需要此触发器。谢谢先生,事实上我已经做了一个代码直接插入到表中,但我不知道如何捕捉错误,它总是出现在代码上我只希望它出现在messagebox上,谢谢,谢谢先生,事实上我已经做了一个代码直接插入到表中,但我只是不知道如何捕捉t