Solved

VB.net SQL Rename a table

Posted on 2013-10-28
3
1,084 Views
Last Modified: 2013-10-29
Hi

What VB.net code would I use to rename an SQL table using
SQL Client style of coding similar to the following:

            Dim myConnection As SqlConnection = New SqlConnection(Globals.ThisAddIn.oRIGHT.lblConnectionString.Text)
            Dim myCommand As SqlCommand

            Dim ra As Integer

            Dim sSQL As String = "ALTER TABLE [" & oTable & "] DROP COLUMN [" & oColumn & "]"


            myConnection.Open()

            myCommand = New SqlCommand(sSQL, myConnection)
            ra = myCommand.ExecuteNonQuery()

            DropColumnSQL = True

            myConnection.Close()
0
Comment
Question by:Murray Brown
[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
3 Comments
 
LVL 6

Assisted Solution

by:ButlerTechnology
ButlerTechnology earned 250 total points
ID: 39605300
You will want to use a stored procedure to rename a table.  Here's an example:
Exec sp_rename Old Name, New Name

Open in new window



Tom
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 250 total points
ID: 39605939
Since it is usually something you do only once, I would not use a stored procedure. And if you are able to create a store procedure, you are also able to rename the table "manually".

Simply use the SQL command provided by ButlerTechnology in your sSQL variable.

Or you might prefer to work with the Microsoft.SqlServer.Management.Smo library, that has been designed specifically to work with the server, while SqlSlient was designed for data access.

Dim srv AsServer srv = New Server("(local)")
Dim db As Database = srv.Databases("AdventureWorks2012");
Dim tb as Table  = New Table(db, "Test Table");
tb.Rename("New Name")

Any way you chose, be aware that it won't work if the user does not have the proper rights on the database.

And also, the rename does not trigger down to whathever was already created to work with the table, such as views, stored procedures, and naturally, external code that already works on the table.
0
 

Author Closing Comment

by:Murray Brown
ID: 39608178
Thanks very much
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

630 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