Microsoft KB Archive/320897

= HOW TO: Retrieve an Identity Value from a Newly Inserted Record from SQL Server by Using Visual C# .NET =

Article ID: 320897

Article Last Modified on 12/26/2003

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 1.1
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft SQL Server 7.0 Standard Edition
 * Microsoft SQL Server 2000 Standard Edition
 * Microsoft SQL Server 2000 64-bit Edition
 * Microsoft Data Engine 1.0

-



This article was previously published under Q320897



For a Microsoft Visual Basic .NET version of this article, see 320141.

IN THIS TASK
SUMMARY
 * Requirements
 * Sample
 * Troubleshooting

REFERENCES



SUMMARY
This step-by-step article explains how to retrieve the identity value when you add a record into a SQL Server table with an identity field.

back to the top

Requirements
This sample uses the Northwind database in SQL Server and retrieves the identity values only for DataTables with no child tables. This technique can also be used with MSDE; however, the Northwind database is not included with MSDE. For information about how to use this procedure with MSDE, see the Troubleshooting section of this article.

A SQL Server identity field is an auto number field for which you can define an incremental value. For this reason, you cannot insert or update a value in this field as long as Identity_ insert is off, which is the default for a SQL Server identity field.

back to the top

Sample
 Start Visual Studio .NET, and then create a new Visual C# Windows Application project:  On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual C# Projects under Project Types, and then click Windows Application under Templates.  Drag a Button onto the form from the Windows Forms toolbox.  At the top of the code window add the following line: using System.Data.SqlClient;   Paste the following code onto the Button's click event: string stConnection = &quot;server=(local);integrated security=sspi;database=northwind&quot;; System.Data.SqlClient.SqlConnection cnNorthwind = new SqlConnection(stConnection); string stSelect = &quot;select EmployeeID,FirstName,LastName from Employees&quot;; System.Data.SqlClient.SqlCommand cmSelect = new SqlCommand(stSelect,cnNorthwind);

string stInsert; stInsert = &quot;Insert into employees (firstName,LastName) values(@FirstName,@LastName);select EmployeeID,FirstName,LastName from Employees where EmployeeID = @@identity &quot;; System.Data.SqlClient.SqlCommand cmInsert = new SqlCommand(stInsert,cnNorthwind); cmInsert.Parameters.Add(new SqlParameter(&quot;@firstName&quot;,System.Data.SqlDbType.VarChar,25,&quot;FirstName&quot;)); cmInsert.Parameters.Add(new SqlParameter(&quot;@LastName&quot;, System.Data.SqlDbType.VarChar,25,&quot;LastName&quot;));

System.Data.SqlClient.SqlDataAdapter daNorthwind = new SqlDataAdapter(cmSelect); daNorthwind.InsertCommand = cmInsert; System.Data.DataSet dsNorthwind = new DataSet; daNorthwind.Fill(dsNorthwind,&quot;Employees&quot;); System.Data.DataRow dr; dr = dsNorthwind.Tables[&quot;Employees&quot;].NewRow; dr[1] = &quot;John&quot;; dr[2] = &quot;Doe&quot;; dsNorthwind.Tables[&quot;Employees&quot;].Rows.Add(dr);

daNorthwind.Update(dsNorthwind,&quot;Employees&quot;); dsNorthwind.AcceptChanges; for (int i=0;i<dsNorthwind.Tables[&quot;Employees&quot;].Rows.Count - 1 ; ++i) {  Console.WriteLine (&quot;EmployeeID: &quot; +    dsNorthwind.Tables[&quot;Employees&quot;].Rows[i][0].ToString); Console.WriteLine (&quot;Employee FirstName: &quot; +   dsNorthwind.Tables[&quot;Employees&quot;].Rows[i][1].ToString); Console.WriteLine (&quot;Employee LastName: &quot; +   dsNorthwind.Tables[&quot;Employees&quot;].Rows[i][2].ToString); }                   </li> Change the connection string to reflect your SQL Server or MSDE information.

</li> Run the application.

</li> Click the Button.

The information for the identity field of the newly inserted record is displayed.</li></ol>

back to the top

Troubleshooting
Before you use this procedure with MSDE, you must use Data Transformation Services (DTS) to import the Northwind database from SQL Server or Microsoft Access.

back to the top

<div class="references_section">