Select @@Identity : Sql Data Type « Database ADO.net « VB.Net






Select @@Identity

  

Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports System.Data


Public Class Form1
    Inherits System.Windows.Forms.Form
    Public Shared Sub Main()
        Dim MyIdentityValue As Integer
        Dim objConnection As New SqlClient.SqlConnection("server=.;database=pubs;trusted_connection=true")
        Dim objCommand As New SqlClient.SqlCommand("INSERT INTO jobs (desc, minV, maxV) VALUES('desc', 50, 100)")
        objConnection.Open()
        objCommand.Connection = objConnection
        If objCommand.ExecuteNonQuery > 0 = True Then
            Dim objIdentifier As New SqlClient.SqlCommand("Select @@Identity", objConnection)
            Try
                MyIdentityValue = objIdentifier.ExecuteScalar
            Catch
                MyIdentityValue = 0
            End Try
        End If
        objConnection.Close()
        System.Console.WriteLine(MyIdentityValue)
    End Sub
End Class

   
    
  








Related examples in the same category

1.Type safe data from SqlDataReaderType safe data from SqlDataReader
2.Sql data type: integer, date and decimalSql data type: integer, date and decimal