插入语句不插入正确的自动增量

时间:2015-12-04 19:32:59

标签: mysql vb.net insert

例如,我当前在表中有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 Admin=@admin, UserForename=@forename, UserSurname=@surname, Username=@username, UserAddress=@address, UserPostcode=@postcode, UserEmailAddress=@emailaddress WHERE UserID=@userID"
            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 个答案:

答案 0 :(得分:0)

  1. 使用TRUNCATE TABLE table_name
  2. 截断表
  3. 按所需顺序插入行