c# - 为什么 MS Access 2007 不允许行插入,但在下一次插入尝试时允许它?

标签 c# sql insert ms-access-2007 key

我的插入语句是:

INSERT INTO myTable (inst_id,user_id,app_id,type,accessed_on)
VALUES (3264,2580,'MyApp','Renew',Now);

...所有值的格式都正确。该表具有上述字段和一个其他字段,一个 long int 自动递增键字段。外键是“inst_id”、“user_id”和“app_id”。

我从 Access 收到此错误:access error

...以及 VS 2005 出错时的以下错误:

System.Data.OleDb.OleDbException: The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.

在进行此插入查询时,我可以查看数据库并查看每个外键值都存在于各自的表中并且已经存在了几个月(对于我正在使用的特定示例)。还设置了这些字段,以便我可以重复,所以这不是问题。在其他表中进行这种性质的调用效果很好。我不需要在插入查询中提供自动递增键值,它会自动为我添加它(就像它应该的那样)。

奇怪的是,如果我在我的代码中这样做:

try
{
    //Execute the query here...
}
catch
{
    //Execute the same query again
}

...或者如果我只是尝试在 Access 中执行两次,它就可以工作。

有没有人遇到过这个?同样,这种类型的插入适用于其他表,所有外键都存在于各自的表中,该表的主键设置为“自增”,并且所有字段(当然除了主键字段)都是设置为允许重复。

有什么想法吗?

编辑: 插入前的最大键:343085。插入后的最大键:343086。格式为:

id: AutoNumber (Field Size=Long Interger, New Values=Increment, Indexed=Yes - No Duplicates)

inst_id: Number (Field Size=Long Interger, Required=Yes, Indexed=Yes - Duplicates OK)

user_id: Number (Field Size=Long Interger, Required=Yes, Indexed=Yes - Duplicates OK)

app_id: Text (Field Size=255, Required=Yes, Indexed=Yes - Duplicates OK)

type: Text (Field Size=50, Required=Yes, Indexed=No)

accessed_on: Date/Time (Default Value=Now(), Required=Yes, Indexed=No)

最佳答案

在这里回顾一些旧的内存......

尝试在您的表中放置一个时间戳字段。

我不记得它为什么有效——与 Access 难以识别记录有关/可能是某种锁定或索引怪癖。几年前,当我的一张 table 发生这种情况时,我对此进行了一些研究。

错误指的键违规不是另一个表中的缺失键,而是同一个表中的重复键。有时,Access 搞错了,认为分配给新记录的键已经分配给表中的另一条记录。我不知道是什么原因导致这种情况发生。但是通过在表中放置一个时间戳字段,它会导致 Access 以不同的方式思考。

这是一个令人沮丧的修复,因为我不知道它为什么会起作用。现在我的表中有一个无用的时间戳字段。但就这样吧。

关于c# - 为什么 MS Access 2007 不允许行插入,但在下一次插入尝试时允许它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5226277/

相关文章:

C# : set default payment method in stripe

c# - 从 Dropbox 上的公开共享文件夹下载图像

android - 查询文本文件到数据库中

Mysql搜索以单词开头的列

sql - 如何排除sql server insert语句中的行?

c# - 遗留应用程序中的数字到二进制转换

c# - ASP .Net 从 SQL Server Compact 数据库获取当前用户 ID

c++ - CGAL 4.4 安排插入(arr,曲线)与一些曲线崩溃

mysql - 选择最大值并选择其他列

c# - 在 C# 中第 1 行的 'Value1' 列数据被截断