2011-02-17 52 views
0

我使用強類型數據集(MSDataSetGenerator上XSD運行)做一些在一個項目我工作的DB訪問,我已經打了MSDataSetGenerator使用的ExecuteScalar()有點問題。代替的ExecuteNonQuery()

由於我使用的標識列作爲我的表的主鍵,我想,當我調用insert()方法返回新生成的ID。然而,正在與插入方法生成TableAdapter的如下:

public int Insert(...stuff...) 
{ 
    // Sets up the command.. 

    return this.InsertCommand.ExecuteNonQuery(); 
} 

返回受影響的行的數量(即1)。

儘管這是一個事實,即InsertCommandText產生爲:

public int Insert(...stuff...) 
{ 
    // Sets up the command.. 

    return (int)this.InsertCommand.ExecuteScalar(); 
} 

有誰知道,如果有:

INSERT INTO table VALUES (values...); 
SELECT Id, ...stuff... FROM table WHERE (Id = SCOPE_IDENTITY()); 

能明顯被用來通過而不是做以下返回ID一個方法,使MSDataSetGenerator使用ExecuteScalar功能,而不是的ExecuteNonQuery()?這似乎很奇怪,它會產生插入後直接選擇新的數據插入命令,但隨後不允許您檢索的數據!

謝謝你,埃德

回答

1

檢查ExecuteMode是否標量或NonQuery查詢的性能。