1
我要增量ID添加主鍵另一個表如何自動增量數據類型插入到另一個表C#
這裏是我的代碼
int id = null; //this id is public
OleDbCommand command = new OleDbCommand("insert into Table1([ID], [Name] values (@ID, @Name)", connection);
command.Parameters.AddWithValue("@ID", id);
command.Parameters.AddWithValue("@ID", txtName.Text);
command.ExecuteNonQuery();
command.Parameters.Clear();
添加到這個數據庫 add to this database
OleDbCommand command2 = new OleDbCommand("insert into Table2([TableID], [OtherInfo], [OtherDocument] values (@TableID, @OtherInfo, @OtherDocument)", connection);
command2.Parameters.AddWithValue("@TableIDID", id);
command2.Parameters.AddWithValue("@OtherInfo", txtOtherInfo.Text);
command2.Parameters.AddWithValue("@OtherDocument", txtOtherDocument.Text);
command2.ExecuteNonQuery();
command2.Parameters.Clear();
i灣t每次我添加一個新的名稱ID字段也添加在其他表對不起我的英語感謝。
我認爲你不需要添加任何值到該列數據類型本身說它的自動增量只是留下ID列 – Dandy
但是。我如何將autoincrement數據類型添加到另一個表中? –
你不需要在第二個表中增加。你可以做什麼是當你插入第一個表中的東西獲取最大(id)和插入到第二個表。 – Nitin