使用datetimepicker更新数据库中的日期 [英] Updating date in database using datetimepicker

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

问题描述

我需要更新数据库中的日期,其中值来自datetimepicker

我正在使用代码

dateTimePicker2.Value.Date

但它显示我的错误

无法从字符串转换日期或时间

I need to update date in database where values are coming from datetimepicker
I am Using code
"dateTimePicker2.Value.Date"
But it is showing me error
"Cannot convert date or time from string"

推荐答案

嗨Ramesh,

使用dateTimePicker1.Value而不是dateTimePicker1.Text。



像往常一样看这里: http://social.msdn.microsoft.com/Forums/en/adodotnetdataproviders/thread/ff2f7106-9a5b-4b70-95c7-28e9ee14336a [ ^ ]
Hi Ramesh,
Use dateTimePicker1.Value instead of dateTimePicker1.Text.

see here as usual same : http://social.msdn.microsoft.com/Forums/en/adodotnetdataproviders/thread/ff2f7106-9a5b-4b70-95c7-28e9ee14336a[^]


没有你的代码,很难确定,但尝试使用参数化查询 - 你应该使用它们来避免Sql注入攻击。如果你连接字符串以形成你的sql UPDATE语句,它可能是一个本地化的东西。由于Value和Value.Date都返回DateTime值,您应该可以直接使用它们。

Without your code, it is difficult to be sure, but try using a parameterized query - you should use them anyway to avoid Sql Injection Attacks. If you are concatenating strings to form your sql UPDATE statement, it may be a localization thing. Since both Value and Value.Date return a DateTime value, you should be able to use them directly.
using (SqlCommand com = new SqlCommand("UPDATE myTable SET myDateColumn=@DAT WHERE Id=@ID", con))
    {
    com.Parameters.AddWithValue("@ID", Id);
    com.Parameters.AddWithValue("@DAT", dateTimePicker2.Value.Date);
    com.ExecuteNonQuery();
    }


update table_name set date_colu_name ='+ datetimepicker1.text +'where date_colu_name is null

i am using使用c#.net访问,代码中的错误是什么?快速回复请
update table_name set date_colu_name='"+datetimepicker1.text+"' where date_colu_name is null
i am using access with c#.net,WHAT is the error in code? fast reply pls


这篇关于使用datetimepicker更新数据库中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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