Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 12th, 2003, 01:51 PM
Authorized User
 
Join Date: Jun 2003
Posts: 15
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to james_sellwood
Default Renaming a Column/Field in Access Table

Hi,

 I am trying to rename a column/field in an Access Database Table from within an ASP.NET page. I am aware that it would be much simpler to rename the column/field from within Access itself but the database is in use and to download it would cause unneccessary down time for what I think should be possible in code. (I am aware of the problems with renaming columns on tables that are in use, that is not a problem as this is a table that is not yet in use.)

I have come accross these bits of SQL / Access code:

Code:
ALTER TABLE tableName RENAME COLUMN oldColumnName TO newColumnName
and
Code:
Tabledefs('tableName').Fields('oldCoumnName').Name = 'newColumnName'
These may work in some way but from within my ASP.NET page I am unable to actually modify the table. I have been trying to use myCommand.ExecuteNonQuery() with the above code but I could not get this to work. I also tried using the above code as part of an OLEDB Update Command but have been unsuccessful.

I would appreciate any help that anyone can provide in pointing me in the right direction.

Thanks

--------------------
:) James Sellwood :)
--------------------
__________________
--------------------
James Sellwood
--------------------
 
Old February 3rd, 2005, 05:34 PM
Registered User
 
Join Date: Feb 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this:
  Dim conn as New ADODB.Connection
  Dim ctlg as New ADOX.Catalog

  conn.Open "<your params>"
  ctlg.ActiveConnection = conn

  Dim tbl_name As String
  Dim old_col_name As String
  Dim new_col_name As String

  tbl_name = "SOMETABLE"
  old_col_name = "SOME OLD COLUMN"
  new_col_name = "SOME_NEW_COLUMN"

  ctlg.Tables(tbl_name).Columns(old_col_name).Name = new_col_name

  conn.close
  Set conn = nothing

Hope this helps
- webtekkie74

 
Old February 4th, 2005, 07:39 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

I would not recommend you to use an ASP.NET app,you can simply do it with a simple windows app instead,anyway for changing column name use this SP in master database,
Code:
EXECUTE sp_rename N'TableName.OldColumnName', N'NewColumnName', 'COLUMN'
as you see it's better to use a simple windows app instead because of security risks,
Quote:
quote:Originally posted by james_sellwood
 I also tried using the above code as part of an OLEDB Update Command but have been unsuccessful.
no,it's just for simple Update sql statements.

_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
 
Old February 4th, 2005, 08:45 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

I forgot to post its code,
Code:
string strConnection=""//according to your DB connection
SqlConnection objConnection=new SqlConnection(strConnection);
SqlCommand objCommand=new SqlCommand("sp_rename",objConnection);
objCommand.CommandType=System.Data.CommandType.StoredProcedure;
objCommand.Parameters.Add("@objname","[table_name].[current_name]");
objCommand.Parameters.Add("@newname","[new_name]");
objCommand.Parameters.Add("@objtype","COLUMN");
objConnection.Open();
objCommand.ExecuteNonQuery();
objConnection.Close();
Attention:all of that was for SqlServer/MSDE

_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Renaming fields in access table using VBA imaley Access VBA 13 August 13th, 2009 02:22 PM
To get text of Button Field column in GridView nitinp ASP.NET 2.0 Basics 1 November 3rd, 2006 10:57 AM
Help with field expressions in access table CStewart Access 8 October 25th, 2005 03:32 PM
Renaming a table ... ERROR ?! SKE Classic ASP Databases 2 May 16th, 2005 06:04 AM
how to make column of table 1 = to column of table gilgalbiblewheel Classic ASP Databases 4 October 11th, 2004 11:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.