我想使用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>
有人可以检查为什么它不能读取插页,
我的数据库表值:
pcode =主键
lname = longtext
fname = longtext
office = longtext
指定= longtext
任何帮助将不胜感激,谢谢,
答案 0 :(得分:6)
很抱歉说这是错误的方法。
数据库具有内置系统以防止数据被复制。这是通过主键或唯一键约束。在您的情况下,您已经创建了一个主键。因此,您无需执行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
答案 1 :(得分:1)
应该有你的方式:
1)在插入前写入一个触发器,并检查是否存在任何类似的行 2)在列上放置唯一索引
答案 2 :(得分:0)
找到了答案,就像@ 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