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