我要防止重複條目用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
任何幫助將非常感激,感謝,
我覺得您的查詢應該是'選擇其中的人員P碼= @ pcode'因爲它看起來像要檢查是否存在由你的命令返回的行 – jelliaes
試圖用計數COUNT(*),但我在intReturn = sqlcmd.ExecuteScalar聲明中得到一個新錯誤, – User2341