![](/img/trans.png)
[英]Is it posible to set the order of the serialization of a custom property of a custom component/control in the designer-generated code?
[英]How can I manually set an ID for an AUTOINCREMENT column using a designer-generated DataTable?
我正在使用C#為圖書館系統創建數據庫系統(使用Access數據庫),但遇到了麻煩。 我有一個名為“ BSUser”的表,該表具有一個用於主鍵“ UserID”的自動遞增列; 如果用戶在定義新用戶時未輸入值,我希望此方法可以自動生成,但是我也希望它能夠接受手動定義的值。 給定用戶輸入的信息,我正在使用以下代碼創建新行。 請注意檢查用戶ID是否為空...
BookshelfDataSet.BSUserRow user = dataSet.BSUser.NewBSUserRow();
if (userID != null) user.UserID = userID.Value;
user.Forename = forename;
user.Surname = surname;
user.IsAdmin = isAdmin;
dataSet.BSUser.AddBSUserRow(user);
SaveDataSetToDatabase();
這是應該更新數據庫的代碼...
private void SaveDataSetToDatabase()
{
this.bookTableAdapter.Update(this.dataSet);
this.copyTableAdapter.Update(this.dataSet);
this.userTableAdapter.Update(this.dataSet);
this.loanTableAdapter.Update(this.dataSet);
this.ratingTableAdapter.Update(this.dataSet);
}
該行已成功保存到數據庫,但是盡管我已經手動設置了該行,但它的ID還是自動生成的。 這是一個問題,因為庫的現有用戶已經具有舊系統中的ID,因此我需要將其帶到新系統中。 請有人能告訴我我在做什么錯以及如何解決該問題嗎?
對於使用Access 2010或更高版本的用戶,可以使用“更改前”數據宏來獲得所需的結果。 對於名為[BSUser]的示例表:
[UserID] –長整數,主鍵
[ExistingID] –長整數
[姓氏] –文字(100)
[姓氏] –文字(100)
和更改前數據宏
如果我們包含[ExistingID]值,則它將用作[UserID]主鍵(前提是表中尚不存在)
var ta = new Database1DataSetTableAdapters.BSUserTableAdapter();
ta.Insert(null, 3, "Gord", "Thompson"); // insert with ExistingID = 3
導致
UserID ExistingID Forename Surname
------ ---------- -------- -----------
3 3 Gord Thompson
如果我們不包含[ExistingID]值,則宏將為我們分配一個
var ta = new Database1DataSetTableAdapters.BSUserTableAdapter();
ta.Insert(null, null, "Nora", "Newuser"); // insert with ExistingID = null
像這樣
UserID ExistingID Forename Surname
------ ---------- -------- -----------
3 3 Gord Thompson
1000 Nora Newuser
您必須使用自動遞增的值,也可以自行設置。 不能同時選擇兩者。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.