问题描述
有一个 GetLastInsertId 在WebMatrix中的方法。但是,使用反射器,我可以看到它像这样实现的:
There is a GetLastInsertId method in WebMatrix. However, using Reflector, I can see that it's implemented like this:
[return: Dynamic]
public object GetLastInsertId()
{
return this.QueryValue("SELECT @@Identity", new object[0]);
}
其中,据我所看到的,是做的非常糟糕的方式,因为 @@身份
返回最后插入考虑到每个表和每一个会话。我需要这个限制的范围和会话我的工作,所以我期待这种使用 SELECT SCOPE_IDENTITY()
,因为这也似乎是什么是最常见的根据我的阅读使用。
Which, as far as I can see, is a very bad way of doing it, because @@Identity
returns the last insert considering every table and every session. I need this restricted to the scope and session I'm working with, so I was expecting this to use SELECT SCOPE_IDENTITY()
, since that also seems to be what is most often used according to my reading.
我的问题是:
- 请在WebMatrix的包装做一些让这个确定在我的情况下使用?
- 如果没有,有没有一个简单的方法使用WebMatrix的类来获得一个插入查询的插入ID,或者我应该求助于这样的东西的SqlClient此?
我感兴趣的SQL Server(不紧凑)的答案,如果有差别。
I am interested in answers for SQL Server (not compact) if that makes a difference.
推荐答案
To quote David Fowler, one of the devs on the Data APIs in WebMatrix: "[SCOPE_IDENITTY] doesn't work on ce". Since we wanted a data API that would work on both CE and regular SQL Server, we used @@Identity. So if you want to use SCOPE_IDENTITY specifically, you should use db.QueryValue method:
var db = Database.Open(...);
db.Insert(...);
var lastInsertId = db.QueryValue("SELECT SCOPE_IDENTITY()")
这篇关于如何获得插入的行ID与WebMatrix中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!