我想使用C#以编程方式向SQL表中添加列。 这是我的代码。
SqlConnection con = new SqlConnection(conString);
con.Open();
string str = "ALTER TABLE [dbo].[tblQuiz] ADD '" + txtQuizNo.Text.ToString() +"' Varchar(50) NULL ";
SqlCommand com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
When I execute the program it say's that Incorrect syntax near the column nameenter code here
.
Is it possible to add column to the sql database programmatically?
SQL Server处理SQL命令。它不在乎它们来自何处。
另请注意,您的错误消息CLEARLY似乎说明了这一点:
不正确的语法表示完全正确。
您为什么还要考虑:
这是一个简单的初学者调试提示:下次SQL命令像这样失败时,将其记录下来或使用调试器获取FINISHED字符串。将其复制/粘贴到SSMS中,然后查看是否在其中运行。如果您尝试过,整个问题将是没有意义的-因为您会清楚地看到它与C#不相关,因为您在SSMS中遇到相同的错误。