[英]Code runs but doesn't update database
This code is supposed to save some values in textboxes to a specific row. 该代码应将文本框中的某些值保存到特定行。 The code runs just fine with no hiccups, but refuses to actually update the database no matter what I do.
该代码运行良好,没有打ic,但无论我做什么,都拒绝实际更新数据库。
try
{
using (var con = new OleDbConnection())
{
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\User\Desktop\esoft\gym\gym\bin\Debug\Clients.accdb;";
con.Open();
using (var com = new OleDbCommand())
{
com.Connection = con;
com.CommandText = "UPDATE gym SET BMI = @bmi and Health = @health and weight_change_to_healthy_bmi = @weight WHERE ID = @id";
com.Parameters.AddWithValue("@bmi", bmi.Text);
com.Parameters.AddWithValue("@health", health.Text);
com.Parameters.AddWithValue("@weight", change.Text);
com.Parameters.AddWithValue("@id", id.Text);
com.ExecuteNonQuery();
}
}
MessageBox.Show("Saved");
}
catch (Exception ex)
{
MessageBox.Show("Not saved: " + ex.Message);
}
Any help would be much appreciated. 任何帮助将非常感激。
As Alex mentioned , SET
part needs ,
instead of AND
for multiple columns. 如Alex 所述 ,
SET
部分需要,
而不是AND
以用于多列。
Check UPDATE
syntax 1 ; 检查
UPDATE
语法 1 ;
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
But I wanna say a few things more; 但是我想说几件事;
AddWithValue
as much as you can. AddWithValue
。 It may generate unexpected and surprising results sometimes . Add
method overload to specify your parameter type and it's size. Add
方法重载来指定您的参数类型及其大小。 ExecuteNonQuery
line. ExecuteNonQuery
行之前打开连接。 ID
column should be some numeric value instead of character. ID
列应为一些数字值而不是字符。 Consider to change it's type or consider to change it's column name that refers some character typed column name. 1: I know I know.. a w3school link 1: 我知道我知道.. w3school链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.