通过 .net 数据集更新调用从插入中获取标识值
下面是一些示例代码,它将一条记录插入到数据库表中:
Dim ds As DataSet = New DataSet()
da.Fill(ds, "Shippers")
Dim RowDatos As DataRow
RowDatos = ds.Tables("Shippers").NewRow
RowDatos.Item("CompanyName") = "Serpost Peru"
RowDatos.Item("Phone") = "(511) 555-5555"
ds.Tables("Shippers").Rows.Add(RowDatos)
Dim custCB As SqlCommandBuilder = New SqlCommandBuilder(da)
da.Update(ds, "Shippers")
它在 Shippers 表中插入一行,ShippersID 为 身份值。我的问题是如何检索 插入新行时生成的标识值 托运人表。
我已经进行了几次网络搜索,并且我在网上看到的资源没有具体回答它或继续谈论存储过程。任何帮助将不胜感激。谢谢!
Here is some sample code that inserts a record into a db table:
Dim ds As DataSet = New DataSet()
da.Fill(ds, "Shippers")
Dim RowDatos As DataRow
RowDatos = ds.Tables("Shippers").NewRow
RowDatos.Item("CompanyName") = "Serpost Peru"
RowDatos.Item("Phone") = "(511) 555-5555"
ds.Tables("Shippers").Rows.Add(RowDatos)
Dim custCB As SqlCommandBuilder = New SqlCommandBuilder(da)
da.Update(ds, "Shippers")
It inserts a row in the Shippers Table, the ShippersID is
a Indentity value. My question is how can i retrieve the
Identity value generated when the new row is inserted in
the Shippers table.
I have done several web searches and the sources I've seen on the net don't answer it speccifically or go on to talk about stored procedures. Any help would be appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是几年前的一篇优秀文章,给出了完美的解释以及实现这一目标的演示。包括显示该过程的屏幕截图。
有关返回行标识的数据适配器插入的 MSDN 文章
干杯!
彼得
Here's an excellent article of some years ago that gives a perfect explanation as well as the demo hwow to achieve this. INcludes screenshots showing the process.
MSDN article on data-adapter insert with returning the row-identity
Cheers!
Peter
您可以调用存储过程来执行插入,而不是使用 SqlCommandBuilder。在存储过程中,您可以将
IDENTITY
值作为OUTPUT
参数返回。请参阅 http://msdn.microsoft.com/en- us/library/ks9f57t0(VS.71).aspx 为例。
Instead of using SqlCommandBuilder, you could call a stored procedure to do the insert. In the stored procedure, you could return the
IDENTITY
value as anOUTPUT
parameter.See http://msdn.microsoft.com/en-us/library/ks9f57t0(VS.71).aspx for an example.
您可以使用IDENT_CURRENT()。
You can use IDENT_CURRENT().