数据库中表字段 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);  //形式: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 + "'";

 

相关文章:

  • 2021-08-03
  • 2021-12-08
  • 2022-12-23
  • 2022-12-23
  • 2021-05-20
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-27
  • 2021-10-16
  • 2022-12-23
  • 2021-11-21
  • 2022-12-23
  • 2021-12-03
相关资源
相似解决方案