Is returning the ID of an inserted record (VB.NET/SQL Server 2005) always correct?
Posted on 2011-02-17
I have a piece of code that inserts a record into a database table that has an auto ID field and then returns the ID of the record:
Dim objDB As New Database
Dim objTemp As Object
objTemp = objDB.GetScalar("INSERT INTO tbl_person(personname,persondob) VALUES('James Bond','01-Jan-1960') SELECT @@IDENTITY FROM tbl_person")
If Not objTemp Is Nothing Then
Response.Write("ID = " & objTemp.ToString)
When the code is executed it returns the correct value.
What I would like to know is, if many users are using the same function, will it always return the correct ID?
Is ASP.NET and/or SQL Server setup to handle this, or is it down to me to surround the SQL statement with a mutex object?
All input is greatly appreciated.