2014-03-25 65 views
0

我有代碼,如果有重複,那麼它會說「用戶名已存在」。但是,我們繼續在我的數據庫中記錄其他數據,如firstname,lastnamemiddlenameusername除外)。如果一個數據有重複,我怎樣才能阻止它記錄其他值?嘗試並捕獲記錄數據

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 
+1

首先檢查記錄用戶名是否已經存在或不存在。如果不存在,則插入數據。 – Oasis

回答

0

嘗試......
它會首先檢查「UserName」,如果不存在的話,你的數據將被插入。

Try 
     Const SQLStatement As String = "IF NOT EXISTS(SELECT * FROM ACCOUNT WHERE [email protected]) 
     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