【问题标题】:Syntax Error while Inserting variables into a table Visual Studios 2015 C#将变量插入表中时出现语法错误 Visual Studios 2015 C#
【发布时间】:2018-04-25 19:42:41
【问题描述】:

在尝试将答案保存/插入到我的数据库中的表中时,我目前对我当前的语法错误问题的确切含义感到困惑。当我尝试使用硬编码变量进行此操作时效果很好,但现在情况并非如此。

部分错误信息:

附加信息:')' 附近的语法不正确

不知道我做错了什么。下面是我正在使用的代码和错误指向的位置。感谢您提供任何可能的帮助和澄清。

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.";
    }

【问题讨论】:

  • 这就是为什么你应该使用参数而不是连接。
  • 多年来,这一直不是构建查询的正确方法。五分钟的研究会让你得到几篇数千个解释这一点的帖子和关于参数的信息。
  • 我学院大部分CS课程的教授基本上都是教我们,然后强迫我们这样写。否则,我们会因为不遵循他的榜样而失分。
  • 可能你的教授也需要看看 Stack Overflow 上的一些帖子。

标签: c# sql-server ado.net


【解决方案1】:

首先你不应该像这样构建 SQL 语句,它很容易出现很多问题,但你的问题在于你的字符串,你的字符串周围没有单引号:

strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", '" + strAnswer + "')";

需要像我上面那样在 strAnswer 周围添加单引号

使用此处列出的参数: https://msdn.microsoft.com/library/bb738521(v=vs.100).aspx

【讨论】:

  • 你的最后一个单引号放错了位置,它应该在双引号内。
【解决方案2】:

我同意关于字符串连接的评论。如果您必须在代码中编写 SQL 查询,您应该使用字符串插值。

如果我必须这样做,我会这样写:

String strSQL = $"INSERT INTO Answer (StudentID, QuestionID, Answer) VALUES ( {int32StudentID}, {int32QuestionID}, '{strAnswer}')";

也就是说,这不是您出现语法错误的原因。您的字符串变量周围缺少单引号。试试这个:

strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", '" + strAnswer + "')";

【讨论】:

    【解决方案3】:

    错误在这一行

    strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", " + strAnswer + ")";
    

    根据您的 SQL 查询和数据库,字段 Answervarcharnvarchar 类型的字段。这种类型的字段总是取字符串类型的值。这已经由你完成了。但是 SQL Server 数据库在单引号 '' 中接受这些值。因此,您的解决方案是

    strSQL += " ( " + int32StudentID + ", " + int32QuestionID + ", '" + strAnswer + "')";
    

    因为我在strAnswer 前面和strAnswer 的最后添加了一个单引号

    谢谢。

    【讨论】:

    • 你犯了和 Brad 一样的错误——最后一个单引号紧跟右括号前面的双引号。
    • 对此我感到非常抱歉。感谢您的关心。
    猜你喜欢
    • 2018-05-04
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多