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 Get a count of the number of primary keys in a SQL table

Hi

I use the following code to get the primary key for a SQL table. If the table has more than one primary key
how do I get a count of the number of primary keys? Thanks

            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 Phillip Burton
Phillip Burton

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
Avatar of ste5an
ste5an
Flag of Germany 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
Avatar of Murray Brown

ASKER

Thanks for clarifying that a table can only one primary key. Also thanks for the code