开发者

MSDataSetGenerator using ExecuteScalar() instead of ExecuteNonQuery()

I'm using a strongly-typed dataset (MSDataSetGenerator run on an XSD) to do some of the DB access in a project I'm working on, and I've hit upon a bit of a problem.

As I'm using Identity columns as the primary key of my tables I want to return the newly-generated ID when I call the Insert() method. However, the TableAdapter is being generated with the insert method as follows:

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

    return this.InsertCommand.ExecuteNonQuery();
}

which returns the number of rows affected (i.e. 1).

This is despite the fact that the InsertCommandText is generated as:

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

Which can obviously be used to return the ID by instead doing the following:

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

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

Does anyone know if there's a way to make the MSDataSetGenerator use the Ex开发者_运维知识库ecuteScalar function as opposed to ExecuteNonQuery()? It seems odd that it would generate an insert command that selects the new data straight after the insert but then doesn't allow you to retrieve that data!

Thanks, Ed


Check the ExecuteMode whether Scalar or NonQuery in the properties of the query.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜