抱歉地说这是错误的方法。
数据库具有内置系统,可防止数据重复。这是通过主键或唯一键约束实现的。对于您的情况,您已经创建了一个主键。因此,绝对不需要您执行该
SELECTCOUNT(*)查询。
而是直接将其插入表中,并在ppre已经存在时捕获完整性错误。
Try cmd = New MySqlCommand("Insert into personnel values('" & ppre.Text & "','" & lname.Text & "','" & fname.Text & "','" & office.Text & "','" & designation.Text & "')") i = cmd.ExecuteNonQuery If ppre.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 IfCatch ex As MySqlException MsgBox("Personnel ID Already Exist!", MessageBoxIcon.Error, "Error!")End Try另请参见MySQL手册页PRIMARY
KEY和UNIQUE索引约束



