0

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

由于我使用 Identity 列作为表的主键,因此我想在调用 Insert() 方法时返回新生成的 ID。但是,TableAdapter 是使用 insert 方法生成的,如下所示:

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()?它会生成一个插入命令,在插入后直接选择新数据,但随后不允许您检索该数据,这似乎很奇怪!

谢谢,埃德

4

1 回答 1

1

检查查询属性中的 ExecuteMode 是 Scalar 还是 NonQuery。

于 2011-08-12T03:55:39.427 回答