2015-12-04 57 views
0

例如,我目前有3個表中的行與0,1和3。主鍵,我希望它填補了國內空白,所以接下來的INSERT語句應採取主鍵爲2.但是,它並沒有這樣做。插入語句不插入使用正確的自動增量

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click 
    Dim msg As String = "" 

    If Validate.CheckAddress(txtAddress.Text) And _ 
     Validate.CheckEmailAddress(txtEmailAddress.Text) And _ 
     Validate.CheckPhoneNumber(txtPhoneNumber.Text) And _ 
     Validate.CheckPostcode(txtPostcode.Text) Then 

     MsgBox("correct validation") 

     Dim Admin As Integer = 0 
     Dim Query As String 

     If chkAdmin.Checked Then 
      Admin = 1 
     Else 
      Admin = 0 
     End If 

     If Not frmLogin.User.DoesUserExist(CInt(lblUserIDValue.Text)) Then 'if user doesnt exist 
      Query = "INSERT INTO tbluser (Admin, UserForename, UserSurname, Username, Password, UserPhoneNumber, UserAddress, UserPostcode, UserEmailAddress) VALUES (@admin, @forename, @surname, @username, @password, @phonenumber, @address, @postcode, @emailaddress)" 
      MsgBox("we are inserting") 
     Else 
      Query = "UPDATE tbluser SET [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected] WHERE [email protected]" 
      MsgBox("we are updating") 
     End If 

     Using Conn As New MySqlConnection(MySQL.ConnectionDetails) 
      Using Comm As New MySqlCommand() 
       With Comm 
        .Connection = Conn 
        .CommandText = Query 
        .CommandType = CommandType.Text 
        .Parameters.AddWithValue("@admin", Admin) 
        .Parameters.AddWithValue("@forename", txtForename.Text) 
        .Parameters.AddWithValue("@surname", txtSurname.Text) 
        .Parameters.AddWithValue("@username", (txtForename.Text + txtSurname.Text.First + txtPhoneNumber.Text.Last.ToString)) 'firstname + first letter of surname + last number of phone number 
        .Parameters.AddWithValue("@password", "password") 
        .Parameters.AddWithValue("@phonenumber", txtPhoneNumber.Text) 
        .Parameters.AddWithValue("@address", txtAddress.Text) 
        .Parameters.AddWithValue("@postcode", txtPostcode.Text) 
        .Parameters.AddWithValue("@emailaddress", txtEmailAddress.Text) 
        .Parameters.AddWithValue("@userID", CInt(lblUserIDValue.Text)) 
       End With 
       Try 
        Conn.Open() 
        If Comm.ExecuteNonQuery() Then 
         MsgBox("Successfull ") 
         Me.Dispose() 
        End If 
       Catch ex As MySqlException 
        msg += "MySQL Exception " & ex.GetBaseException.ToString 
       End Try 
      End Using 
     End Using 

    Else 

     If (Validate.CheckAddress(txtAddress.Text) = False) Then 
      msg += "Not a valid address " & Environment.NewLine 
     End If 
     If (Validate.CheckEmailAddress(txtEmailAddress.Text) = False) Then 
      msg += "Not a valid email address " & Environment.NewLine 
     End If 
     If (Validate.CheckPhoneNumber(txtPhoneNumber.Text) = False) Then 
      msg += "Not a valid phone number " & Environment.NewLine 
     End If 
     If (Validate.CheckPostcode(txtPostcode.Text) = False) Then 
      msg += "Not a valid postcode " 
     End If 

    End If 

    If (msg <> "") Then 
     MessageBox.Show("The following problems were found: " & Environment.NewLine & msg, "User - Error(s)", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 
    End If 

End Sub 
+1

主鍵的意思是唯一的,不連續的截斷你的表。如果定義ID作爲自動增量數據庫會爲您創建一個(其工作的一部分,多數民衆贊成!)這意味着你沒有在SQL INSERT語句中指定它 – Plutonix

+0

@Plutonix我已經設置了ID爲自動增量 – Man16

+0

解決您的問題,長您需要創建子系統,在子系統中定期關閉[生產]小時將運行一個過程,該過程將枚舉所有密鑰並將所有「間隙」密鑰寫入單獨的表中。然後,在你的'Insert'邏輯中,你將嘗試保留一個間隙數字並將它用作PK,而你需要禁用自動增量。當沒有可用的間隙號碼時,您將像平常一樣繼續插入。但是你爲什麼要這麼麻煩呢?你會得到什麼?這值得麼? –

回答

0
  1. 使用TRUNCATE TABLE表名
  2. 插入你的行中所需的序列
+0

我不想完全清空表格,但... – Man16

+1

然後與當前的ID生活在一起:P –

+0

@ Man16 ***「我不想完全清空表格」***,您可以將這些數據轉儲到臨時table/table變量,然後'TRUNCATE'表格,最後將其全部轉回... – Codexer