我数据中的日期格式是2008-2-26   21:00:24这种,但是我dateTimePicker获取的值是2008-2-26这种,如何把2008-2-26这个时间的记录查询出来?能否给个示例代码?谢谢,麻烦了

解决方案 »

  1.   

    select * from table1 where DATEDIFF(day, yourdatetimecol, '2008-2-26') = 0
      

  2.   

    select * from table1 where Date(DateTimeField) = '2008-2-26'
      

  3.   

    string requestdate = dateTimePicker1.Value.ToShortDateString();
                //MessageBox.Show(requestdate);
                string SQLstr = "select * from publicinfo where Date(infodate)='" + requestdate + "'";
    这样写好像不对,错在哪里?正确写法怎么写呵呵,不好意思,麻烦了
      

  4.   

    string SQLstr = string.Format("select * from publicinfo where DATEDIFF(day, yourdatetimecol, '{0:yyyy-MM-dd}') = 0", dateTimePicker1.Value);
      

  5.   

    如果不想使用DATEDIFF()函数,这样写也可以:
    string SQLstr = string.Format("select * from publicinfo where infodate between '{0:yyyy-MM-dd}') and '{0:yyyy-MM-dd} 23:59:59'", dateTimePicker1.Value);
      

  6.   

    SELECT * FROM TABLE WHERE REPLACE(CONVERT(VARCHAR(10),DATECOL,120),N'-0','-')='2008-2-26'
      

  7.   

    更正一下,上面多了个)
    string SQLstr = string.Format("select * from publicinfo where infodate between '{0:yyyy-MM-dd}' and '{0:yyyy-MM-dd} 23:59:59'", dateTimePicker1.Value);
      

  8.   

    很不幸,wuyi8808你的代码没查询出来呵呵
      

  9.   

    Sorry,你试试这个
    select * from table1 where DateValue(DateTimeField) = '2008-2-26'            string requestdate = dateTimePicker1.Value.ToShortDateString();
                //MessageBox.Show(requestdate);
                string SQLstr = "select * from publicinfo where DateValue(infodate)='" + requestdate + "'";
      

  10.   

    void Dtp1Event(objetc o, EventArgs e)
    {
      string SQLstr = string.Format("select * from publicinfo where DATEDIFF(day, infodate, '{0:yyyy-MM-dd}') = 0", ((DateTimePicker)o).Value);
      // ...
    }
      

  11.   

    如果有问题,你可以先在查询分析器里执行看看:
    select * from publicinfo where DATEDIFF(day, infodate, '2008-02-26') = 0
      

  12.   

    我的整个代码是这样写的,看看对不对
    public void dateTimePicker1_ValueChanged(object sender, EventArgs e)
            {
                System.Data.OleDb.OleDbDataReader dr;
                string requestdate = dateTimePicker1.Value.ToShortDateString();
                //MessageBox.Show(requestdate);
                //string SQLstr = "select * from publicinfo where DateValue(infodate)='" + requestdate + "'";
                string SQLstr = string.Format("select * from publicinfo where DATEDIFF(day, infodate, '{0:yyyy-MM-dd}') = 0", dateTimePicker1.Value);            dr = new server.control.ClassOptionData().ExSQLReDr(SQLstr);
                ShowOldMessage som = new ShowOldMessage();
                if (dr.Read())
                {
                    while (dr.Read())
                    {
                        MessageBox.Show(dr["info"].ToString());
                    }
                }
            }
      

  13.   

                if (dr.Read())
                {
                    while (dr.Read())
                    {
                        MessageBox.Show(dr["info"].ToString());
                    }
                }
    改成:                while (dr.Read())
                    {
                        MessageBox.Show(dr["info"].ToString());
                    }就是说,把 if (dr.Read()) { ... } 去掉。
      

  14.   

    老大,试过了,返回信息是
    服务器: 消息 208,级别 16,状态 1,行 1
    对象名 'publicinfo' 无效。