Results 1 to 4 of 4

Thread: [2005] Add column to SQL database

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    63

    [2005] Add column to SQL database

    I have figured out how to add a column to an existing database in VB code by using a SQL statement like this:

    Code:
    Dim mycmd As New SqlCommand("ALTER TABLE myTable ADD NewCol int", conMain)
    conMain.Open() : mycmd.ExecuteNonQuery() : conMain.Close()


    But shouldn't I be able to do it via VB code? There is an add column function:

    Code:
    mydataset.Tables(0).Columns.Add("NewCol", GetType(Integer))
    It appears to add the column to the database in memory but I can't get it to update the actual database. I've tried various update commands but nothing works.

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

    Re: [2005] Add column to SQL database

    Nope... doesn't work that way... adding a filed to a dataset doesn't add it to the database, nor should it. You have to run the proper DDL commands (ALTER TABLE, etc...) in order to do that.

    -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
    Member
    Join Date
    Sep 2006
    Posts
    63

    Re: [2005] Add column to SQL database

    Thanks for the reply. What is the purpose of the dataset.tables.columns.add function then if you can't really add a column with it?

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

    Re: [2005] Add column to SQL database

    The purpose of adding a column to a DataTable is exactly that: adding a column to a DataTable. How do you think that DataTable's get created when you retrieve data from the database? The schema of the result set is read and a DataTable is created with a matching schema. How is that done? Columns are added to it. How is that done? By calling the Columns.Add method of the DataTable.

    Apart from that, not every DataTable will correspond directly with a table in a database. What if you were to retrieve data into a DataTable and then you wanted to add a column that was the sum of two other columns? How would you do that? The same way.
    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

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