2016-11-08 137 views
1

我要防止重复条目用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 

末次

我发现这一点的同时我搜索答案,但是当我试图运行它时,它不读取插入命令,而是直接进入msbox“人员ID已存在”,即使这些人没有相同的人员ID。

有人可以检查它为什么不读插入请

我的数据库表中的值:

P码=主键

L-NAME = LONGTEXT

FNAME = LONGTEXT

office = longtext

指定= LONGTEXT

任何帮助将非常感激,感谢,

+0

我觉得您的查询应该是'选择其中的人员P码= @ pcode'因为它看起来像要检查是否存在由你的命令返回的行 – jelliaes

+0

试图用计数COUNT(*),但我在intReturn = sqlcmd.ExecuteScalar声明中得到一个新错误, – User2341

回答

5

遗憾地说,这是错误的做法。

数据库有一个内置的系统来防止数据被复制。这是通过主键或唯一的关键限制。就你而言,你已经创建了一个主键。所以你绝对没有必要去做那个SELECT COUNT(*)查询。

相反,只要直接插入到表中并在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 

也请参考MySQL手册PRIMARY KEY and UNIQUE Index Constraints

+0

谢谢先生,实际上我已经做了一个代码直接插入表中,但我不知道如何捕获错误,它总是出现在我只想要的代码上它是在消息框上,谢谢, – User2341

+0

https://msdn.microsoft.com/en-us/library/ys1b32h3(v=vs.100).aspx – e4c5

1

应该有你的方式:

1)写插入之前的触发器,并检查是否有任何类似的行存在。
2)将唯一索引列

+3

绝对不需要为此触发。 – e4c5

0

找到了答案,因为什么@ e4c5说,它是一种错误的做法,所以我restructed我的代码,并终于成功了工作,只是想分享的答案也许这将帮助别人。

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 
+0

对不起,说仍然错,因为你仍然在做一个选择,然后插入。请仔细阅读vb.net中的异常处理以及数据库竞争条件 – e4c5

+0

另请参阅我的更新答案。 – e4c5

+0

我想知道为什么它在我的工作,它检测到pcode重复,并保存数据,如果pcode是唯一的, – User2341