views:

341

answers:

1

I know this might be a bit awkward but I am trying to modify the order of certain columns in a MS Access database in C# with OLE DB. How can I commit a certain change in the order of the columns of a datatable in a dataset? If that is not possible, how can I reorder columns of database table by using a dataset?

Here is a sample of what I have (in C#):

command.Connection = conn;
command.CommandText = tableName;
command.CommandType = CommandType.TableDirect;
adapter = new OleDbDataAdapter(command);
dataset = new DataSet(tableName);
adapter.Fill(dataset, tableName);
dataset.Tables[0].Columns[dataset.Tables[0].Columns.Count-1].SetOrdinal(CB_PositionCol.SelectedIndex);
dataset.Tables[0].AcceptChanges();

The AcceptChanges does not seem to work for what I wish to do since I believe it only commits changes in DataRows...

Thank you for any help!

+1  A: 

Use an ALTER TABLE statement with an OleDbCommand. DataSet, DataTable and DataTableAdapters are meant to be transparent to the underlying structure of the data. You can actually use Table Adapters to transform data from two different structures.

http://www.functionx.com/vbnet/oledb/Lesson02.htm

Matthew Whited
So basically I would have to drop every column from the position I want to insert to the last column and add new columns for the ones I removed?
Partial
Unfortunately, you could also create a new table. Transfer the data, drop the old table, alter the new table back to the old tables name and then compact/repair... fun but it would work. Why do you need the columns reordered (this is pretty much what happens for your "transparently" when you reorder the columns from the user interface. Minus the compact/repair)
Matthew Whited
If you want to view the columns in a particular way you can just change the order in your SELECT statement, DataView, or allow column reordering in your UI.
Matthew Whited
I need to add columns dynamically from my application to a MS Access table and I want the users to be able to insert in the position they want. Also, I want the order of column in MS Access and my application to be the same. I do not want the users of the application to have to select the order of the columns every time they open the application.
Partial
You could persist the order of the columns of the view separately from the data storage (looser coupling) and allow this to work independently. It's either that or you will be working within the limits of ADO.Net and might have to jump out to COM interop to try and reorder the data in a more consistent manner (I'm not 100% sure that the COM DDL stuff won't just drop and recreate the table anyway)
Matthew Whited
Anyway, I am having some problems with adding the rows once the table is recreated... it tells me something about that the query is a DDL query and that it cannot be use as a row source... :S
Partial
You will need to run the ALTER/CREATE commands as ExecuteNonQuery instead of using the Fill/Update on the DataAdapter
Matthew Whited
I am doing that now! Thank you.
Partial
I must admit it is not really pretty but it works lol :D
Partial
Glad it helped. Don't forget that you risk data corruption on Access databases when moving big datasets around. You might want to create a backup before you do any heavy lifting.
Matthew Whited