|
-
May 13th, 2008, 04:34 AM
#1
Can't Rename ColumnName in and mdb file
I have an mdb file (MS Access). I want to rename a table's column.
I tried this and no exception is thrown, nor it works.
Code:
try
{
string szMDBFilePath ="c:\\temp\\db1.mdb";
string password ="";
this.m_oleDBConnection.Close();
strTable = "Tabella1";
strOldColumnName = "campo1";
strNewColumnName = "newcolname";
this.m_oleDBConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + szMDBFilePath + ";Jet OLEDB:Database Password=" + password + ";";
m_oleDBConnection.Open();
m_oleDBDataAdapter = new OleDbDataAdapter("SELECT * FROM [" + strTable + "]", m_oleDBConnection);
m_DBDataSet = new DataSet();
this.m_oleDBDataAdapter.Fill(m_DBDataSet, "[" + strTable + "]");
System.Data.DataTableCollection dtcConnessioneATabella = m_DBDataSet.Tables;
DataTable dtTabella = dtcConnessioneATabella["[" + strTable + "]"];
this.m_oleDBDataAdapter.FillSchema(dtTabella, SchemaType.Source);//m_DBDataSet, "[" + strTable + "]");
dtTabella.Columns[strOldColumnName].ColumnName = strNewColumnName;
this.m_oleDBDataAdapter.Update(dtTabella);
return NO_ERROR;
}
catch (Exception e)
{
m_strExceptionMessage = e.Message;
return ERROR;
}
How am I supposed to implement it?
Mr. Burns
-
May 13th, 2008, 05:32 AM
#2
Re: Can't Rename ColumnName in and mdb file
You can't rename a column name in this way. You have to add a new column with the new name. Then you have to copy the data from the old column to the new one. Finally you have to delete the old column.
Useful or not? Rate my posting. Thanks.
-
May 13th, 2008, 05:54 AM
#3
Re: Can't Rename ColumnName in and mdb file
There ought to be a way to rename it!
I can do what you say, but I want to rename it. I found someone that did it, using ADO, but I can't see why can't I do it using OleDB.
Mr. Burns
-
May 13th, 2008, 06:24 AM
#4
Re: Can't Rename ColumnName in and mdb file
OleDB does not provide you any method that renames a column. However, using old ADOX technology you can rename a column in the database.
Look at this thread
http://www.devnewsgroups.net/group/m...opic50261.aspx
-
May 13th, 2008, 07:09 AM
#5
Re: Can't Rename ColumnName in and mdb file
it seems strange to me that I can change DataTable.Columns[..].ColumnName property without throwing any exceptions (I can see in the debugger's watch window that the property changes correctly).
The m_oleDBDataAdapter.Update(..) does not throw any exception either.
Are you 100% sure that OleDB does not allow to do it?
I wouldn't like to include other dlls to do it with ADOX....
Mr. Burns
-
May 13th, 2008, 08:22 AM
#6
Re: Can't Rename ColumnName in and mdb file
DataTable is in memory representation of the database. You can change the names of the columns in the datatable, however that will not reflect in the database itself. Did you check if the column name in the database got changed or not?
-
May 13th, 2008, 09:05 AM
#7
Re: Can't Rename ColumnName in and mdb file
 Originally Posted by MontgomeryBurns
it seems strange to me that I can change DataTable.Columns[..].ColumnName property without throwing any exceptions
You dont seem to have realised that a DataTable is a client side data storage container, completely independent of a database. You can call the column names anything you like, they dont ahve to match the db, they dont have to be the same type.. infact, you dont even need to have a db.
DataTable is NOT a database, not THE database, and the fact that the wizard names all the columns like it finds in the database is purely for reasons of a) ease for Microsoft to code it taht way and b) you to recognise what local column represents what remote column
-
May 14th, 2008, 02:16 AM
#8
Re: Can't Rename ColumnName in and mdb file
ok with that cjard, I do realize that.
But why OleDbDataAdapter.Update() does not throw exceptions, either? That should interact with the real db, shouldn't it?
Mr. Burns
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|