Murray Brown
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_COL UMN_USAGE "
sSQL = sSQL & "WHERE OBJECTPROPERTY(OBJECT_ID(c onstraint_ name), 'IsPrimaryKey') = 1"
sSQL = sSQL & "AND table_name = '" & oTableName & "'"
Dim connection As New SqlConnection(Globals.This AddIn.oRIG HT.lblConn ectionStri ng.Text)
Dim cmd As New SqlCommand(sSQL, connection)
connection.Open()
Get_Primary_Key = cmd.ExecuteScalar().ToStri ng
connection.Close()
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_COL
sSQL = sSQL & "WHERE OBJECTPROPERTY(OBJECT_ID(c
sSQL = sSQL & "AND table_name = '" & oTableName & "'"
Dim connection As New SqlConnection(Globals.This
Dim cmd As New SqlCommand(sSQL, connection)
connection.Open()
Get_Primary_Key = cmd.ExecuteScalar().ToStri
connection.Close()
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER