将整数和字符串插入 SQL 服务器数据库 c# 的问题
Issue with Inserting integer and string into SQL Server database c#
我无法将整数值 [Question Type] 和字符串 [Question Space] 同时插入数据库的同一行。每当我单击按钮并尝试执行时,都会出现一个错误:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Incorrect syntax near '('.
代码:
SqlCommand command5 = new SqlCommand("INSERT INTO ([Question Space], [Question Type]) Questions VALUES ('@QuestionText', '@checkedradiobutton')", connect);
command5.Parameters.AddWithValue("@QuestionText", QuestionText);
command5.Parameters.AddWithValue("@checkedradiobutton", checkedradiobutton);
command5.ExecuteNonQuery();
如果有人能给我任何帮助,我将不胜感激。
如果你想看,这里是按钮的完整代码:
private void button1_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
SqlConnection connect = new SqlConnection(connectionString);
connect.Open();
int checkedradiobutton = 0;
if(radioButton1.Checked)
{
checkedradiobutton = 1;
}
else if(radioButton2.Checked)
{
checkedradiobutton = 2;
}
else if(radioButton3.Checked)
{
checkedradiobutton = 3;
}
string QuestionText = QuestionBox.Text;
SqlCommand command5 = new SqlCommand("INSERT INTO ([Question Space], [Question Type]) Questions VALUES ('@QuestionText', '@checkedradiobutton')", connect);
command5.Parameters.AddWithValue("@QuestionText", QuestionText);
command5.Parameters.AddWithValue("@checkedradiobutton", checkedradiobutton);
command5.ExecuteNonQuery();
}
还有我的数据库table:
CREATE TABLE [dbo].[Questions]
(
[QuestionID] INT IDENTITY (1, 1) NOT NULL,
[Actual answer] NVARCHAR (50) NULL,
[Question Space] NVARCHAR (50) NULL,
[Question Type] INT NULL,
PRIMARY KEY CLUSTERED ([QuestionID] ASC)
);
您不必在查询中用 ' 包装参数。开始清洁它,看看它是否有效
SqlCommand command5 = new SqlCommand("INSERT INTO [dbo].[Questions] ([Question Space], [Question Type]) Questions VALUES (@QuestionText, @checkedradiobutton)", connect);
command5.Parameters.AddWithValue("@QuestionText", QuestionText);
command5.Parameters.AddWithValue("@checkedradiobutton", checkedradiobutton);
command5.ExecuteNonQuery();
INSERT INTO 的正确语法是
INSERT INTO <table> (field1, field2, fieldN) VALUES (value1, value2, valueN)
所以你的命令应该写成
SqlCommand command5 = new SqlCommand(@"INSERT INTO Questions
([Question Space], [Question Type]) VALUES
(@QuestionText, @checkedradiobutton)", connect);
请注意参数占位符不应包含在单引号中,因为这样做会将它们转换为文字文本。 (在 [Question Space]
字段中插入 "@QuestionText"
字符串)
最后,尽量避免使用 AddWithValue,这是一个有很多缺点的快捷方式,您可以在 Can we stop using AddWithValue already and in How Data Access Code Affects Database Performance
中阅读
这还是一行代码
command5.Parameters.Add("@QuestionText", SqlDbType.NVarChar).Value = QuestionText;
INSERT语法错误,table的名字应该在"INSERT INTO"之后,例如:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
干杯。
我无法将整数值 [Question Type] 和字符串 [Question Space] 同时插入数据库的同一行。每当我单击按钮并尝试执行时,都会出现一个错误:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Incorrect syntax near '('.
代码:
SqlCommand command5 = new SqlCommand("INSERT INTO ([Question Space], [Question Type]) Questions VALUES ('@QuestionText', '@checkedradiobutton')", connect);
command5.Parameters.AddWithValue("@QuestionText", QuestionText);
command5.Parameters.AddWithValue("@checkedradiobutton", checkedradiobutton);
command5.ExecuteNonQuery();
如果有人能给我任何帮助,我将不胜感激。
如果你想看,这里是按钮的完整代码:
private void button1_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
SqlConnection connect = new SqlConnection(connectionString);
connect.Open();
int checkedradiobutton = 0;
if(radioButton1.Checked)
{
checkedradiobutton = 1;
}
else if(radioButton2.Checked)
{
checkedradiobutton = 2;
}
else if(radioButton3.Checked)
{
checkedradiobutton = 3;
}
string QuestionText = QuestionBox.Text;
SqlCommand command5 = new SqlCommand("INSERT INTO ([Question Space], [Question Type]) Questions VALUES ('@QuestionText', '@checkedradiobutton')", connect);
command5.Parameters.AddWithValue("@QuestionText", QuestionText);
command5.Parameters.AddWithValue("@checkedradiobutton", checkedradiobutton);
command5.ExecuteNonQuery();
}
还有我的数据库table:
CREATE TABLE [dbo].[Questions]
(
[QuestionID] INT IDENTITY (1, 1) NOT NULL,
[Actual answer] NVARCHAR (50) NULL,
[Question Space] NVARCHAR (50) NULL,
[Question Type] INT NULL,
PRIMARY KEY CLUSTERED ([QuestionID] ASC)
);
您不必在查询中用 ' 包装参数。开始清洁它,看看它是否有效
SqlCommand command5 = new SqlCommand("INSERT INTO [dbo].[Questions] ([Question Space], [Question Type]) Questions VALUES (@QuestionText, @checkedradiobutton)", connect);
command5.Parameters.AddWithValue("@QuestionText", QuestionText);
command5.Parameters.AddWithValue("@checkedradiobutton", checkedradiobutton);
command5.ExecuteNonQuery();
INSERT INTO 的正确语法是
INSERT INTO <table> (field1, field2, fieldN) VALUES (value1, value2, valueN)
所以你的命令应该写成
SqlCommand command5 = new SqlCommand(@"INSERT INTO Questions
([Question Space], [Question Type]) VALUES
(@QuestionText, @checkedradiobutton)", connect);
请注意参数占位符不应包含在单引号中,因为这样做会将它们转换为文字文本。 (在 [Question Space]
字段中插入 "@QuestionText"
字符串)
最后,尽量避免使用 AddWithValue,这是一个有很多缺点的快捷方式,您可以在 Can we stop using AddWithValue already and in How Data Access Code Affects Database Performance
中阅读这还是一行代码
command5.Parameters.Add("@QuestionText", SqlDbType.NVarChar).Value = QuestionText;
INSERT语法错误,table的名字应该在"INSERT INTO"之后,例如:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
干杯。