TableAdapter更新后的SCOPE_IDENTITY()

时间:2012-12-05 09:06:11

标签: vb.net vb.net-2010 tableadapter scope-identity

我已经更新了TableAdapter的高级选项,因此它会刷新数据表。

我正在寻找在使用TableAdapter.Update后立即检索新添加的行的SCOPE_IDENTITY。

如何实现这一目标?

谢谢!

1 个答案:

答案 0 :(得分:1)

TableAdapter会自动在插入语句的末尾插入SCOPE_IDENTITYDataRow包含插入后新创建的标识值。在将行添加到表中并通过TableAdapter更新之前,不要设置PK列。

Dim daLocation As New LocationTableAdapter() ' the TableAdapter
Dim newLocation = Location.NewLocationRow() ' the new DataRow

newLocation.Name = "Berlin"
Location.AddLocationRow(newLocation)
' now update the DataTable or the DataRow with the TableAdapter 
Dim numRowsUpdated As Int32 = daLocation.Update(newLocation) 
Dim id As Int32 = newLocation.idLocation ' now the new ID is available