';附近的语法不正确;s';
本文关键字:不正确 语法 | 更新日期: 2023-09-27 18:28:24
我在sc.ExecuteNonQuery();
上得到一个错误。。错误:Incorrect syntax near 's'
代码:
con = new SqlConnection("Data Source=DELL-PC;Initial Catalog=sashi;Integrated Security=True");
con.Open();
SqlCommand sc = new SqlCommand("INSERT INTO Login VALUES('" + textBoxUID.Text + "','" + textBoxPWD.Text + "','" + comboBoxQUN.Text + "','" + textBoxANS.Text + "' ) ", con);
sc.ExecuteNonQuery();
MessageBox.Show("Record has been inserted");
con.Close();
我忘了什么或者错误在哪里?
请使用以下参数:
using (var con = new SqlConnection("Data Source=DELL-PC;Initial Catalog=sashi;Integrated Security=True"))
{
con.Open();
using(var sc = connection.CreateCommand())
{
sc.CommandText = "INSERT INTO Login VALUES(@uid,@pass,@qun,@ans)";
sc.Parameters.Add(new SqlParameter("@uid", textBoxUID.Text));
sc.Parameters.Add(new SqlParameter("@pass", textBoxPWD.Text));
sc.Parameters.Add(new SqlParameter("@qun", comboBoxQUN.Text));
sc.Parameters.Add(new SqlParameter("@ans", textBoxANS.Text));;
sc.ExecuteNonQuery();
}
}
Sql参数有助于防止Sql注入攻击。。而且更容易阅读。。您的登录表只有四列吗?否则,您还必须在插入语句中指定:INSERT INTO (col1, col2 ....