使用访问数据库更新声明不起作用 [英] working with access database update statment not working

查看:74
本文介绍了使用访问数据库更新声明不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string date=
DateTime.Now.ToString("d.M.yyyy",System.Globalization.DateTimeFormatInfo.InvariantInfo);

String MyString = @"UPDATE cas SET Odhod= '" + label1.Text + "' 
WHERE sifra = " + textBox1.Text + " and Datum = "+date+"";

当我在没有Datum的情况下执行此更新时,它可以工作,但在Datum中不起作用.我已连接到Access数据库,表中的Datum字段类型为日期/时间,请帮忙.

When I do thise update without Datum it works, but with Datum doesn't work. I'm connected to accesss database, and Datum field type in table is date/time Guys please help.

该程序是: https://www. dropbox.com/s/hx4zduvul8mh2uy/8.4.zip

问题所在: http://img43.imageshack.us/img43/5189/errorbh.jpg

推荐答案

像往常一样,使用字符串连接会带来很多麻烦.
(Sql注入,解析问题)

As usual, using string concatenation brings in a lot of trouble.
(Sql Injection, Parsing problems)

只需使用参数化查询

string MyString = @"UPDATE cas SET Odhod= ? WHERE sifra = ? and Datum = ?"; 


using(OleDbConnection cn = new OleDbConnection(connectionstring))
using(OleDbCommand cmd = new OleDbCommand(MyString, cn)
{
    cn.Open();
    cmd.Parameters.AddWithValue("@p1", label1.Text);
    cmd.Parameters.AddWithValue("@p2", textbox.Text);
    cmd.Parameters.AddWithValue("@p3", Convert.ToDate(date));
    cmd.ExecuteNonQuery();
}

当然,存储在基准"字段中的日期"值应与参数@ p3中传递的日期完全相同.有时最好在您的日期中添加时间值

Of course, the Date value stored in the Datum field should be exactly like the date passed in parameter @p3. Sometime it is good to add also the time value to your date

string date= DateTime.Now.ToString("d.M.yyyy 00:00:00", ......);

这篇关于使用访问数据库更新声明不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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