Solved

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

Posted on 2014-11-10
4
143 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:murbro
[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 33

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:murbro
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net error: An exception occurred during a WebClient request 6 42
sql query 5 44
Regarding Disk IO 3 50
"lblTime is not declared" 3 38
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

697 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