ASP.NET  (C#)  通过form 获得了两个输入的日期, 格式为: 03/01/2011          string from = Request.Form["txtbox_PosAuthApproDate_from"];
          string to = Request.Form["txtbox_PosAuthApproDate_to"];
          DateTime fromd = DateTime.ParseExact(from, "MM/dd/yyyy", null); // 转换后 fromd= 2012/3/01 0:00:00
          DateTime tod = DateTime.ParseExact(to, "MM/dd/yyyy", null);将获得的string 转换成datetime 类型 ,然后从数据库Position 表中查找 时间处于 from 和 to 直接的数据 :
      
sql= " select id, time from  POSITION  where  time between "+fromd+ " and "+ to;报错sql=" select  DISTINCT  POSITION_NBR , INACTIVE_EFFECTIVE_DT  from  POSITION  where ( INACTIVE_EFFECTIVE_DT  between to_date('"+fromd+ "','YYYY/MM/DD HH24:MIS') and  to_date('"+to+ "' ,'YYYY/MM/DD HH24:MIS') )" ; 也报错。求指教! 谢谢!

解决方案 »

  1.   

    fromd= '2012/3/01 0:00:00'       'YYYY/MM/DD HH24:MIS'自己一个字一个字对,能对得上吗?对不上当然报错。
      

  2.   

    把读入的字符串直接用呗
    sql="select DISTINCT POSITION_NBR , INACTIVE_EFFECTIVE_DT from POSITION where INACTIVE_EFFECTIVE_DT between to_date("+from+ ",'MM/dd/yyyy') 
    and to_date("+to+" ,'MM/dd/yyyy')" ;
      

  3.   

    请参考下面的代码sql="select DISTINCT POSITION_NBR , INACTIVE_EFFECTIVE_DT from POSITION where INACTIVE_EFFECTIVE_DT between to_date(" + fromd + ", 'MM/dd/yyyy') and to_date(" + tod + " ,'MM/dd/yyyy')" ;
      

  4.   

    谢谢各位提示, 最后改成这样就对了:sql = "select  DISTINCT  POSITION_NBR , INACTIVE_EFFECTIVE_DT  from  POSITION  where INACTIVE_EFFECTIVE_DT  between to_date('2010/8/14 0:00:00','yyyy/MM/dd HH24:MI:SS') and  to_date('2012/3/12 0:00:00' ,'yyyy/MM/dd HH24:MI:SS') ";