Solved

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

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

734 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