Access数据库无法正确更新 [英] Access database doesn't update properly

查看:109
本文介绍了Access数据库无法正确更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在用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.

推荐答案

不要这样做。不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。

这可能会同时解决您的问题:WHERE子句很可能与任何记录不匹配。

Don't do it like that. Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
The chances are that this will cure your problem at the same time: it's very likely that the WHERE clause is not matching any records.
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数据库无法正确更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆