数据库中表字段 pubdate ,如设置初始默认值为 getdate() ,则最后产生的日期为:2006-10-17 或 2006-01-06 这样的格式数据。
-----------------------------
假如做查询,从前台取过来的日期 enddate 为 "2006-10-17" ,注意此日期无时,分秒。
如果想要做一个查询 ,把表中数据所有小于等于 2006-10-17 日期的数据筛选出来,如果查询语句这样写的话:
select * from 表 where pubdate<='"+enddate+"' ,则只能取出 2006-10-17 日以前的数据,2006-10-17当日的数据出不来,因为 2006-10-17 相当于 2006-10-17 00:00:00
解决方法:
DateTime dtn = DateTime.Parse(this.TextBox2.Text.Trim().ToString());
1 where pubdate<= '"+dtn.AddDays(1).ToShortDateString()+"'
2 where convert(varchar(10),DateAndTime,120) = '" +this.TextBox2.Text.Trim().ToString()+ "'
DateTime dt
=
DateTime.Now;
string
dts
=
dt.ToShortDateString();
//
形式:2011-9-21 ,注意,是9 而不是 09 ,不能用这个字符串与数据库日期进行比较。
DateTime dtf
=
DateTime.Parse(dts); //形式:2011-9-21 0:00:00 会自动把时间加上,但是全是 0
string
dtsm
=
dt.ToShortTimeString();
//
形式:8:41 ,8:01 ,是8 不是 08:41,但是后面的分钟是两位数的。
int
hm
=
int
.Parse(dt.Hour.ToString())
*
60
+
int
.Parse(dt.Minute.ToString());
string
vnum
=
"
V
"
+
((hm
-
(hm
%
15
))
/
15
).ToString();
int
nowyear
=
dt.Year;
//
2011
int
nowmonth
=
dt.Month;
//
9 不是 09
int
nowday
=
dt.Day;
//
3 不是 03
//
数据库中 datepart(month, DateAndTime) 也为 9 不是 09
//
数据库中 datepart(day, DateAndTime) 也为 3 不是 03
//
select convert(varchar(10),提交日期,120) from adslmoney 得到的提交日期为:2010-05-04 月份和日子,都是双位数 05-04
string
DateFormats
=
Convert.ToDateTime(dt).ToString(
"
yyyy-MM-dd
"
);
//
但是这样的话,月份和日子,就是双位数2011-09-21
//
string sqlcheck = "select ID from HeatLine where PID = '" + pipeid + "' and datepart(year, DateAndTime) = " + nowyear + " and datepart(month, DateAndTime) = " + nowmonth + " and datepart(day, DateAndTime) = " + nowday + "";
string
sqlcheck
=
"
select ID from HeatLine where PID = '
"
+
pipeid
+
"
' and convert(varchar(10),DateAndTime,120) = '
"
+
DateFormats
+
"
'
"
;