【问题标题】:Syntax error during inserting data using mysql使用mysql插入数据时出现语法错误
【发布时间】:2014-10-05 08:46:34
【问题描述】:

错误:

“s”附近的语法不正确。 字符串 ');' 后面的非闭合引号。

代码:

private void btnAdd_Click(object sender, EventArgs e)
{
    SqlConnection cn = new SqlConnection(global::CIMT.Properties.Settings.Default.Database2ConnectionString);

    try 
    {
        string sql = "INSERT INTO Students(Student_Id,First_Name,Last_Name,Fathers_Name,DOB,Mobile,Address,Post_Code) VALUES('"+this.txtId.Text+"','"+this.txtFName.Text+"','"+this.txtLName.Text+"','"+this.txtFaName.Text+"','"+this.txtDOB.Text+"','"+this.txtMob.Text+"','"+this.txtAddress.Text+"','"+this.txtPostCode.Text+ "');";
        SqlCommand exesql = new SqlCommand(sql, cn);
        cn.Open();
        exesql.ExecuteNonQuery();

        MessageBox.Show("Add new record done !!" , "Message" , MessageBoxButtons.OK , MessageBoxIcon.Information);
        this.studentsTableAdapter.Fill(this.database2DataSet.Students);
    }

    catch (Exception ex) 
    {
        MessageBox.Show(ex.Message , "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    finally 
    {
        cn.Close();
    }
}

【问题讨论】:

  • 这种方法容易受到 SQL 注入的攻击。为了我们的缘故,请参数化您的查询。这也几乎肯定会修复这个语法错误。
  • 您是否将输入输入为 "hello's world" ,然后,它不会被转义,并且您会收到错误,因此如上所述(SQL 注入),最好使用参数化查询。

标签: c# mysql winforms


【解决方案1】:

使用那些在 cmets 中告诉你的参数化查询,不仅可以避免错误,还可以帮助你避免 SQL 注入。

private void btnAdd_Click(object sender, EventArgs e)
{
    var cnString = global::CIMT.Properties.Settings.Default.Database2ConnectionString;
    using (SqlConnection cn = new SqlConnection(cnString))
    {
        try 
        {
            cn.Open();
            using (var exesql = new SqlCommand(
                      @"INSERT INTO Students(Student_Id
                                            ,First_Name
                                            ,Last_Name
                                            ,Fathers_Name
                                            ,DOB
                                            ,Mobile
                                            ,Address
                                            ,Post_Code) 
                        VALUES(@Student_Id
                                ,@First_Name
                                ,@Last_Name
                                ,@Fathers_Name
                                ,@DOB
                                ,@Mobile
                                ,@Address
                                ,@Post_Code);",
            cn))
            {
                exesql.Parameters.AddWithValue("@Student_Id", this.txtId.Text);
                exesql.Parameters.AddWithValue("@First_Name", this.txtFName.Text);
                exesql.Parameters.AddWithValue("@Last_Name",this.txtLName.Text );
                exesql.Parameters.AddWithValue("@Fathers_Name", this.txtFaName.Text);
                exesql.Parameters.AddWithValue("@DOB", this.txtDOB.Text);
                exesql.Parameters.AddWithValue("@Mobile", this.txtMob.Text);
                exesql.Parameters.AddWithValue("@Address", this.txtAddress.Text);
                exesql.Parameters.AddWithValue("@Post_Code", this.txtPostCode.Text);

                exesql.ExecuteNonQuery();

                MessageBox.Show("Add new record done !!" , "Message" , MessageBoxButtons.OK 
                                , MessageBoxIcon.Information);
                this.studentsTableAdapter.Fill(this.database2DataSet.Students);
            }
        }
        catch (Exception ex) 
        {
            MessageBox.Show(ex.Message , "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

阅读SqlParameter Class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 2018-09-18
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    相关资源
    最近更新 更多