1
我也遇到一个问题,我似乎无法绕到我的头。
我有一个表的数据库称为
客户 - 客户ID(主键),名字,姓氏,地址,邮编 和性别。它充满了512行。
我在VB中创建了一个小应用程序,您可以使用该信息填写表单,然后单击按钮注册新客户。下面是代码的样本:
Private Sub save(ByRef SQLStatement As String)
If minConnection.State = ConnectionState.Closed Then
myConnection.Open()
End If
With minCommand
.CommandText = SQLStatement
.CommandType = CommandType.Text
.Connection = minConnection
.ExecuteNonQuery()
End With
MsgBox("Saved to Database", MsgBoxStyle.Exclamation, "Saved!")
myConnection.Close()
myConnection.Dispose()
End Sub
Private Sub cmdCustomer_Click(sender As Object, e As EventArgs) Handles cmdCustomer.Click
myConnection.Open()
If rbW.Checked = True Then
sex = "W"
Else
sex = "M"
End If
Dim newCustomer As String = ("insert into customer(CustomerID, Firstname, Surname, Address, Zipcode, Sex) VALUES(" & txtCustomerID.Text & ", '" & txtFirstname.Text & "', '" & txtSurname.Text & "', '" & txtAddress.Text & "', " & txtZipcode.Text & ", '" & sex & "')")
save(newCustomer)
End Sub
在我的经验,这应该做的工作,但由于某种原因,我不能看到我的数据库中新的条目。但是,如果我尝试使用相同的CustomerID创建新客户,则会收到类似Duplicate Entry
的错误消息。有了这条消息,我知道数据应该已经被插入。为了进一步测试,我使用sql语句创建了一个datagridview:Select * from customer
。 datagridview确实显示我的新注册客户!
考虑到这一切,我认为问题出在MySQL Workbench中,而不是我的代码甚至是Visual Basic。虽然,我似乎无法找到如何解决这个问题。我在这里远吗?这只是一个愚蠢的错误?
对不起,我的英语,这不是我的第一语言,也是我的代码,因为它是非常业余的。预先感谢您提供的任何建议。
这可能是由MySQL的默认隔离级别'可重复读取'造成的。尝试在运行SELECT之前在SQL客户端运行'commit'(或'rollback')。这将结束当前的交易并开始新的交易。 – 2013-05-10 13:41:09
'Duplicate Entry'意味着您不能插入数据,因为有重复,请尝试首先删除重复。 – Rahul 2013-05-10 13:42:46
ZipCode字段是一个数字吗? – Steve 2013-05-10 13:42:47