使用ExecuteScalar()而不是ExecuteNonQuery()的MSDataSetGenerator

时间:2011-02-17 15:51:42

标签: c# sql-server xsd executenonquery msdatasetgenerator

我正在使用强类型数据集(在XSD上运行MSDataSetGenerator)来处理我正在处理的项目中的一些数据库访问,我遇到了一些问题。

由于我使用Identity列作为表的主键,因此我想在调用Insert()方法时返回新生成的ID。但是,使用insert方法生成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()?奇怪的是,它会生成一个插入命令,在插入后直接选择新数据,但不允许您检索该数据!

谢谢,Ed

1 个答案:

答案 0 :(得分:1)

在查询的属性中检查ExecuteMode Scalar或NonQuery。