如何获取上次插入的ID? [英] How do I get the Last Inserted ID?
问题描述
首先我输入了一个值:
First I inserted a value:
QUERY( Insert into Table values ("TRY"))
然后我想把最后插入的id的值放到文本框中。因为我不知道如何在文本框中绑定。我首先使用了gridview。
Then I wanted to put the value of the last inserted id to a textbox. Since I do not know how to bind in textbox . I used gridview first.
<pre lang="xml">QUERY(Select last_insert_id)
GRV_Dat7.DataSource = ds
GRV_Dat7.DataBind()
TextBox_IDNew1.Text = GRV_Dat7.Rows(0).Cells(0).Text</pre>
但这不起作用。为什么?谁能帮我?谢谢。
But this didnt work. Why? Can anyone help me? thanks.
推荐答案
在sql server中使用@@ IDENTITY你将得到最后一个插入的id并使用输出参数你可以将该值反转到你的代码
by using @@IDENTITY in sql server u will get last inserted id and using output parameters u can retrive that value in to ur code
你好NekoNao,
在ASP.Net中从SQL Server获取最后一个插入的id你可以这样:
1)编写一个过程来插入记录,方法是将参数作为要插入特定表的所有值传递。还声明一个额外的输出参数,以获取该范围内表的最后插入记录ID。
Ex: -
Hi NekoNao,
In ASP.Net to get the last inserted id from SQL Server you can go like this :-
1) Write a procedure to insert the records by passing parameter as all the values you want to insert to a specific table. Also declare an extra output parameter to get the last inserted record id to the table in that scope.
Ex :-
CREATE PROCEDURE [dbo].[SP_InsertTestData]
(
@Value1 VARCHAR(20)
, @Value2 VARCHAR(MAX)
, @Value3 VARCHAR(50) = ''
, @LastInsertedRecordID INT OUTPUT
)
AS
BEGIN
**********
END
2)您可以在插入操作之后使用SQL Server中的SCOPE_IDENTITY()方法获取最后插入的记录ID,然后将此值分配给特定的输出参数。
Ex: -
2) You can get the last inserted record id using SCOPE_IDENTITY() method in SQL Server using just after the insertion operation then assign this value to the particular output parameter.
Ex:-
--Inside the procedure body
--Below statement goes just after insert statement
SET @LastInsertedRecordID = SCOPE_IDENTITY()
3)现在要在ADO.Net代码中获取此ID并将此值设置为特定的Label或TextBox,这里可能会对您有所帮助。< br $> b $ b
点击这里 [ ^ ]
希望这对您有所帮助。
3) Now to get this ID in ADO.Net code and to set this value to a particular Label or TextBox is being explained here which might be further helpful for you.
Click Here[^]
Hope this definitely of help to you.
简单试试这个,
从table_name中选择MAX(your_identityId)
将此值绑定到文本框这是在你的gridview。
simple try this,
select MAX(your_identityId) from table_name
bind this value to textbox which is in ur gridview.
这篇关于如何获取上次插入的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!