Retrieve the Unique ID for a New Record

Problem

You need to insert a record and retrieve its unique ID number, which is generated by the database.

Solution

Use a database stored procedure that returns this information in an output parameter.

Discussion

Many database tables use an ID or a GUID (globally unique identifier) field to uniquely identify records. This field is generated by the database when the record is created, not by the client. However, the client often needs to show this information to the user, and it might serve as a registration number or a confirmation number. To make it easy for the client to retrieve this information, it’s recommended that you use a stored procedure that explicitly returns this information.

For example, the Northwind ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.