c# - 为什么在尝试更新数据库时出现语法错误?
问题描述
我一直试图环顾四周并解决这个问题,并且我已经尝试了几个小时,所以我决定我会问其他人。我得到一个
'UPDATE 语句中的语法错误。'
单击保存按钮时。
这是我的代码:
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "update Profiles set [PROFILE NAME]='" + textBox1.Text + "', [LOGIN EMAIL]='" + textBox2.Text + "', [PASSWORD]='" + textBox3.Text + "', [FULL NAME]='" + textBox4.Text + "', [CARD NUMBER]='" + textBox5.Text + "', [EXP MONTH]='" + comboBox1.Text + "', [EXP YEAR]='" + comboBox2.Text + "', CVV='" + textBox6.Text + "', where ID=" + textBox7.Text + "";
command.CommandText = query;
command.ExecuteNonQuery();
connection.Close();
MessageBox.Show("Profile Saved");
RefreshDBConnection();
更新代码:
ConnectToDataBase();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
//string query = "update Profiles set [PROFILE NAME]='" + textBox1.Text + "', [LOGIN EMAIL]='" + textBox2.Text + "', [PASSWORD]='" + textBox3.Text + "', [FULL NAME]='" + textBox4.Text + "', [CARD NUMBER]='" + textBox5.Text + "', [EXP MONTH]='" + comboBox1.Text + "', [EXP YEAR]='" + comboBox2.Text + "', CVV='" + textBox6.Text + "' where ID='" + Convert.ToInt32(textBox7.Text) + "'";
string query = "update Profiles set [PROFILE NAME]= @Profile, [LOGIN EMAIL]= @Email, [PASSWORD]= @Pass, [FULL NAME]= @Name, [CARD NUMBER]= @Card, [EXP MONTH]= @EXPM, [EXP YEAR]= @EXPY, CVV= @CVV where ID = '" +textBox7.Text+ "'";
command.Parameters.AddWithValue("@Profile", textBox1.Text);
command.Parameters.AddWithValue("@Email", textBox2.Text);
command.Parameters.AddWithValue("@Pass", textBox3.Text);
command.Parameters.AddWithValue("@Name", textBox4.Text);
command.Parameters.AddWithValue("@Card", Convert.ToInt32(textBox5.Text));
command.Parameters.AddWithValue("@EXPM", Convert.ToInt32(comboBox1.Text));
command.Parameters.AddWithValue("@EXPY", Convert.ToInt32(comboBox2.Text));
command.Parameters.AddWithValue("@CVV", Convert.ToInt32(textBox6.Text));
command.CommandText = query;
command.ExecuteNonQuery();
connection.Close();
MessageBox.Show("Profile Saved");
RefreshDBConnection();
this.Close();
解决方案
你的语句,
前多了一个逗号:Where
CVV='" + textBox6.Text + "', where
只需将其删除。如果它的类型是整数,你应该将你的转换textBox7.Text
为 int,ID= '" + Convert.ToInt32(textBox7.Text) + "'
(不要忘记用单引号括起来)。此外,您应该始终使用参数化查询来避免SQL 注入。像这样的东西:
string query = "update Profiles set [PROFILE NAME]= @Profile,... where ID = @Id";
command.Parameters.AddWithValue("@Profile", textBox1.Text);
command.Parameters.AddWithValue("@Id", textBox7.Text);//Or Convert.ToInt32(textBox7.Text)
虽然直接指定类型并使用Value
属性比AddWithValue
:
command.Parameters.Add("@Profile", SqlDbType.VarChar).Value = textBox1.Text;
command.Parameters.Add("@Id", SqlDbType.Int).Value = Convert.ToInt32(textBox7.Text);
当然,建议using
始终使用语句。
推荐阅读
- c - 递归函数,根据索引 ij 中包含的内容切换到下一个索引,C
- jquery - laravel jquery 3 选择下拉列表类别、类型、子类型。类型下拉返回 id 从 0 开始而不是 1
- arrays - Read() 不适用于数组中的字符串类型输入
- javascript - 计算给定代码的时间复杂度的问题
- javascript - 我需要在 classList 模板字符串 javascript 中增加一个数字
- javascript - 如何使用 px 像使用 rem 一样自动响应页面
- python - Python:在条件下使用数组的索引范围
- python-3.x - 在 Beautiful Soup 中抓取多个页面
- c# - 如何确定谁或什么杀死了我的 Windows 服务
- c++ - 为什么会发生此链接器警告和段错误?