简体   繁体   中英

INSERT command for auto increment field in access from asp.net website

I've got my primary key set to auto increment in my access database.

How do I specify that I want the primary key to have the next available value?

eg

OleDbCommand cmd = new OleDbCommand("INSERT into Table VALUES (?,"string","string");", conn);

thanks!

If it is set to auto increment you dont have to specify a value for it

just do

OleDbCommand cmd = new OleDbCommand("INSERT into Table VALUES ("string","string");", conn);

although one would prefer you write the column names explicitly

If the database field is already set to auto-incremente, then you don't need to worry about it!

Just insert the other fields! INSERT into Table VALUES ("string","string")

If you have a table as such:

Table

 ID    SomeString     SomeString2

where ID is the primary key, then you'd not need to specify the value for the primary key. It would be "auto-incremented" as you have specified that.

So you'd enter:

OleDbCommand cmd = new OleDbCommand("INSERT into Table (SomeString, SomeString2) VALUES ('stringA','stringB')", conn);

Note : Its a good practice to always mention the column names when performing insertion of records.

Hope this helps!!!

您可以跳过它:

OleDbCommand cmd = new OleDbCommand("INSERT into Table VALUES ("string","string");", conn);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM