Solved

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

Posted on 2014-12-23
3
195 Views
Last Modified: 2014-12-23
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
Comment
Question by:murbro
  • 2
3 Comments
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 40515063
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
 
LVL 13

Accepted Solution

by:
Jesus Rodriguez earned 500 total points
ID: 40515065
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
 

Author Closing Comment

by:murbro
ID: 40515133
Great. Thanks very much
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now