[英]I am trying to ALTER TABLE ADD COLUMN Using C#
I would like to add column to the SQL table programmatically using C#.我想使用 C# 以编程方式将列添加到 SQL 表中。
This is my code.这是我的代码。
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:当我执行程序时,它说:
Incorrect syntax near the column name.
列名附近的语法不正确。
Is it possible to add column to the SQL database programmatically?是否可以以编程方式将列添加到 SQL 数据库?
Is it possible to add column to the sql database programmatically?
是否可以以编程方式将列添加到 sql 数据库?
SQL Server processes SQL Commands. SQL 服务器处理 SQL 命令。 IT DOES NOT CARE WHERE THEY COME FROM.
它不关心他们来自哪里。
Also note that your error message CLEARLY seems to state this:另请注意,您的错误消息显然是 state 这个:
it say's that Incorrect syntax n
它说是不正确的语法 n
Incorrect Syntax means EXACTLY THIS.不正确的语法就是这个意思。
ALTER TABLE [dbo].[tblQuiz] ADD '" + txtQuizNo.Text.ToString() +"'
ALTER TABLE [dbo].[tblQuiz] ADD '" + txtQuizNo.Text.ToString() +"'
Why do you even consider:你为什么还要考虑:
Here is a simple beginner debug hint: Next time a SQL command fails like that, log it or use a debugger to get the FINISHED string.这是一个简单的初学者调试提示:下次 SQL 命令失败时,记录它或使用调试器获取 FINISHED 字符串。 Copy/paste it into SSMS and see whether it runs there.
将其复制/粘贴到 SSMS 中并查看它是否在那里运行。 Your whole question would be moot if you tried - because you would CLEARLY see it is NOT related to C# because you get the same error in SSMS.
如果您尝试过,您的整个问题将毫无意义 - 因为您会清楚地看到它与 C# 无关,因为您在 SSMS 中遇到相同的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.