我现在用的时间控件取得的时间为:2010-1-1 赋值给变量 sj  我的MSSQL中的字段 INPUT_TIME 数据格式为: 2010-1-1 下午 15:00:00如果我想把我取得的 2010-1-1 作为查询条件,取得时间 2010-1-1 这天的数据,
select * from xx where INPUT_TIME ='"+sj+"'应该如何处理?  

解决方案 »

  1.   

    string sql="select * from xx where datediff(d,INPUT_TIME,'"+sj+"')=0"
      

  2.   

    的确查找应该是没有问题,但是却不能输出。
    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["conn"]);
            string ks = ksrq.Value;
            string sql = "select * from zj_leiji_mingxi where datediff(d,'"+ks+"',input_time)=0";
            SqlCommand command = new SqlCommand(sql, conn);
            conn.Open();
            SqlDataReader rs = command.ExecuteReader();
            if (rs.HasRows && rs.Read())
            {
                while (rs.Read())
                {
                    Response.Write("赠奖组别:" + rs["input_time"] );
                }
            }
            else
            {
                    Response.Write("没有相关赠奖记录!");
            }
                   
        }
    }
    现在我数据库里有2010-09-02 08:23:59 的数据 选中这一天,查找就没有输出。但是如果选择别的日期就输出 "没有相关赠奖记录!"有人知道其中原因吗?
      

  3.   

    select * from xx where substring(INPUT_TIME,1,8) ='"+sj+"'
      

  4.   

    调试没发现什么问题,应该是那天的找到了。但是没法输出 是我用DATAREADER的原因吗?
      

  5.   

    sj 不要直接 ''包围, 加上 cast('"+ sj+"' as datetime) 试一试
      

  6.   

    原因已经查明 if(hasrow) 就OK了 原来是 if(hasrow&&read())