• Javascript
  • Python
  • Go
Tags: .net ado.net

Retrieving IDENTITY column value on insert with SqlCommandBuilder (without Stored Proc)

As a programmer, you may have encountered the need to retrieve the value of an IDENTITY column after inserting a new record into a database ...

As a programmer, you may have encountered the need to retrieve the value of an IDENTITY column after inserting a new record into a database table. This value is crucial for maintaining data integrity and for further manipulation of the inserted data. One common approach to retrieve this value is by using a stored procedure. However, there is another way to achieve this without the use of a stored procedure, by utilizing the SqlCommandBuilder class.

Before we delve into the details of this approach, let's first understand what an IDENTITY column is. An IDENTITY column is a special type of column in a database table that automatically generates a unique numeric value for each new row inserted into the table. This value is typically used as the primary key for the table.

Now, let's assume we have a table called "Employees" with the following structure:

| ID (IDENTITY) | First Name | Last Name | Age |

| ------------- | ---------- | --------- | --- |

| 1 | John | Smith | 28 |

| 2 | Jane | Doe | 32 |

| ... | ... | ... | ... |

Our goal is to insert a new record into this table and retrieve the value of the IDENTITY column for the newly inserted record.

To achieve this, we will first create a new instance of the SqlCommandBuilder class, passing in the SqlCommand object that we will use to insert the new record. This class is responsible for automatically generating SQL commands based on the changes made to the associated SqlCommand object.

Next, we will use the AddWithValue method of the SqlCommand object to add parameters for the values we want to insert into the table. For example, to insert a new employee with the first name "Mark", last name "Johnson", and age "25", we would use the following code:

```

SqlCommand cmd = new SqlCommand("INSERT INTO Employees (FirstName, LastName, Age) VALUES (@firstName, @lastName, @age)");

cmd.Parameters.AddWithValue("@firstName", "Mark");

cmd.Parameters.AddWithValue("@lastName", "Johnson");

cmd.Parameters.AddWithValue("@age", 25);

```

Now, here's the crucial part. Before executing the SqlCommand, we need to set the SqlCommand's UpdatedRowSource property to UpdateRowSource.OutputParameters. This tells the SqlCommand to return the values of any output parameters after the command has been executed.

```

cmd.UpdatedRowSource = UpdateRowSource.OutputParameters;

```

Finally, we can execute the command and retrieve the IDENTITY value by accessing the Value property of the output parameter with the name "@IDENTITY". Here's the complete code:

```

SqlCommand cmd = new SqlCommand("INSERT INTO Employees (FirstName, LastName, Age) VALUES (@firstName, @lastName, @age)");

cmd.Parameters.AddWithValue("@firstName", "Mark");

cmd.Parameters.AddWithValue("@lastName", "Johnson");

cmd.Parameters.AddWithValue("@age", 25);

cmd.UpdatedRowSource = UpdateRowSource.OutputParameters;

cmd.ExecuteNonQuery();

int newID = Convert.ToInt32(cmd.Parameters["@IDENTITY"].Value);

```

And that's it! We have successfully inserted a new record into the Employees table and retrieved the value of the IDENTITY column without using a stored procedure.

In conclusion, the SqlCommandBuilder class provides a convenient and efficient way to insert data into a database table and retrieve the value of an IDENTITY column. This approach eliminates the need for creating and maintaining stored procedures, making our code more manageable and maintainable. So the next time you need to retrieve the value of an IDENTITY column, remember to give the SqlCommandBuilder class a try.

Related Articles