将变量插入 table Visual Studios 2015 C# 时出现语法错误
Syntax Error while Inserting variables into a table Visual Studios 2015 C#
我现在对我当前语法错误问题的确切含义感到困惑,同时试图 save/insert 回答我数据库中的 table。当我尝试使用硬编码变量时它工作正常但现在不是这样了。
部分错误信息:
Additional information: Incorrect syntax near ')'
不确定我做错了什么。下面是我正在使用的代码以及错误指向的位置。感谢您提供任何可能的帮助和澄清。
protected void btnSaveAnswers_Click(object sender, EventArgs e)
{
Int32 int32StudentID = Convert.ToInt32(Session["StudentID"]);
Int32 int32QuestionID = Convert.ToInt32(Session["QuestionID"]);
String strAnswer = "";
// Save the student's answer to the Answer table.
// Develop the SQL call.
String strSQL = "";
strSQL = "INSERT ";
strSQL += "INTO Answer ";
strSQL += " (StudentID, QuestionID, Answer) ";
strSQL += "VALUES ";
strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", " + strAnswer + ")";
// Define the network connection to the SQL Server database.
SqlConnection objSqlConnection = new SqlConnection(WebConfigurationManager.ConnectionStrings["OPT"].ConnectionString);
// Create the SQL command object.
SqlCommand objSqlCommand = new SqlCommand();
objSqlCommand.Connection = objSqlConnection;
objSqlCommand.CommandType = CommandType.Text;
objSqlCommand.CommandText = strSQL;
// Open the connection.
objSqlConnection.Open();
// Execute the Insert statement.
objSqlCommand.ExecuteNonQuery();
// Close the connection.
objSqlConnection.Close();
this.Master.MessageForeColor = System.Drawing.Color.White;
this.Master.Message = "You have saved your answer for this question, click next to continue.";
}
首先你不应该像这样构建 SQL 语句,它容易出现很多问题,但你的问题出在你的字符串上,你没有在它周围加上单引号:
strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", '" + strAnswer + "')";
需要像上面那样在 strAnswer 周围添加单引号
使用此处概述的参数:
https://msdn.microsoft.com/library/bb738521(v=vs.100).aspx
错误在这一行
strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", " + strAnswer + ")";
根据您的 SQL 查询和数据库,字段 Answer
是 varchar
或 nvarchar
类型的字段。这种类型的字段总是取字符串类型的值。你已经完成了。但是 SQL 服务器数据库在单引号 ''
内接受这些值。因此你的解决方案是
strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", '" + strAnswer + "')";
因为我在 strAnswer
前面和 strAnswer
的最后添加了一个单引号
谢谢。
我同意关于字符串连接的评论。如果您必须在代码中编写 SQL 查询,您应该使用字符串插值。
如果非要我写的话,我会这样写:
String strSQL = $"INSERT INTO Answer (StudentID, QuestionID, Answer) VALUES ( {int32StudentID}, {int32QuestionID}, '{strAnswer}')";
也就是说,这不是您出现语法错误的原因。您的字符串变量周围缺少单引号。试试这个:
strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", '" + strAnswer + "')";
我现在对我当前语法错误问题的确切含义感到困惑,同时试图 save/insert 回答我数据库中的 table。当我尝试使用硬编码变量时它工作正常但现在不是这样了。
部分错误信息:
Additional information: Incorrect syntax near ')'
不确定我做错了什么。下面是我正在使用的代码以及错误指向的位置。感谢您提供任何可能的帮助和澄清。
protected void btnSaveAnswers_Click(object sender, EventArgs e)
{
Int32 int32StudentID = Convert.ToInt32(Session["StudentID"]);
Int32 int32QuestionID = Convert.ToInt32(Session["QuestionID"]);
String strAnswer = "";
// Save the student's answer to the Answer table.
// Develop the SQL call.
String strSQL = "";
strSQL = "INSERT ";
strSQL += "INTO Answer ";
strSQL += " (StudentID, QuestionID, Answer) ";
strSQL += "VALUES ";
strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", " + strAnswer + ")";
// Define the network connection to the SQL Server database.
SqlConnection objSqlConnection = new SqlConnection(WebConfigurationManager.ConnectionStrings["OPT"].ConnectionString);
// Create the SQL command object.
SqlCommand objSqlCommand = new SqlCommand();
objSqlCommand.Connection = objSqlConnection;
objSqlCommand.CommandType = CommandType.Text;
objSqlCommand.CommandText = strSQL;
// Open the connection.
objSqlConnection.Open();
// Execute the Insert statement.
objSqlCommand.ExecuteNonQuery();
// Close the connection.
objSqlConnection.Close();
this.Master.MessageForeColor = System.Drawing.Color.White;
this.Master.Message = "You have saved your answer for this question, click next to continue.";
}
首先你不应该像这样构建 SQL 语句,它容易出现很多问题,但你的问题出在你的字符串上,你没有在它周围加上单引号:
strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", '" + strAnswer + "')";
需要像上面那样在 strAnswer 周围添加单引号
使用此处概述的参数: https://msdn.microsoft.com/library/bb738521(v=vs.100).aspx
错误在这一行
strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", " + strAnswer + ")";
根据您的 SQL 查询和数据库,字段 Answer
是 varchar
或 nvarchar
类型的字段。这种类型的字段总是取字符串类型的值。你已经完成了。但是 SQL 服务器数据库在单引号 ''
内接受这些值。因此你的解决方案是
strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", '" + strAnswer + "')";
因为我在 strAnswer
前面和 strAnswer
谢谢。
我同意关于字符串连接的评论。如果您必须在代码中编写 SQL 查询,您应该使用字符串插值。
如果非要我写的话,我会这样写:
String strSQL = $"INSERT INTO Answer (StudentID, QuestionID, Answer) VALUES ( {int32StudentID}, {int32QuestionID}, '{strAnswer}')";
也就是说,这不是您出现语法错误的原因。您的字符串变量周围缺少单引号。试试这个:
strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", '" + strAnswer + "')";