知道asp.net和sql server中最后插入的行的ID? [英] Knowing the Id of the last inserted row in asp.net and sql server?

查看:50
本文介绍了知道asp.net和sql server中最后插入的行的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在asp.net中使用类型化的数据集,并且要插入具有自动递增的主键字段的表之一.但是我需要获取在该字段中插入的最后一个值,这意味着在表中插入了最后一行.我该怎么做?我已经尝试过选择身份,但是它不起作用.还有其他方法吗?

预先感谢.

Hi all ,

I am using typed datasets in asp.net,and i am inserting into one of the tables which have auto incremented primary key field.But i need to get the last value inserted in this field,which means the last row inserted in table.So how can i make it ?.I have already tried select identity but it did not work.So is there any other way ?

Thanks in advance.

推荐答案

我已经弄清楚了.所以对于所有想要解决同一问题的人.这就是我所做的:

我向数据库添加了存储过程,它是:

I have already figured it out.So for all people that wants a solution for the same issue.This is what i did :

I added a stored procedure to the database and here it is :

ALTER PROCEDURE dbo.StoredProcedure1


    @Inventory_Title nvarchar(500)


AS
INSERT INTO Inventory
                         (Inventory_Title)
VALUES        (@Inventory_Title)
Select SCOPE_IDENTITY()
    /* SET NOCOUNT ON */
    RETURN



然后我在表适配器中添加了一个查询,该查询以存储的程序作为其源.然后我使用c#从代码中调用了该方法,这是代码:



Then i added a query into the table adapter,that has the stored procuder as its source.Then i called the method from the code using c# and this is the code :

DataSet1TableAdapters.InventoryTableAdapter da=new DataSet1TableAdapters.InventoryTableAdapter();



int id = Convert.ToInt32( da.InsertWithReturn("Inv1"));



通过这种方式,我得到了最后插入的ID.



And by this way i got the last inserted id.


您可以尝试:

ID_of_last_record + 1

插入并返回上一条记录
you can try :

ID_of_last_record+1
or
Insert and return Last record


在Visual Studio中,您可以打开.xsd文件并选择配置表适配器.选择高级选项"按钮,然后选择刷新数据表"复选框.
In Visual Studio you can open your .xsd file and select the configure the table adapter. Select the "Advanced Options" button and select the "refresh data table" checkbox.


这篇关于知道asp.net和sql server中最后插入的行的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆