我有代码,如果有重复,那么它会说“用户名已经存在”。但是,它继续记录我的数据库中的firstname
,lastname
和middlename
等其他数据(username
除外)。如果一个数据有重复,如何阻止它记录其他值?
Try
Const SQLStatement As String = "START TRANSACTION; INSERT INTO people(Firstname, Middlename, Lastname) VALUES ( @p0, @p1, @p2);
INSERT account(uname, pass, type) VALUES( @p3, @p4,@p5); COMMIT;"
Using connection As New MySqlConnection()
connection.ConnectionString = ServerString
Dim command As New MySqlCommand(SQLStatement, connection)
command.Parameters.AddWithValue("@p0", firstname.Value)
command.Parameters.AddWithValue("@p1", middlename.Value)
command.Parameters.AddWithValue("@p2", lastname.Value)
command.Parameters.AddWithValue("@p3", username.Value)
command.Parameters.AddWithValue("@p4", password.Value)
command.Parameters.AddWithValue("@p5", type.Value)
connection.Open()
command.ExecuteNonQuery()
End Using
lblALabel.Text = "Successfully Registered!"
firstname.Value = ""
middlename.Value = ""
lastname.Value = ""
username.Value = ""
password.Value = ""
Catch ex As Exception
lblALabel.Text = "Username is taken"
End Try
答案 0 :(得分:0)
试试这个......
它将首先检查“UserName
”,如果不存在,则会插入您的数据。
Try
Const SQLStatement As String = "IF NOT EXISTS(SELECT * FROM ACCOUNT WHERE UNAME=@P3)
BEGIN
START TRANSACTION;
INSERT INTO people(Firstname, Middlename, Lastname) VALUES ( @p0, @p1, @p2);
INSERT account(uname, pass, type) VALUES( @p3, @p4,@p5);
COMMIT;
END"
.......
.......
Catch ex As Exception
lblALabel.Text = "Username is taken"
End Try