Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

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
0
Murray Brown
Asked:
Murray Brown
  • 2
1 Solution
 
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
 
Jesus RodriguezIT 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
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Great. Thanks very much
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now