当前位置:编程学习 > C#/ASP.NET >>

如何查询日期之间的数据?

做了一个题库支领的小程序,我想查询日期之间的数据
日期是 2012-08-10 2012-12-30 机构名称 大自然
8月2日的信息也出来了 应该只出现 8月10日 至 12月30日 之间的数据
[img=http://pan.baidu.com/share/link?shareid=185138&uk=4010532994][/img]

String str = "select * from 题库支出 where 支领日期>=@abc and 支领日期<=@bcd  and 机构名称=@ccc";
            OleDbDataAdapter da = new OleDbDataAdapter(str, sp.conn());

            da.SelectCommand.Parameters.Add("@abc", OleDbType.Date, 80).Value = DateTime.ParseExact("2012-08-10", "yyyy/mm/dd", System.Globalization.CultureInfo.CurrentCulture);
            da.SelectCommand.Parameters.Add("@bcd", OleDbType.Date, 80).Value = DateTime.ParseExact("2012-12-30", "yyyy/MM/dd", System.Globalization.CultureInfo.CurrentCulture);
            da.SelectCommand.Parameters.Add("@ccc", OleDbType.VarChar, 80).Value = "大自然";

            da.Fill(ds, "题库支出");

            dataGridViewX1.DataSource = ds.Tables["题库支出"];
  sql 日期 --------------------编程问答-------------------- 时间的比较格式错了应该是between 日期1 and 日期2 --------------------编程问答-------------------- 我来补充一下,我想查询 2012-08-10 and 2012-12-10 and 机构名称=大自然
但是查询出来结果不对 8月2日的信息也出来了
--------------------编程问答--------------------

using (SqlConnection conn = new SqlConnection(connectionString))
            {
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                sda.SelectCommand.Parameters.Add("@start", SqlDbType.Date, 80).Value = "2012-08-10";
                sda.SelectCommand.Parameters.Add("@end", SqlDbType.Date, 80).Value = "2012-12-30";
                DataSet ds=new DataSet();
                sda.Fill(ds,"dt1");

                if (ds.Tables.Count > 0)
                    dt = ds.Tables["dt1"];
            }
--------------------编程问答--------------------
   if (!txtDateB.Text.Trim().Length > 0)
        {
            sql += string.Format("and (date >= '{0}')" , txtDateB.Text.Trim());//选取录入日期大于查询日期的数据
        }
        if (!txtDateE.Text.Trim().Length > 0)
        {
            sql += string.Format("and (date <= '{0}')", DateTime.Parse(txtDateE.Text.Trim()).ToString("yyyy-MM-dd") + " 23:59:59");//选取录入日期小于查询日期的数据
        }
--------------------编程问答-------------------- select * from 题库支出 where 支领日期 between '2012-08-10' and '2012-12-30' and 机构名称='大自然';
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,