MSDataSetGenerator使用ExecuteScalar()而不是ExecuteNonQuery()
我正在使用强类型数据集(在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()?奇怪的是,它会生成一个插入命令,在插入后直接选择新数据,但不允许您检索该数据!
谢谢,艾德
没有找到相关结果
已邀请:
1 个回复
庞协腿宛炯