Click here to Skip to main content
15,915,763 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to add columns in sql server 2005 from windows application made on c#. And the columns name is taken from textbox in my windows form.When i click submit button it should make a new column in sql table.and a sub query will insert a value in that column. I want the code of all this procedure please help me out or guide me how o do this thing..supply with the whole code in C#
Thanx alot
Posted

You need to use Alter command to do this. See
http://www.w3schools.com/sql/sql_alter.asp[^].

This[^] might give you some ideas as well.
 
Share this answer
 
Comments
namaskaaar 21-Aug-11 10:54am    
i know this code by i want to set the name of this column entered in the textbox of my windows application..
thatraja 21-Aug-11 11:19am    
why? any reason?
I think this helps you too.Call it from your application...
SQL
ALTER TABLE <table_name>
(
  ADD <column name=""> <optional constraint>
  [, ... ]
)
 
Share this answer
 
v4
Comments
namaskaaar 21-Aug-11 10:54am    
i know this code by i want to set the name of this column entered in the textbox of my windows application..
I think you can easily add column in your Table
you execute following command

ALTER TABLE <TABLE_NAME> ADD <COLUMN_NAME> <DATA_TYPE> <CONSTRAINT>

example
ALTER TABLE EMP ADD SALARY int
 
Share this answer
 
Comments
namaskaaar 21-Aug-11 10:53am    
i know this code by i want to set the name of this column entered in the textbox of my windows application..
Try to do something like this.
C#
using (SqlConnection connection = new SqlConnection("Your connection string"))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand())
                {
                    command.CommandText = string.Format("ALTER TABLE TABLE_NAME ADD {0} {1}", "[txtColumnName.Text]", "[cmbColumnType.SelectedValue]");
                    command.Connection = connection;
                    command.ExecuteNonQuery();
                }
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900