Solved

Add Column to Database

Posted on 2014-04-01
5
952 Views
Last Modified: 2014-04-05
I have a Visual Basic .Net Winforms project which has a database with a table and somehow it uses SQL Compact 3.5. The form has:

DataSet1
BindingSource
TableAdapter
TableAdapterManager
BindingNavigator

I want to add a column to the table in the database so I can store another piece of information.

I would like to deploy a new version of the program, and the program will update the user's existing *.sdf database file and add a new column to it.

Can't figure out how to do that from Visual Basic VB.Net

I have added the column to my DataSet1.xsd

and I added the column to Server Explorer > Data Connections > Tables > mytable > Columns

Program runs fine, uses default value for the new column, updates file *.sdf database file with other information. But the database file itself still doesn't have the new column added to it. That's the part I need to figure out a way to do.

Somehow I need to get my VB.Net program to issue a "SQL ALTER table ADD newcolumn" statement to add a column to the database file.
0
Comment
Question by:deleyd
  • 2
  • 2
5 Comments
 
LVL 39

Expert Comment

by:lcohan
ID: 39970042
"Add Column to Database"

You can add a Table to a Database
OR
You can add a Column to a Table

but not to "Add Column to Database"
0
 

Author Comment

by:deleyd
ID: 39970876
Add column to table in database.
0
 
LVL 20

Assisted Solution

by:ElrondCT
ElrondCT earned 500 total points
ID: 39972357
I use an OleDbCommand:

    	Friend WithEvents odbconn As System.Data.OleDb.OleDbConnection
	odbconn = New System.Data.OleDb.OleDbConnection
        odbconn.ConnectionString = "Provider=SQLOLEDB;Data Source=MySQLServer;User ID=MyID;Persist Security Info=True;Password=MyPassword;Initial Catalog=MyDatabase"
        odbconn.Open()
        Dim cmd As OleDbCommand
        cmd = New OleDbCommand("ALTER TABLE Table1 ADD NewField Varchar(50);", odbconn)
        cmd.ExecuteNonQuery()
        odbconn.Close()

Open in new window

0
 

Author Comment

by:deleyd
ID: 39972688
Thank you I was able to get it to work (with a slight modification switching to a SqlCeConnection)
        Dim dbconn As SqlCeConnection = Me.CFacilityTableAdapter.Connection
        dbconn.Open()
        Dim cmd As SqlCeCommand
        cmd = New SqlCeCommand("ALTER TABLE CFacility ADD COLUMN Sort Integer NOT NULL;", dbconn)
        cmd.ExecuteNonQuery()
        dbconn.Close()

Open in new window

Is it possible to add auto incrementing? I tried adding AUTO_INCREMENT, AUTOINCREMENT, AUTOINC, but it just complains it doesn't recognize that.

Also, is it possible to test to determine if the table already has a column named "Sort"?
0
 
LVL 20

Accepted Solution

by:
ElrondCT earned 500 total points
ID: 39972891
cmd = New SqlCeCommand("ALTER TABLE CFacility ADD COLUMN Sort Integer IDENTITY(1,1) NOT NULL;", dbconn)

The first 1 for Identity indicates that the numbering starts with 1; the second 1 indicates that you increment by one. Hint for finding things like this: use SQL Server Management Studio to create a script that will recreate the table (if you already have a table designed).

You've got two choices to find if a column already exists: get the schema and look through it, or just try to add a column inside TRY ... CATCH and trap the exception that happens if you try to add a duplicate column.

To get the schema for the table (using OleDb; you'll have to adjust for SqlCeConnection):

Dim schemaTable As DataTable = _
                          odbconn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, _
                          New Object() {Nothing, Nothing, "MyTable", Nothing})

schemaTable.Rows(x)(3) is the column that has the column name, so you can loop through the rows to see if you get a match.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with adding DLL file in Windows project 20 32
Passing Parameter to Stored Procedure 4 24
T-SQL Default value in Select? 5 27
SQL Query assistance 16 27
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
I have a large data set and a SSIS package. How can I load this file in multi threading?
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
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

831 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