0

I need to update database after adding/changing column in DataColumnCollection in DataTable. How can I do this?

I used this code to add new column:

dataTable.Columns.Add("newColumn", typeof (int));

This code works for changed rows in dataTable, but not for changed Columns (it does nothing).

SqlDataAdapter adapter  = new SqlDataAdapter(cmdText, _connection);
SqlCommandBuilder cb = new SqlCommandBuilder(adapter);
adapter.Update(dataTable);

In cmdText is select command used to get dataTable.

Pyro2266
  • 310
  • 2
  • 5
  • 18
  • You can't change the schema of your database changing the schema of the in-memory datatable. You need to use the appropriate SQL command for your database. IE: search for T-SQL ALTER TABLE – Steve May 01 '16 at 22:01
  • @Steve thx for reply. So there is no way to change schema of database using DataTable? I use now dataTable.Columns as dataSource for DataGridView and it's confortable to change it this way :). – Pyro2266 May 01 '16 at 22:05
  • No and I find a bit weird that you need to change the schema of your _database table_ while running your app. Usually these changes happen when the requirements change. What kind of application needs to change its database schema -permanently- at runtime? – Steve May 01 '16 at 22:14
  • Nothing big, just school project. It should be something like database manager. – Pyro2266 May 01 '16 at 22:16
  • 1
    I see, but still no way to do it using a DataTable object. You need to use the proper SQL command – Steve May 01 '16 at 22:21

1 Answers1

0

So it looks like there is no way to do this using dataTable. You have to use SQL command (ALTER TABLE ...).

Pyro2266
  • 310
  • 2
  • 5
  • 18