Hi,
I hope someone can help me.
I working on a visual studio project. I would like add a new column to my existing sql table (tbl_names). The column name is a string (newCol).
I tried two different ways:
con.Open();
SqlCommand inc = new SqlCommand("ALTER TABLE tbl_names ADD " + newCol + " varchar(3)", con);
inc.ExecuteNonQuery();
con.Close();
And:
con.Open();
var incString = string.Format("ALTER TABLE tbl_names ADD " + newCol + " varchar(3)");
SqlCommand inc = new SqlCommand(incString, con);
con.Close();
Unfortunately I got errors when I tried them.
How can I add a new column if the column's name is a string?
I am new to c# so help would be great.
Thanks,
Timur
Nilesh JadavRank #109 · 20.3K reputationPosted Oct 1, 2015, 10:32 PM EDT
you forget to add column.
Shridhar SharmaRank #440 · 5.7K reputationPosted Oct 1, 2015, 1:37 PM EDT
Timur Lenk18 reputationPosted Oct 2, 2015, 6:35 AM EDT
Nilesh JadavRank #109 · 20.3K reputationPosted Oct 2, 2015, 7:14 AM EDT
Timur Lenk18 reputationPosted Oct 2, 2015, 10:11 AM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts