none
How to update new columns to database?

    Question

  • Hi all,

     

    I try to use Columns.add(..)  to add a column to a existing table. as following:

     

    ...

    testConnection.Open();

    DataSet ds = new DataSet();

    ds.Tables.Add(animalsTable);

    dataAdapter.Fill(ds, "Animals");

    ds.Tables["Animals"].Columns.Add("ExtendedAccount", typeof(decimal), "AccountNumber * 2");

    ds.AcceptChanges();

    dataAdapter.Update(ds, "Animals");

    testConnection.Close();

     

    I can view dataset ds when debugging, it will add new column which means in memory I successfully add it.

     

    However I cannot update it to Database, after reading help I see Update() only effective on rows,

     

    But I didnot find how to update column, any ideas?

    Tuesday, January 22, 2008 11:01 AM

Answers

  • When you call Update method of DataAdapter new DataColumns are not created in a database tables. Update only updates actual data against existing structures in a database and does not create or extends any objects inside of database. If you need to do it, you would need to analyze structure using your own code and execute ALTER TABLE statement that will modify structure of the table.

     

    Tuesday, January 22, 2008 11:29 AM
    Moderator

All replies

  • When you call Update method of DataAdapter new DataColumns are not created in a database tables. Update only updates actual data against existing structures in a database and does not create or extends any objects inside of database. If you need to do it, you would need to analyze structure using your own code and execute ALTER TABLE statement that will modify structure of the table.

     

    Tuesday, January 22, 2008 11:29 AM
    Moderator
  • Thanks, is there other method using c# to achieve this task? I really hope not using SQL such as ALTER TABLE...

    Wednesday, January 23, 2008 1:40 AM
  • If it is SQL Server, you could use SMO .NET library that allows manipulating objects in SQL Server database, but there is nothing automatic in .NET. You might find some third party tool for it.

     

    Wednesday, January 23, 2008 11:18 AM
    Moderator