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

小弟遇到一个关于时间查询的问题,很是不解,希望高人解答,万分感谢

小弟遇到一个关于时间查询的问题,很是不解,希望高人解答,万分感谢
c#  access数据库
在对一个数据库进行按天查询的时候,总是实现不了
按照书上的写也不对,想来想去有可能是一个原因
书上的例子只有天,但是我的时间包括时分秒
在textbox1中,输入时间查询
SELECT name,age,time from biao where time=#" + TextBox1.Text.tostring + "# --------------------编程问答-------------------- 在线等。。。。。。。。。 --------------------编程问答-------------------- 顺便问下,经常看到说到园子里看看高手们写的经典例子,经典例子在什么地方呢?? --------------------编程问答-------------------- 。。。怎么办呢?。。。。 --------------------编程问答-------------------- 限制条件这样写 
"where time between '" + TextBox1.Text.tostring +"' and DateAdd(day,1,"+TextBox1.Text.tostring +")" --------------------编程问答-------------------- 制条件这样写 
"where time between '" + TextBox1.Text.tostring +"' and DateAdd(day,1,"+TextBox1.Text.tostring +")"

什么意思啊??还需要这样写吗??
--------------------编程问答-------------------- 急,在线................. --------------------编程问答-------------------- 1。
在对一个数据库进行按天查询的时候,总是实现不了
-----------------------------------
怎么实现不了?报错? 查不出数据?


2。
SELECT name,age,time from biao where time=#" + TextBox1.Text.tostring + "#
-----------------------------------
语句并没有错,确保数据库中具有你输入的时间的记录 --------------------编程问答-------------------- select   *   from   table1   where   出生年月   like   #3/21/1980#可以具体到天 --------------------编程问答-------------------- Jinglecat(晓风残月)
这位大哥帮帮忙
我的错误是查不出数据,并不抱错 --------------------编程问答-------------------- 数据库中有我输入的时间,可是就是出不来结果
数据库中的时间都是有 时分秒的
但是我输入查询的没有输入时分秒 --------------------编程问答-------------------- 救急 --------------------编程问答-------------------- "select name,age,time from biao where datediff(day,time,'"+TextBox1.Text.tostring()+"')=0" --------------------编程问答-------------------- 对了,time是关键字,所以规范的写法应该是
"select name,age,[time] from biao where datediff(day,[time],'"+TextBox1.Text.tostring()+"')=0" --------------------编程问答-------------------- 好的,试一下 --------------------编程问答-------------------- 不行啊,所有的方法都试了还是不行啊!!! --------------------编程问答-------------------- 谁来帮忙解决一下这个问题啊?? --------------------编程问答-------------------- 1.
用 > 或者 < 试试, 可能是精度的问题:

string sql = "SELECT name,age,time from biao where time > #" + TextBox1.Text.tostring + "#"

2.
贴出完整代码看看, 是否有其他忽略的问题? --------------------编程问答-------------------- using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Odbc;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TextBox1.Text = DateTime.Now.ToString("yyyy-MM-dd");       
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        OdbcConnection con = new OdbcConnection("DSN=haha");
        try
        {
            con.Open();
            DateTime chaxun = Convert.ToDateTime(TextBox1.Text.ToString().Trim());
            OdbcDataAdapter da = new OdbcDataAdapter("SELECT name,age,shijian from biao where shijian=#"+chaxun+"#", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
            Response.Write("<script language =javascript>alart('成功')</script>");
        }
        catch
        {
            Response.Write("<script language =javascript>alart('失败')</script>");
        }

    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
}
--------------------编程问答-------------------- 这就是所有代码了 --------------------编程问答-------------------- 调试程序的时候别用trycatch,不然怎么出错都不知道,应该不是查询语句本身的问题 --------------------编程问答-------------------- 精度问题啊, 直接使用 = , 会连同 时间 比较的 --------------------编程问答-------------------- SELECT name,age,convert(nvarchar(10),shijian,111) as colTime from biao where colTime=#"+chaxun+"#试一下这样可以吗?好像你的不符 --------------------编程问答-------------------- 1.
使用 FORMAT 试试

string sql = String.Format("SELECT name,age,shijian from biao where format(shijian, 'yyyy-mm-dd')=#{0}#", DateTime.Now.ToString("yyyy-MM-dd"));

注意: 格式符号 'yyyy-mm-dd' 一定要对应, 因为这里实际上是按字符串比价

2.
更倾向于使用 BETWEEN

DateTime begin = Convert.ToDateTime(TextBox1.Text.ToString().Trim());;
DateTime end = end.AddDays(1); // 偏移量按自己需求
string sql = String.Format("SELECT name,age,shijian from biao where shijian between #{0}# and #{1}#", begin.ToString(), end.ToString());

Hope helpful!

--------------------编程问答-------------------- like --------------------编程问答-------------------- 不要把简单的问题复杂化只是一个时间格式问题!难道还不行? --------------------编程问答-------------------- l_strWhere+="pld_dProducttionData=#"+Convert.ToDateTime(this.txtdPDate.Text.Trim()).ToShortDateString().ToString().Trim()+"#"; --------------------编程问答-------------------- 问题是。。。。。。。。。。。。还是不行 啊
查不出结果。。。。。。。。。。 --------------------编程问答-------------------- 无语, 我都行, 就你不行...... --------------------编程问答-------------------- 哈哈哈哈,解决了!!
DateTime begin = Convert.ToDateTime(TextBox1.Text.ToString().Trim());;
DateTime end = end.AddDays(1); // 偏移量按自己需求
应该是DateTime end = begin.AddDays(1); // 偏移量按自己需求b吧??
总算是能查了,但是还是要跟着小时,分秒
现在输入2007-06-07 14:23:78 能查出结果了,但是能不能不输入14:23:78 呢?

--------------------编程问答-------------------- 成功!!!!Jinglecat(晓风残月) 这位大哥!!太感谢你了!!!
你帮了我大忙了!!!
万分感谢!!!
怎么给你分啊?? --------------------编程问答-------------------- 但是能不能不输入14:23:78 呢?
-------------------
不输入日期的时候,

1.
系统会自动用默认日期值代替, Access 中好像是 1889-12-31

2.
所以, 你最好判断下, 如果没有输入日期, 手动加上当前的日期 --------------------编程问答-------------------- 点击页面顶部的 "管理" 进入揭帖

回复 | 推荐 | 收藏 | 专题 | 公告 | 管理  | 关闭窗口
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,