为什么在尝试更新数据库时出现语法错误?
Why am I getting syntax error while trying to update database?
我一直在尝试环顾四周并解决这个问题,我已经尝试了几个小时,所以我决定问问其他人。
我收到
'Syntax error in UPDATE statement.'
点击保存按钮时。
这是我的代码:
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) + "'
(不要忘记用单引号括起来)。此外,您应该始终使用 parameterized queries to avoid SQL Injection。像这样:
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
语句。
CVV='" + textBox6.Text + "', where
你必须删除这里的逗号。
也最好使用参数,因为你有几个。像那样发送它们会在将来引起问题。所以我建议你使用 cmd.Parameters.Add();
而不是原始用法。另外如果 ID 是 Integer 你必须 Convert.ToInt32(textBox7.Text);
另一个原因可能是,您从文本框中读取的值可能包含特殊字符,这会使语法在连接到 SQL 字符串时无效。
这在使用参数查询时也可以避免。
我一直在尝试环顾四周并解决这个问题,我已经尝试了几个小时,所以我决定问问其他人。 我收到
'Syntax error in UPDATE statement.'
点击保存按钮时。
这是我的代码:
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) + "'
(不要忘记用单引号括起来)。此外,您应该始终使用 parameterized queries to avoid SQL Injection。像这样:
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
语句。
CVV='" + textBox6.Text + "', where
你必须删除这里的逗号。
也最好使用参数,因为你有几个。像那样发送它们会在将来引起问题。所以我建议你使用 cmd.Parameters.Add();
而不是原始用法。另外如果 ID 是 Integer 你必须 Convert.ToInt32(textBox7.Text);
另一个原因可能是,您从文本框中读取的值可能包含特殊字符,这会使语法在连接到 SQL 字符串时无效。 这在使用参数查询时也可以避免。