Solved

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

Posted on 2014-12-23
3
232 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
[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
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

730 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