CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2003
    Location
    Springfield
    Posts
    190

    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

  2. #2
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    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.

  3. #3
    Join Date
    Jul 2003
    Location
    Springfield
    Posts
    190

    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

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    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

  5. #5
    Join Date
    Jul 2003
    Location
    Springfield
    Posts
    190

    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

  6. #6
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    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?

  7. #7
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Can't Rename ColumnName in and mdb file

    Quote 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
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  8. #8
    Join Date
    Jul 2003
    Location
    Springfield
    Posts
    190

    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
  •  





Click Here to Expand Forum to Full Width

Featured