您请求对 table 的更改未成功

The changes you requested to the table were not successful

在有人开始大喊"duplicate"之前,让我说一下,我已经彻底搜索了这个问题的解决方案,但没有找到任何解决方案。

背景信息:我正在尝试从使用 Visual Basic 2010 Express 编译的 windows 表单程序 (exe) 插入 Access 2003 数据库中的一个小 table。

在同一个数据库中执行了四个插入语句,每个都在不同的 table 上执行,但前三个没有问题,第四个抛出错误:

cmd = New OleDbCommand("INSERT INTO PrivateTableName 
                        VALUES('" & Serial.Text & "','Pending',
                               '" & coverArea.Text & "','')", myConnection)
Try
     cmd.ExecuteNonQuery()
Catch ex As Exception
     MsgBox("Error attempting to insert into PrivateTableName " & ex.ToString)
     Exit Sub
End Try

出于保护隐私的目的,我更改了 table 的名称,但请放心,table 名称与此无关。

这条语句抛出异常

The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship

错误本身是不言自明的,但完全不可能。下面是 table 设计

的屏幕截图

注意主键不是自动递增的数字。主键是一个序列号(或者更确切地说是一个字符串),它由一个 5 位数字(从前一个顺序索引)和两个随机生成的字母(即 14582KH)组成,我已经验证了该值它尝试插入,因为主键尚不存在。

我已经尝试过压缩和修复数据库,但没有帮助。 table中的字段与其他table中的字段没有任何关系,完全独立。

有什么想法吗?

区域列的数据类型为数字,但您使用

向其中插入了一个字符串
"'" & coverArea.Text & "'"

您应该将命令创建为:

cmd = New OleDbCommand("INSERT INTO PrivateTableName VALUES('" & 
 Serial.Text & 
 "','Pending'," 
 & coverArea.Text 
 & ",'')", myConnection)

我无法想象这会在调试模式下工作。仅当您 运行 直接针对您可能会使用正确语法的数据库的 INSERT 语句时。

更好更安全的方法是使用 DbParameters

虽然我不确定它为什么重要,但指定字段似乎可以解决问题。

cmd = New OleDbCommand("INSERT INTO PrivateTableName ([My Order Number],
[BV Order Number], [Area]) VALUES('" & Serial.Text & _ "','Pending'," &
coverArea.Text & ")", myConnection)

首先,您应该检查是否有任何字段错误。如果不是选择是(重复OK)或否。