Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VB.net SQL Check if Primary key is an identity column

Hi

I use the following VB.net code to check if a column is a primary key.
How do I find if it is an identity column
For example "[ID] bigint identity(1,1) primary key" is an identity column
and "[ID] bigint primary key" is not an identity column


               sSQL = "SELECT column_name "
                sSQL = sSQL & "FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE "
                sSQL = sSQL & "WHERE OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1"
                sSQL = sSQL & "AND table_name = '" & oTableName & "'"

                Dim connection As New SqlConnection(Globals.ThisAddIn.oRIGHT.lblConnectionString.Text)
                Dim cmd As New SqlCommand(sSQL, connection)
                connection.Open()
                Get_Primary_Key = cmd.ExecuteScalar().ToString
                connection.Close()
SOLUTION
Avatar of Om Prakash
Om Prakash
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murray Brown

ASKER

Thanks very much