使用 Microsoft.Jet.OLEDB.4.0 从 C# 将行插入 Access db,自动编号列设置为零

发布于 2024-07-06 13:52:30 字数 399 浏览 6 评论 0原文

我正在使用 C# 和 Microsoft.Jet.OLEDB.4.0 提供程序将行插入 Access mdb。

是的,我知道 Access 很糟糕。 这是一个巨大的遗留应用程序,其他一切都运行正常。

该表有一个自动编号列。 我插入行,但自动编号列设置为零。

我用谷歌搜索了这个问题并阅读了我能找到的关于这个主题的所有文章。 有人建议为自动编号列插入 -1,但这不起作用。 我找到的其他建议都不起作用。

我正在使用 OleDbParameter,而不是连接一个大的 SQL 文本字符串。

我尝试过有交易和没有交易的插入。 没有不同。

如何让此插入工作(即正确设置自动编号列内容)?

预先非常感谢,

亚当·莱弗特

I'm using C# and Microsoft.Jet.OLEDB.4.0 provider to insert rows into an Access mdb.

Yes, I know Access sucks. It's a huge legacy app, and everything else works OK.

The table has an autonumber column. I insert the rows, but the autonumber column is set to zero.

I Googled the question and read all the articles I could find on this subject. One suggested inserting -1 for the autonumber column, but this didn't work. None of the other suggestions I could find worked.

I am using OleDbParameter's, not concatenating a big SQL text string.

I've tried the insert with and without a transaction. No difference.

How do I get this insert to work (i.e. set the autonumber column contents correctly)?

Thanks very much in advance,

Adam Leffert

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愁以何悠 2024-07-13 13:52:30

在 Access 中,可以将显式值插入到 IDENTITY(又名 Automnumber)列中。 如果您(或您的中间件)将值零写入 IDENTITY 列,并且 IDENTITY 列上没有唯一约束,那么这可能会解释它。

需要明确的是,您应该使用语法

INSERT INTO (<column list>) ... 

,并且列列表应省略 IDENTITY 列。 Jet SQL 将允许您省略整个列列表,但随后隐式包含 IDENTITY 列。 因此,您应该使用 INSERT INTO () 语法来显式省略 IDENTITY 列。

在 Access/Jet 中,您可以将显式值写入 IDENTITY 列,在这种情况下,该值显然不会自动生成。 因此,请确保您和您的中间件(ADO.NET 等)没有显式地将零值写入 IDENTITY 列。

顺便说一句,下表中的 IDENTITY 列将每秒自动生成值零 INSERT:

CREATE Table Test1 
(
   ID INTEGER IDENTITY(0, -2147483648) NOT NULL, 
   data_col INTEGER
);

In Access it is possible to INSERT an explicit value into an IDENTITY (a.k.a. Automnumber) column. If you (or your middleware) is writing the value zero to the IDENTITY column and there is no unique constraint on the IDENTITY column then that might explain it.

Just to be clear you should be using the syntax

INSERT INTO (<column list>) ... 

and the column list should omit the IDENTITY column. Jet SQL will allow you to omit the entire column list but then implicitly include the IDENTITY column. Therefore you should use the INSERT INTO () syntax to explicitly omit the IDENTITY column.

In Access/Jet, you can write explicit values to the IDENTITY column, in which case the value will obviously not be auto-generated. Therefore, ensure both you and your middleware (ADO.NET etc) are not explicitly writing a zero value to the IDENTITY column.

BTW just for the IDENTITY column in the below table will auto-generate the value zero every second INSERT:

CREATE Table Test1 
(
   ID INTEGER IDENTITY(0, -2147483648) NOT NULL, 
   data_col INTEGER
);
茶底世界 2024-07-13 13:52:30

执行插入时,您需要确保没有为“自动编号”列指定值。 就像在 SQL Server 中一样,您不需要为标识列插入值。

When doing the insert, you need to be sure that you are NOT specifying a value for the AutoNumber column. Just like in SQL Server you don't insert a value for an identity column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文