I've updated my TableAdapter's advanced options, so it would Refresh the data table.

I'm looking to retrieve the SCOPE_IDENTITY of the new added row, right after I use TableAdapter.Update.

How can this be achieved?

Thanks!

有帮助吗?

解决方案

The TableAdapter will insert the SCOPE_IDENTITY automatically at the end of the insert statement. The DataRow contains the newly created identity value after the insert. Just don't set the PK column before you add the row to the table and update it via the TableAdapter.

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top