后台数据库中的数据表有一个日期字段,如何在C#中实现按日期进行范围性查找,但在日期比较时不对字段的时间部进行比较

解决方案 »

  1.   

    SELECT * FROM 表 where 日期字段 Between '2006-01-01' And '2006-06-29'取2006年1月1日0时0分0秒 到 2006年06月29日0时0分0秒之间的数据
      

  2.   

    简单:
    用个Conver函数,for instance:   1980 < Conver(Datetime).shortdate < 2006for detail ref:  ToShortDateString()
      

  3.   

    lz的意思大概是忽略时间部分吧?例如查询2006-6-1到2006-6-15之间的数据,“2006-06-15 03:03:03”要包含其中string sdate = this.Calendar1.SelectedDate.ToShortDateString();
    string edate = this.Calendar2.SelectedDate.ToShortDateString();
    System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection("连接字符串");
    System.Data.SqlClient.SqlCommand cm = new System.Data.SqlClient.SqlCommand();
    cm.Connection = cnn;
    cm.CommandText = "select * from table1 where thedate between @startdate and @enddate";
    cm.Parameters.Add("@startdate",System.Data.SqlDbType.VarChar);//用字符串类型
    cm.Parameters.Add("@enddate",System.Data.SqlDbType.VarChar);//用字符串类型
    cm.Parameters["@startdate"].Value = sdate;
    cm.Parameters["@enddate"].Value = ddate + " 23:59:59"; System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cm);
    DataTable dt = new DataTable();
    da.Fill(dt);
      

  4.   

    用timespan最简单        DateTime dt1 = DateTime.Now;
            DateTime dt2 = DateTime.Now.AddHours(3);
            TimeSpan ts = dt1 - dt2;
            if(ts.Days == 0)
              ....        if(ts.Days > 0)
              ....
            if(ts.Days < 0)
              ....
      

  5.   

    select
    orderTime = convert(varchar(10),orderTime,120)
    from orders
    可以把时间格式的具体时间去掉,不知道对你是否有用.
      

  6.   

    假设你想得到2006-3-15 到 2006-3-20的日期你可以写(1)
    select * from 表名 where 日期字段 between  to_date('15-3-2006 23:59:59', 'dd-mm-yyyy hh24:mi:ss')  and  to_date('20-3-2006 00:00:00', 'dd-mm-yyyy hh24:mi:ss')
    (2)把日期转换成字符串,然后截取,比较字符串还有using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                DateTime t1 = new DateTime(2006,2,2,1,1,1);
                DateTime t2 = new DateTime(2006,2,2,11,11,11);
                if (t1.Date == t2.Date)
                {
                    Console.WriteLine("true");            }
                else
                {
                    Console.WriteLine("false");
     
                }            Console.Read();                    }
        }
    }你可以看一下程序运行结果
      

  7.   

    赞同wht6411(华中科大,即将大四,寻求武汉工作机会)的方法