VB.net SQL set three columns as the primary key in am existing table

Hi

What VB.net code would I use to add a composite primary key  to an existing SQL table, which
includes three of the columns

Thanks
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
 
Jesus RodriguezConnect With a Mentor IT ManagerCommented:
On VB.NET
        Dim cmd As New SqlCommand
        Dim Cnx As SqlConnection = New SqlConnection("Your connection  String")
        With cmd
            .Connection = Cnx
            .CommandText = "ALTER TABLE dbo.Your_Table ADD CONSTRAINT PK_Your_Table PRIMARY KEY (COL1, COL2,COL3)"
            .CommandType = CommandType.Text
            Cnx.Open()
            Try
                cmd.ExecuteNonQuery()
            Catch ex As Exception

            End Try
            Cnx.Close()
        End With

Open in new window

0
 
Jesus RodriguezIT ManagerCommented:
You can alter your table and if you now your columns is only running a query like

ALTER TABLE dbo.Your_Table
ADD CONSTRAINT PK_Your_Table 
PRIMARY KEY (COL1, COL2) 

Open in new window

0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Great. Thanks very much
0
All Courses

From novice to tech pro — start learning today.