c向sql中插入資料時如何自動生成編號作為主鍵

時間 2021-10-14 22:23:54

1樓:不識臺北路

建立**的時候將主鍵設定為identitycreate table test

(id int primary key identity,value nvarchar(128) not null)插入資料的時候,不要給主鍵賦值,主鍵會自動增長insert into test values('value1')insert into test values('value2')資料表中的資料為:

id    value

1    value1

2    value2

當然,如果你不希望id從1開始,或者希望每條記錄之間的增量不為1,你可以這樣來建立你的**:

create table test

(id int primary key identity(10000,2),

value nvarchar(128) not null)這樣,你的**中的第一條記錄的id就是10000,而每次增加一條記錄,id的增量為2.

2樓:匿名使用者

把資料表的主鍵設為標識列自動增長

3樓:匿名使用者

把資料表的主鍵設為標識列自動增長,不建議把 資料庫中自動增長的列作為主鍵。。可以自己設定一個列作為主鍵 如:guid

怎樣用SQL向資料庫中批量的插入資料,主鍵是隨機生成的

使用資料型別uniqueidentifier最合適了 建立表 create table a tmp primary id uniqueidentifier not null col1 varchar 10 collate chinese prc ci as null,col2 varchar 10 ...

在C中,如何使用sql語句向資料庫中新增資訊

禹仙居安然 正常情況下,sql server中能實現的語句在c 中aqlcommand中執行是沒有問題,我們做開發的時候一般是sql server中先驗證查詢語句沒問題了才往c 中新增。您這裡導致這種情況的可能是 c 中字串的拼裝需要注意的一些問題,你可以在執行sqlcommand的 處斷點,然後檢...

c如何讀取SQL中的資料,C 中怎麼讀取SQL資料庫表中的資料後顯示在一個TEXTBOX中

string strconn data source 資料庫伺服器名稱或ip initial catalog 資料庫名稱 persist security info true user id sa password password sqlconnection conn new sqlconnect...