[英]MSDataSetGenerator using ExecuteScalar() instead of ExecuteNonQuery()
我正在使用強類型的數據集(在XSD上運行MSDataSetGenerator)在我正在處理的項目中執行某些數據庫訪問,但是遇到了一些問題。
當我將Identity列用作表的主鍵時,我想在調用Insert()方法時返回新生成的ID。 但是,TableAdapter是使用以下插入方法生成的:
public int Insert(...stuff...)
{
// Sets up the command..
return this.InsertCommand.ExecuteNonQuery();
}
返回受影響的行數(即1)。
盡管事實是將InsertCommandText生成為:
INSERT INTO table VALUES (values...);
SELECT Id, ...stuff... FROM table WHERE (Id = SCOPE_IDENTITY());
顯然,可以通過執行以下操作來返回ID:
public int Insert(...stuff...)
{
// Sets up the command..
return (int)this.InsertCommand.ExecuteScalar();
}
有誰知道是否有一種方法可以使MSDataSetGenerator使用ExecuteScalar函數而不是ExecuteNonQuery()? 似乎會生成一個插入命令,該命令會在插入后立即選擇新數據,但是卻不允許您檢索該數據,這似乎很奇怪!
謝謝,埃德
檢查查詢屬性中的ExecuteMode是標量還是NonQuery。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.