简体   繁体   English

我正在尝试使用 C# 更改表添加列

[英]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:你为什么还要考虑:

  • using this REALLY outdated syntax to add to strings.使用这种过时的语法添加到字符串中。
  • Turning the name of the new field into a STRING CONSTANT (by bracketing it in ') instead of a name constant?将新字段的名称转换为字符串常量(通过将其括在 ' 中)而不是名称常量? Names are not strings in SQL - in fact it is illegal to use constants or variables instead of names.名称不是 SQL 中的字符串 - 实际上使用常量或变量代替名称是非法的。 So, you create what it says - a SYNTAX error - by making the name a string constant.因此,您通过将名称设为字符串常量来创建它所说的 - 语法错误。

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.

相关问题 c#alter table name add column - c# alter table name add column C#Alter在OLEDB上的Excel上添加列 - C# Alter add column on excel with OLEDB c# (ado) 更改表添加列 - 为变量中的列命名 (日期时间) - c# (ado) alter table add column - give a name to a column from a variable (Datetime) 我正在尝试使用 MVC C# 在 SQL 中记录审核表,以使用 controller 跟踪编辑 - I am trying to log an audit table in SQL using MVC C# to track edits using the controller 索引越界c#(我正在尝试添加savegame并使用WriteAllLines) - index out of bounds c# (I'm trying to add savegame and am using WriteAllLines) C#Alter Table并以编程方式添加一列ASP.Net和SQL Server - C# Alter Table and Add a column programmatically ASP.Net & SQL Server 如何使用 C# 中的方法在列表中添加数据? 我正在尝试添加但它无法保存数据而只显示最后一个条目 - How add data in list using method in C# ? i am trying to add but it cannot hold data and just display the last entry 我正在尝试使用带有IronPython nuget的C#调用python - I am trying to call python using C# with the IronPython nuget 我正在尝试使用c#用字母替换数组中包含1的每个列值,似乎不起作用 - i am trying to replace each column values which contain 1's in an array with alphabets using c#, doesn't seem to work .NET的SQLite无法使用alter table添加列 - Sqlite for .net can't add column using alter table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM