从数据库中选择vb.net日期和时间 [英] vb.net date and time selection from database

查看:63
本文介绍了从数据库中选择vb.net日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DateTimePicker2.Value.ToShortDateString(drr.GetValue(5))


这是我的编码,但仅显示错误...我想从数据库sql 2005中检索日期,以使用vb.net将其显示到组合框中.


This is my coding but it shows error only... i want to retrive the date from database sql 2005 to show it into combobox using vb.net.

推荐答案

1 )从数据库中读取值.
2)将其转换为DateTime.
2.1)如果将其存储在SQL Date类型中,则只需将其强制转换即可:
1) Read the value from the database.
2) Convert it to a DateTime.
2.1) If you stored this in an SQL Date type, then just cast it:
Dim d As DateTime = drr("myDate")


2.2)如果您将其存储为字符串,请对其进行转换:


2.2) If you stored it as a string, then convert it:

Dim d As DateTime = DateTime.ParseExact(ddr("myDate"), "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture)


3)现在,您可以将其转换为ComboBox的适当字符串.

如果您不将字符串转换为DateTime,那么您将受制于您在其中输入信息的区域性:因此,如果它是由日语输入的,它将显示为2011-08-01,而在美国输入则为显示为2011年8月1日.如果您的用户是英语,这两种方法都不会很有帮助...

始终将日期存储在日期"字段中:可以解决此问题!


3) You can now convert it to the appropriate string for the ComboBox.

If you don''t convert the string to DateTime, then you are stuck with the culture that you entered the info in: so if it is entered by a Japanese it will display as 2011-08-01, where as an American input would show as 08/01/2011. Neither is very helpful if your user is English...

Always store dates in Date fields: it gets around this problem!


DateTimePicker2.Value = Format(CDate(rdr(5)), "dd/MM/yy")


有时,ms sql服务器上的日期格式与本地计算机上的系统日期格式不同.用途:
Sometimes, date format on ms sql server differs from system date format on local computer. Use:
SET DATEFORMAT ymd;
SELECT ...
FROM ...
WHERE (DateField=@aDate)


临时更改日期格式.


to temporary change date format.


这篇关于从数据库中选择vb.net日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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