例如,我目前有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
主键的意思是唯一的,不连续的截断你的表。如果定义ID作为自动增量数据库会为您创建一个(其工作的一部分,多数民众赞成!)这意味着你没有在SQL INSERT语句中指定它 – Plutonix
@Plutonix我已经设置了ID为自动增量 – Man16
解决您的问题,长您需要创建子系统,在子系统中定期关闭[生产]小时将运行一个过程,该过程将枚举所有密钥并将所有“间隙”密钥写入单独的表中。然后,在你的'Insert'逻辑中,你将尝试保留一个间隙数字并将它用作PK,而你需要禁用自动增量。当没有可用的间隙号码时,您将像平常一样继续插入。但是你为什么要这么麻烦呢?你会得到什么?这值得么? –