Solved

VB.net Get 2 key olumn names in a SQL table

Posted on 2014-11-10
4
144 Views
Last Modified: 2014-11-10
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()
0
Comment
Question by:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 34

Accepted Solution

by:
ste5an earned 500 total points
ID: 40432496
Looks like the same question as in VB.net Get a count of the number of primary keys in a SQL table.  

So same answer here:

USE AdventureWorks2012;

SELECT  KC.name ,
        C.name 
FROM    sys.schemas S
        INNER JOIN sys.tables T ON T.schema_id = S.schema_id
        INNER JOIN sys.key_constraints KC ON KC.parent_object_id = T.object_id
        INNER JOIN sys.index_columns IC ON IC.object_id = T.object_id
                                           AND IC.index_id = KC.unique_index_id
        INNER JOIN sys.columns C ON C.object_id = T.object_id
                                    AND C.column_id = IC.column_id
WHERE   S.name = 'Production'
        AND T.name = 'ProductInventory'
	AND KC.type = 'PK';

Open in new window


btw, the alternative would be using SQL Server Management Objects (SMO) Programming Guide.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40432507
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.
0
 

Author Closing Comment

by:Murray Brown
ID: 40432521
Thanks. The questions were slightly different but the code achieves both. Thanks very much
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40432580
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
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question