Results 1 to 1 of 1

Thread: What's the identity of the row I just inserted?

  1. #1

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    What's the identity of the row I just inserted?

    I was stumbling around the net and ran into this article. With a couple slight alterations, it can be used with ADO.Net:

    The example uses Orders in the Northwind database (you have to change OrderID to an identity field in order for it to work).
    VB Code:
    1. Dim strConn As String = "server=server;database=Northwind;"
    2.         Dim strSQL As String = "SET NOCOUNT ON; INSERT INTO Orders(CustomerID) VALUES ('123456'); SELECT IdentityInsert=@@IDENTITY; SET NOCOUNT OFF"
    3.         Dim sqlConn As New SqlClient.SqlConnection(strConn)
    4.         Dim sqlCmd As New SqlClient.SqlCommand(strSQL, sqlConn)
    5.  
    6.         Dim intIdentity As Integer
    7.         sqlConn.Open()
    8.         intIdentity = Convert.ToInt32(sqlCmd.ExecuteScalar)
    9.         sqlConn.Close()
    10.  
    11.         MessageBox.Show("New records identity: " & intIdentity)
    Last edited by sevenhalo; Aug 11th, 2006 at 12:50 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width