2016-11-08 189 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