为什么MS Access 2007年不允许行插入,但随后允许其在下次插入的企图? [英] Why does MS Access 2007 not allow a row insert, but then allow it on the next insert attempt?

查看:383
本文介绍了为什么MS Access 2007年不允许行插入,但随后允许其在下次插入的企图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的INSERT语句:

  INSERT INTO myTable的(INST_ID,USER_ID,APP_ID,类型,accessed_on)
VALUES(3264,2580,'MyApp的','更新',现在);



...其中所有的值的格式是否正确。该表有上述领域和另外一个,一个长整型自动递增键字段。外键是INST_ID,USER_ID和APP_ID



我从Access收到此错误:



...和VS 2005以下错误,当它出现了错误:




System.Data.OleDb.OleDbException:您请求表
的改变并不成功,因为他们会
创建在索引中,
主键或关系重复值。更改

包含重复数据的字段或字段中的数据,去掉
指数,或重新定义索引以允许
重复项,然后再试一次。




在使这个插入查询我可以看看到数据库中,看到外键值的他们各自的表中不存在,并已数月(对于具体的例子我使用)。这些字段也设置这样我就可以有重复,所以这不是问题。在其他表这种性质的呼叫的伟大工程。我不需要插入查询提供自动递增键值,它会自动添加它为我(像它应该)。



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

 
{
//此处执行查询...
}

{
//执行相同的查询再次
}

...或者,如果我只是尝试,并在访问执行此两次,它的工作原理。



有没有人遇到过吗?再次,这种类型的插入物适用于其他表中,所有的外键都存在各自的表中,该表的主键被设定为自动增量'和所有字段(当然比主键字段之外)是。设置为允许重复



任何想法



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

  ID:自动编号(字段大小=长基于整数,新值=递增,索引=是 - 无重复) 

INST_ID:编号(字段大小=长基于整数,必需=是,索引=是 - 重复)

USER_ID:数(字段大小=长基于整数,必需=是,索引=是 - 重复)

APP_ID:文本(字段大小= 255,必需=是,索引=是 - 重复)

型:文本(字段大小= 50,必需=是,索引=否)

accessed_on:日期/时间(默认值= NOW(),必需=是,索引=否)


解决方案

这是一些老的记忆在这里...



尝试把一个时间戳字段在表



我不记得确切原因,工程 - 事做有困难访问识别记录/或许有些一种锁定或索引怪癖。我以前做了这几年的一些研究,当它发生在我的表之一。



关键违反了错误指的不是的缺少另一个表中的关键,它是在同一个表中的重复的关键。有时,访问得到它的电线交叉,认为它是分配给新的记录键已经分配给表中的另一个记录。我不知道是什么原因导致这种情况发生。但是,通过将一个时间戳字段的表,它会导致访问不同的想法。



这是一个令人沮丧的修正,因为我不知道为什么它的工作原理。现在我已经在我的表原本无用的时间戳字段。但是,就这样吧。


My insert statement is:

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

...where all of the values are formatted correctly. The table has the above fields and one other, a long int auto-increment key field. The foreign keys are 'inst_id', 'user_id', and 'app_id'.

I am getting this error from Access:

...and the following error from VS 2005 when it errors out:

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.

When making this insert query I can look into the database and see that the each of the foreign key values exist in their respective tables and have been for months (for the particular example I am using). These fields are also set so that I can have duplicates, so that is not the issue. Calls of this nature in other tables works great. I do not need to supply the auto-increment key value in the insert query, it adds it for me automatically (like it should).

The weird thing is that if I do this in my code:

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

...or if I just try and execute this within Access twice, it works.

Has anyone encountered this before? Again, this type of insert works for other tables, all foreign keys are present in their respective tables, the primary key of this table is set as 'Auto-increment', and all fields (other than the primary key field of course) are set to allow duplicates.

Any ideas?

EDIT: Largest key before inserting: 343085. Largest key after inserting: 343086. The format is:

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)

解决方案

Going by some old memory here...

Try putting a timestamp field in your table.

I can't remember exactly why that works -- something to do with Access having difficulty identifying records / maybe some kind of locking or indexing quirk. I did some research on that several years ago when it happened to one of my tables.

The key violation the error refers to isn't a missing key in another table, it's a duplicate key in the same table. Sometimes, Access gets it's wires crossed and thinks that the key it's assigning to the new record is already assigned to another record in the table. I don't know what causes that to happen. But by putting a timestamp field in the table, it causes Access to think differently.

It's a frustrating fix, because I don't know why it works. And now I have an otherwise useless timestamp field in my table. But so be it.

这篇关于为什么MS Access 2007年不允许行插入,但随后允许其在下次插入的企图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆