Results 1 to 8 of 8

Thread: Add New Column to Database by using codes

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    3

    Add New Column to Database by using codes

    Hi all,

    I'm wondering is it possible to create a new column to an existing database using vb .net 2008 express and SQL CE version?

    I've tried with these codes below with no success
    Code:
            If TextBox1.Text = "" Then
                MsgBox("Enter Column Name", MsgBoxStyle.Question, "Add Column")
            Else
                Dim str_addcolumn As String
                Dim com_addcolumn As New SqlServerCe.SqlCeCommand
                Dim adp_addcolumn As New SqlServerCe.SqlCeDataAdapter
                Dim con_addcolumn As New SqlServerCe.SqlCeConnection
    
                con_addcolumn.ConnectionString = My.Settings.CBDesignProgramConnectionString
                str_addcolumn = "ALTER TABLE GeneralNF ADD " & TextBox1.Text & " NVARCHAR(100)"
                com_addcolumn = con_addcolumn.CreateCommand
                com_addcolumn.CommandText = str_addcolumn
                con_addcolumn.Open()
                
                Try
                    com_addcolumn.ExecuteNonQuery()
                    con_addcolumn.Close()
                Catch ex As Exception
                    MsgBox(ex.Message)
                    con_addcolumn.Close()
                End Try
            End If
    If there is something I did wrong please correct me.

    Thanks in advance.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Add New Column to Database by using codes

    How do you know it's not working? Do you get errors or something? Or what?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    3

    Re: Add New Column to Database by using codes

    Quote Originally Posted by techgnome
    How do you know it's not working? Do you get errors or something? Or what?

    -tg
    Thanks for your prompt reply.

    Basically after those codes I have a GridView Refresh

    Code:
    ModifyGeneralNF.Show()        
    ModifyGeneralNF.TableAdapterManager1.UpdateAll(ModifyGeneralNF.CBDesignProgramDataSet1)
    ModifyGeneralNF.GeneralNFDataGridView.Refresh()
    The Update was not shown. Also I check the database data also showed no update.

    Not too sure whats wrong or something wrong with my codes?

    Thanks again

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Add New Column to Database by using codes

    Adding new columns to your database tables doesn't magically change anything about your TableAdapters. The TableAdapters were created during development based on the schema of the database you were connected to at that time. They are classes that are compiled into your application.

    If you want to be able to change the database schema at run time and have that reflected in your application then you can't use a typed DataSet. You're going to have to use an untyped DataSet and DataAdapters insteda of TableAdapters. That way the DataTable schema can be created at run time instead of pre-ordained at design time.

    That said, adding columns to a database is something that you should never do lightly. It's a major change that is usually accompanied by a recompilation of the application because a different data schema almost always means different logic.

    You should explain to us what this column represents and why you're adding it because I think it's 99% certain that your database structure is quite wrong and you shouldn't be adding a column at all.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    3

    [Resolved] Add New Column to Database by using codes

    Quote Originally Posted by jmcilhinney
    Adding new columns to your database tables doesn't magically change anything about your TableAdapters. The TableAdapters were created during development based on the schema of the database you were connected to at that time. They are classes that are compiled into your application.

    If you want to be able to change the database schema at run time and have that reflected in your application then you can't use a typed DataSet. You're going to have to use an untyped DataSet and DataAdapters insteda of TableAdapters. That way the DataTable schema can be created at run time instead of pre-ordained at design time.

    That said, adding columns to a database is something that you should never do lightly. It's a major change that is usually accompanied by a recompilation of the application because a different data schema almost always means different logic.

    You should explain to us what this column represents and why you're adding it because I think it's 99% certain that your database structure is quite wrong and you shouldn't be adding a column at all.
    Ok thanks for the info..ill let the customers know.

  6. #6
    New Member
    Join Date
    Oct 2008
    Posts
    8

    Re: Add New Column to Database by using codes

    I try to add column too. The problem that appears is in DataSet Updating.
    I use this code:


    Code:
      Ds.Tables("MyTableName").Columns.Add("NewColumnName")
            Adapter.Update(Ds, "MyTableName")
    This code add new column, but when the program restarts new column despair.
    Database is made in Access.

    Suggestions???

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Add New Column to Database by using codes

    Quote Originally Posted by dozd
    I try to add column too. The problem that appears is in DataSet Updating.
    I use this code:


    Code:
      Ds.Tables("MyTableName").Columns.Add("NewColumnName")
            Adapter.Update(Ds, "MyTableName")
    This code add new column, but when the program restarts new column despair.
    Database is made in Access.

    Suggestions???
    Adding a column to a DataTable has no effect on the database whatsoever. It was never intended to. If you want to add a column to a table in a database then you have to execute an ALTER TABLE statement, as klover has shown in the first post.

    I reiterate though, adding new columns to a database is NOT something that should be happening often. It is a MAJOR change to an application and would usually be accompanied by a new version of the application itself. If you feel you need to add a new column to a database table during the normal course of your application's execution then chances are that your database is incorrectly designed. The number of rows in a table can change often but the number of columns should almost never change.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    New Member
    Join Date
    Oct 2008
    Posts
    8

    Re: Add New Column to Database by using codes

    Thank you very much for help jmcilhinney
    Thanks for uderstanding too

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width