ACCESS数据中用的是短日期格式
OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath(@"~\QiXin.mdb"));
try
{
DateTime MyDate1 = Convert.ToDateTime(this.TextBoxStartDate.Text.ToString());
DateTime MyDate2 = Convert.ToDateTime(this.TextBoxEndDate.Text.ToString()); string OleDbstr = "select * from DaoDaXieChe where RiQi between'" + MyDate1.ToString("yyyy-mm-dd") + "'and '" + MyDate2.ToString ("yyyy-mm-dd") + "' order by RiQi";
//string OleDbstr = "select * from DaoDaXieChe where RiQi between '2007-7-6' and '2007-8-5' order by RiQi";
//OleDbcon = new OleDbConnection(strCon);
OleDbDataAdapter myda = new OleDbDataAdapter(OleDbstr, OleDbcon);
DataSet myds = new DataSet();
OleDbcon.Open();
myda.Fill(myds);
this.GridView1.DataSource = myds;
this.GridView1.DataBind();
}
catch (Exception error)
{
Response.Write("<script>alert('您输入的时间格式有误,请输入****-**-**格式的日期')</script>");
this.TextBoxStartDate.Text = "处理失败!原因为:" + error.ToString();
}
finally
{
//关闭数据库连接
OleDbcon.Close();
}
OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath(@"~\QiXin.mdb"));
try
{
DateTime MyDate1 = Convert.ToDateTime(this.TextBoxStartDate.Text.ToString());
DateTime MyDate2 = Convert.ToDateTime(this.TextBoxEndDate.Text.ToString()); string OleDbstr = "select * from DaoDaXieChe where RiQi between'" + MyDate1.ToString("yyyy-mm-dd") + "'and '" + MyDate2.ToString ("yyyy-mm-dd") + "' order by RiQi";
//string OleDbstr = "select * from DaoDaXieChe where RiQi between '2007-7-6' and '2007-8-5' order by RiQi";
//OleDbcon = new OleDbConnection(strCon);
OleDbDataAdapter myda = new OleDbDataAdapter(OleDbstr, OleDbcon);
DataSet myds = new DataSet();
OleDbcon.Open();
myda.Fill(myds);
this.GridView1.DataSource = myds;
this.GridView1.DataBind();
}
catch (Exception error)
{
Response.Write("<script>alert('您输入的时间格式有误,请输入****-**-**格式的日期')</script>");
this.TextBoxStartDate.Text = "处理失败!原因为:" + error.ToString();
}
finally
{
//关闭数据库连接
OleDbcon.Close();
}
access 对于日期查询是有格式要求的" and (Apply_Time >= #" + BeginTime + "# and Apply_Time <= #"+ EndTime +"#)";
还是不行呢
string OleDbstr = "select * from DaoDaXieChe where RiQi >= #" + MyDate1.ToString("yyyy-mm-dd") + "# and RiQi <= #" + MyDate2.ToString ("yyyy-mm-dd") + "# order by RiQi";试试
改成ToString ("yyyy-MM-dd")
保证 开始日期小于结束日期,date >= 小日期 and date <= 大日期你把日期字段改回去,改成 日期/时间 类型
但是还是没有数据太郁闷了
这段前台自动生成的就可以查
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/QiXin.mdb"
SelectCommand="SELECT * FROM [DaoDaXieChe] WHERE (([RiQi] >= ?) AND ([RiQi] <= ?))">
<SelectParameters>
<asp:Parameter DefaultValue="2007-7-14" Name="RiQi" Type="DateTime" />
<asp:Parameter DefaultValue="2007-7-19" Name="RiQi2" Type="DateTime" />
</SelectParameters>
比较时间就是这样了
where date>#2000-2-5# and date<#2008-5-2#