使用访问数据库更新声明不起作用

编程入门 行业动态 更新时间:2024-10-26 13:30:13
本文介绍了使用访问数据库更新声明不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 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.

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

问题所在: 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", ......);

更多推荐

使用访问数据库更新声明不起作用

本文发布于:2023-10-23 19:54:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521854.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   声明   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!