C# SQL 服务器语法错误
C# with SQL server syntax error
所以我看这个已经太久了,我可能遗漏了什么但我无法理解是什么。当我尝试在标题为 "Unit Number" 的第三列中添加数据库条目时收到错误消息,该错误指出尝试添加时此区域中的语法无效。
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();
}
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);
}
在 using
语句中包含您的 SqlConnection
和 SqlCommand
对象将确保资源的处置。 using
将转换为 try-finally
块,在 SqlConnection
的情况下,它将 close/dispose 连接。
使用上面的代码,您不需要 finally
块,因为在 using
语句的范围结束后连接将是 diposed/closed。
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;) 会丢弃你的 table,其余的将被视为评论。
哎呀,你的学生们去了table!
您应该使用 Habib 的 C# 代码来避免这个问题。
所以我看这个已经太久了,我可能遗漏了什么但我无法理解是什么。当我尝试在标题为 "Unit Number" 的第三列中添加数据库条目时收到错误消息,该错误指出尝试添加时此区域中的语法无效。
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();
}
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);
}
在 using
语句中包含您的 SqlConnection
和 SqlCommand
对象将确保资源的处置。 using
将转换为 try-finally
块,在 SqlConnection
的情况下,它将 close/dispose 连接。
使用上面的代码,您不需要 finally
块,因为在 using
语句的范围结束后连接将是 diposed/closed。
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;) 会丢弃你的 table,其余的将被视为评论。
哎呀,你的学生们去了table!
您应该使用 Habib 的 C# 代码来避免这个问题。