Murray Brown
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_COL UMN_USAGE "
sSQL = sSQL & "WHERE OBJECTPROPERTY(OBJECT_ID(c onstraint_ name), 'IsPrimaryKey') = 1"
sSQL = sSQL & "AND table_name = 'MARA_MBEW'"
Dim connection As New SqlConnection(Globals.This AddIn.oRIG HT.lblConn ectionStri ng.Text)
Dim cmd As New SqlCommand(sSQL, connection)
connection.Open()
Dim oResult As String
oResult = cmd.ExecuteScalar().ToStri ng
MsgBox(oResult)
connection.Close()
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_COL
sSQL = sSQL & "WHERE OBJECTPROPERTY(OBJECT_ID(c
sSQL = sSQL & "AND table_name = 'MARA_MBEW'"
Dim connection As New SqlConnection(Globals.This
Dim cmd As New SqlCommand(sSQL, connection)
connection.Open()
Dim oResult As String
oResult = cmd.ExecuteScalar().ToStri
MsgBox(oResult)
connection.Close()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)).ToStri ng()
MsgBox(rdr.Item(1)).ToStri ng()
End If
Dim rdr As SqlDataReader
rdr = cmd.ExecuteReader
If (rdr.Read) Then
MsgBox(rdr.Item(0)).ToStri
MsgBox(rdr.Item(1)).ToStri
End If
sSQL = "SELECT column_name, OtherFieldName "
.
.
Dim rdr As SqlDataReader
rdr= cmd.ExecuteNonQuery
if (rdr.Read)
MsgBox(rdr.Item(0)).ToStri
MsgBox(rdr.Item(1)).ToStri
End If
connection.Close()
sorry if my air code have some syntax issues. I will try to QC in vs shortly.