hello there
i want to create a column in an existing table of my database using C# i mean in my window form 1 textbox and one button is there
i enter some text and clik on the button
then i creates a column named as i write in textbox
my database is sql server database .mdf file
help me out please
Loading


Satyapriya NayakRank #47 · 31.5K reputationPosted Jul 14, 2012, 3:58 AM EDT
Kunal code is working fine
using System.Data.SqlClient;
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
string str;
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "ALTER TABLE [dbo].[Test] ADD " + TextBox1.Text.ToString() + " Varchar(50) NULL ";
com = new SqlCommand(str,con);
com.ExecuteNonQuery();
con.Close();
}
Please accept kunal's answer
Thanks
praveen maurya103 reputationPosted Jul 14, 2012, 5:49 AM EDT
i am using the following code
using System.Data.SqlClient;
System.Data.SqlClient.SqlConnection cn;
{ (button click event)
}
and it shows error that is
Incorrect syntax near 'addexfvarchar'
where exf is the name i am entering in my textbox for new table
Satyapriya NayakRank #47 · 31.5K reputationPosted Jul 14, 2012, 6:03 AM EDT
string str = "alter table [dbo].new add" + textBox1.Text.ToString() + "varchar(50) null";
Change it to
str = "ALTER TABLE [dbo].[new] ADD " + TextBox1.Text.ToString() + " Varchar(50) NULL ";
Note
1.put space between add and " .
2.put space between " and varchar(50).
3.change [dbo].new to [dbo].[new].
Thanks
shivkumar patil23 reputationPosted Feb 25, 2016, 8:59 PM EST
i want to create a column in an existing table of my database using C# i mean in my window form 2 textbox and one button is there
i enter some text and click on the button
then i creates a 2 column named as i write in 2 textbox
my database is sql server database .mdf file
help me out please
Raja TRank #685 · 3.5K reputationPosted Feb 25, 2016, 9:30 PM EST
Kunal VaishyaRank #990 · 2K reputationPosted Jul 14, 2012, 1:34 AM EDT
Set Command String in SqlCommand and Execute Command
ALTER TABLE [dbo].[DatabaseName] ADD ColumnName Varchar(50) NULL
ALTER TABLE [dbo].[Nothwind] ADD "+ textbox1.Text.ToString() +" Varchar(50) NULL
just do it column will created