C# SqlParameter - 提供 SQL (Microsoft SQL)
我目前的任务是处理一个数据库项目,该数据库的架构无法更改。我需要在一个要求 ID 唯一的表中插入一个新行,但该结构的原始创建者没有将此值设置为自动增量。为了解决这个问题,我一直在使用类似于以下的代码:
(SELECT TOP 1 [ID] from [Table] ORDER BY [ID] DESC) + 1
在给出 ID 字段的值时,基本上有一个内部查询。问题是,在接下来的几行中,我需要刚刚输入的 ID。如果我可以设置一个 SQLParameter 来输出该列,我可以获得它设置的值,问题是我使用的是 SQL,而不是像我使用其他 SQLParameters 那样的硬值。我不能使用 SQL 来代替值吗?
这是一个潜在的高容量交换,所以我不想做 2 个不同的查询(一个用于获取 id,然后一个用于插入)。
I am currently tasked with a project on a database whose schema cannot be changed. I need to insert a new row into a table that requires an ID to be unique, but the original creators of the structure did not set this value to autoincrement. To go around this, I have been using code akin to:
(SELECT TOP 1 [ID] from [Table] ORDER BY [ID] DESC) + 1
when giving the value of the ID field, basically having an inner query of sorts. Problem is that a few lines down, I need that ID I just inputted. If I could set a SQLParameter to output for this column, I could get the value it was set to, problem is I'm using SQL, and not a hard value like I do with other SQLParameters. Can't I use SQL in place of just a value?
This is a potential high volume exchange, so I'd rather not do 2 different queries (one to get id, then one to insert).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您说您无法更改架构,但是您可以向项目中添加一个额外的表来执行自动增量列吗?然后您可以使用该表(安全地)创建新 ID 并将其返回到您的代码。
这类似于 Oracle 处理 ID 的方式,有时也在 Oracle 上运行的 sql server 供应商应用程序将使用该方法只是为了帮助最小化两个数据库之间的差异。
更新:
啊,我刚刚发现你对另一个答案的评论。在这种情况下,我认为唯一可行的另一件事是将您的两个语句(插入新 ID,然后读回新 ID)放入带有 可串行化 隔离级别。这有点糟糕,因为它让你对性能和锁定陷阱持开放态度。
You say you cannot change the schema, but can you add an additional table to the project that does an autoincrement column? Then you could use that table to (safely) create your new IDs and return them to your code.
This is similar to how Oracle does IDs, and sometimes vendor applications for sql server that also run on Oracle will use that approach just to help minimize the differences between the two databases.
Update:
Ah, I just spotted your comment to the other answer here. In that case, the only other thing I can think that might work is to put your two statements (insert a new ID, and then read back the new ID) inside a transaction with the SERIALIZABLE isolation level. And that just kinda sucks, because it leaves you open to performance and locking gotchas.
您是否可以在数据库中创建一个存储过程来执行此操作,并且存储过程的返回值将返回您需要的 ID?
Is it possible for you to create a stored procedure in the database to do this and the return value of the stored procedure will then return the ID that you need?
我有点困惑你需要在哪里使用这个 ID。如果它在同一个存储过程中,只需使用此方法:
I'm a bit confused about where you need to use this ID. If it inside of the same stored proc just use this method:
您可以在单个 SqlCommand 中放置多个 SQL 语句。因此,您可以轻松地按照 Abe 建议:
然后你只需在 SqlCommand 上调用 ExecuteScalar,它将执行 INSERT,然后返回它使用的 ID。
You can put more than one SQL statement in a single SqlCommand. So you could easily do something along the lines of what Abe suggested:
Then you just call ExecuteScalar on your SqlCommand, and it will do the INSERT and then return the ID it used.