错误显示 MySQL 中使用 vb.net 的记录是否重复
Error show if there is a duplication of record in MySQL using vb.net
美好的一天!有人可以帮我修复我的代码吗?如果记录重复,我想显示一条错误消息。例如,我输入了一个用户名“admin”,但它已经在我的数据库中,所以它应该显示一条消息说“用户名已经存在!”。否则,如果用户名尚未使用,那么它将被添加到我的数据库中。我正在使用 Visual Studio 2005 和 Navicat for MySQL
这是我的代码:
conn.Open()
Dim qadd As String = "SELECT * FROM tbl_user WHERE uname='" & txt_uname.Text & "'"
Dim cmd As New MySqlCommand(qadd, conn)
Dim data As MySqlDataReader = cmd.ExecuteReader
If data.Read Then
If data(0) = txt_uname.Text Then
MsgBox("User " & data(0) & " already exists! ", MsgBoxStyle.Critical)
Else
Dim qstr As String = "INSERT INTO tbl_user (uname, pword, ulvl) VALUES ('" & txt_uname.Text & "' , '" & txt_pword1.Text & "' , '" & txt_pword2.Text & "') ON DUPLICATE KEY UPDATE uname = '" & txt_uname.Text & "'"
Dim cm As New MySqlCommand(qstr, conn)
Dim dat As MySqlDataReader = cm.ExecuteReader
MsgBox("User has been added!", MsgBoxStyle.Information)
txt_uname.Clear()
txt_pword1.Clear()
txt_pword2.Clear()
txt_uname.Focus()
End If
End If
conn.Close()
仍有很大的改进空间,我在 phone 上输入了这个,没有进行语法检查,但我认为它应该能让你朝着正确的方向前进。您需要阅读的内容是参数化您的 query/insert 语句和可以帮助管理数据库连接的 Using 关键字。
Dim qadd As String = "SELECT Count(uname) FROM tbl_user WHERE uname='" & txt_uname.Text & "'"
Dim cmd As New MySqlCommand(qadd, conn)
Dim userCounter as int = cmd.ExecuteScaler
if userCounter > 0 then
MsgBox("User " & data(0) & " already exists! ", MsgBoxStyle.Critical)
Else
Dim qstr As String = "INSERT INTO tbl_user (uname, pword, ulvl) VALUES ('" & txt_uname.Text & "' , '" & txt_pword1.Text & "' , '" & txt_pword2.Text & "') ON DUPLICATE KEY UPDATE uname = '" & txt_uname.Text & "'"
Dim cm As New MySqlCommand(qstr, conn)
Dim dat As MySqlDataReader = cm.ExecuteReader
MsgBox("User has been added!", MsgBoxStyle.Information)
txt_uname.Clear()
txt_pword1.Clear()
txt_pword2.Clear()
txt_uname.Focus()
End If
美好的一天!有人可以帮我修复我的代码吗?如果记录重复,我想显示一条错误消息。例如,我输入了一个用户名“admin”,但它已经在我的数据库中,所以它应该显示一条消息说“用户名已经存在!”。否则,如果用户名尚未使用,那么它将被添加到我的数据库中。我正在使用 Visual Studio 2005 和 Navicat for MySQL 这是我的代码:
conn.Open()
Dim qadd As String = "SELECT * FROM tbl_user WHERE uname='" & txt_uname.Text & "'"
Dim cmd As New MySqlCommand(qadd, conn)
Dim data As MySqlDataReader = cmd.ExecuteReader
If data.Read Then
If data(0) = txt_uname.Text Then
MsgBox("User " & data(0) & " already exists! ", MsgBoxStyle.Critical)
Else
Dim qstr As String = "INSERT INTO tbl_user (uname, pword, ulvl) VALUES ('" & txt_uname.Text & "' , '" & txt_pword1.Text & "' , '" & txt_pword2.Text & "') ON DUPLICATE KEY UPDATE uname = '" & txt_uname.Text & "'"
Dim cm As New MySqlCommand(qstr, conn)
Dim dat As MySqlDataReader = cm.ExecuteReader
MsgBox("User has been added!", MsgBoxStyle.Information)
txt_uname.Clear()
txt_pword1.Clear()
txt_pword2.Clear()
txt_uname.Focus()
End If
End If
conn.Close()
仍有很大的改进空间,我在 phone 上输入了这个,没有进行语法检查,但我认为它应该能让你朝着正确的方向前进。您需要阅读的内容是参数化您的 query/insert 语句和可以帮助管理数据库连接的 Using 关键字。
Dim qadd As String = "SELECT Count(uname) FROM tbl_user WHERE uname='" & txt_uname.Text & "'"
Dim cmd As New MySqlCommand(qadd, conn)
Dim userCounter as int = cmd.ExecuteScaler
if userCounter > 0 then
MsgBox("User " & data(0) & " already exists! ", MsgBoxStyle.Critical)
Else
Dim qstr As String = "INSERT INTO tbl_user (uname, pword, ulvl) VALUES ('" & txt_uname.Text & "' , '" & txt_pword1.Text & "' , '" & txt_pword2.Text & "') ON DUPLICATE KEY UPDATE uname = '" & txt_uname.Text & "'"
Dim cm As New MySqlCommand(qstr, conn)
Dim dat As MySqlDataReader = cm.ExecuteReader
MsgBox("User has been added!", MsgBoxStyle.Information)
txt_uname.Clear()
txt_pword1.Clear()
txt_pword2.Clear()
txt_uname.Focus()
End If