问题描述
我一直在用C#创建一个软件。我试图更新Access数据库中的一些信息。这是我的数据库fields.Date,total_h,W_hours,delay_h。日期是主键。所以我想更新Date =datetimePicker.text的数据。这是代码我试过的。
I have been creating a software in C#. I tried to update some information in my Access database. Here's my database fields.Date ,total_h, W_hours, delay_h. Date is the Primary key. So I want to Update data where Date="datetimePicker.text". here is the Code What I tried.
try
{
connection.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = connection;
string update = "update summery_data set total_h='"+tHour+"', delay_h='"+delay+"' WHERE Date= " + dateTimePicker1.Text + " ";
cmd.CommandText = update;
cmd.ExecuteNonQuery();
MessageBox.Show(" Updated successfully");
connection.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
程序正常运行,没有任何异常,并且还显示已成功更新消息。但是,当我打开并检查数据库时,数据尚未更新。我无法理解问题是什么......?请帮助我有人知道。
我尝试过:
程序正常运行,没有任何异常,并且还显示已成功更新消息。但是,当我打开并检查数据库时,数据尚未更新。
The Program is running properly without any Exception and displaying the "Updated successfully" message also. But when I open and Check the database the data has not been updated. I can't Understand what the problem is...?. please help me someone knows about it.
What I have tried:
The Program is running properly without any Exception and displaying the "Updated successfully" message also. But when I open and Check the database the data has not been updated.
推荐答案
string update = "update summery_data set total_h=@TH, delay_h=@DH WHERE Date=@DAT";
cmd.CommandText = update;
cmd.Parameters.AddWithValue("@TH", tHour);
cmd.Parameters.AddWithValue("@DH", delay);
cmd.Parameters.AddWithValue("@DAT",dateTimePicker1.Value);
这篇关于Access数据库无法正确更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!