C#与SQL服务器语法错误

时间:2015-03-06 16:26:27

标签: c# sql database syntax

所以我现在看这个太久了,我可能会遗漏一些但我无法理解的是什么。尝试在标题为"单元号"的第三列中添加数据库条目时收到错误该错误表明在尝试添加时,此区域中的语法无效。

SqlConnection cn = new SqlConnection(global::ProjectAssessment.Properties.Settings.Default.Database1ConnectionString);
        try
        {
            string sql = "INSERT INTO Students (Student_Id,Student_name,Unit_number,Unit_grade) values(" +textBox1.Text+ ",'" +textBox2.Text+ ",'" +textBox3.Text+ ",'" +textBox4.Text+"')";
            SqlCommand exesql = new SqlCommand(sql, cn);
            cn.Open();
            exesql.ExecuteNonQuery();

            MessageBox.Show("Student record added", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.studentsTableAdapter.Fill(this.database1DataSet.Students);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            cn.Close();
        }

2 个答案:

答案 0 :(得分:4)

string sql = "INSERT INTO Students (Student_Id,Student_name,Unit_number,Unit_grade) 
     values(" +textBox1.Text+ ",'" +textBox2.Text+ ",'" +textBox3.Text+ ",'" +textBox4.Text+"')";

                                                //^^^^ and others

单一看,您可以看到您的值中缺少单引号。您可以修复它们,但不要。使用SqlParameters,它们不仅可以避免这些错误,还可以避免SQL注入。

所以你的代码应该是这样的:

try
{
    using (SqlConnection cn = new SqlConnection(global::ProjectAssessment.Properties.Settings.Default.Database1ConnectionString))
    using (SqlCommand exesql = new SqlCommand(@"INSERT INTO Students (Student_Id,Student_name,Unit_number,Unit_grade) values(@studentID, @studentName, @unitNumber, @unitGrade)", cn))
    {
        exesql.Parameters.Add("@studentID", SqlDbType.Int).Value = textBox1.Text;
        exesql.Parameters.Add("@studentName", SqlDbType.VarChar).Value = textBox2.Text;
        //... and others

        cn.Open();
        exesql.ExecuteNonQuery();

    }

    MessageBox.Show("Student record added", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
    this.studentsTableAdapter.Fill(this.database1DataSet.Students);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

SqlConnectionSqlCommand对象括在using语句中将确保资源的处置。 using将转换为try-finally块,如果SqlConnection,它将关闭/处置连接。

使用上面的代码,您不需要finally块,因为在using语句的范围结束后,连接将被暂停/关闭。

答案 1 :(得分:0)

string sql = "INSERT INTO Students (Student_Id,Student_name,Unit_number,Unit_grade) values(" +textBox1.Text+ ",'" +textBox2.Text+ ",'" +textBox3.Text+ ",'" +textBox4.Text+"')";

应该是

string sql = "INSERT INTO Students (Student_Id,Student_name,Unit_number,Unit_grade) values(" +textBox1.Text+ ",'" +textBox2.Text+ "','" +textBox3.Text+ "','" +textBox4.Text+"')";

请注意,非常不安全,并且容易出现SQL注入!例如,如果某人确定学生ID应为;DROP TABLE STUDENTS; --,那么您的SQL语句将如下所示:

INSERT INTO STUDENTS (Student_Id, Student_name, Unit_number, Unit_grade) values(;DROP TABLE STUDENTS; --, 'tb2val', 'tb3val', 'tb4val')

根据事情的执行方式,第一个语句会抛出语法错误,第二个语句(DROP TABLE STUDENTS;)会丢弃你的表,其余语句将被视为注释。

哎呀,那里有你的学生桌!

您应该使用Habib的C#代码来避免此问题。

相关问题