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 2 key olumn names in a SQL table

Hi
I am using the following code to get the primary key of a table. This table has a second key column.
How do I get the name of that

        Dim sSQL As String
        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 = 'MARA_MBEW'"

        Dim connection As New SqlConnection(Globals.ThisAddIn.oRIGHT.lblConnectionString.Text)
        Dim cmd As New SqlCommand(sSQL, connection)
        connection.Open()
        Dim oResult As String
        oResult = cmd.ExecuteScalar().ToString
        MsgBox(oResult)
        connection.Close()
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
Dim sSQL As String
        sSQL = "SELECT column_name, OtherFieldName "
.
.


Dim rdr As SqlDataReader
        rdr= cmd.ExecuteNonQuery
        if (rdr.Read)
          MsgBox(rdr.Item(0)).ToString
          MsgBox(rdr.Item(1)).ToString
      End If
        connection.Close()

sorry if my air code have some syntax issues. I will try to QC in vs shortly.
Avatar of Murray Brown

ASKER

Thanks. The questions were slightly different but the code achieves both. Thanks very much
In case this QCed version was needed:

 Dim rdr As SqlDataReader
        rdr = cmd.ExecuteReader
        If (rdr.Read) Then
            MsgBox(rdr.Item(0)).ToString()
            MsgBox(rdr.Item(1)).ToString()
        End If