如何过滤>>从日期到日期?当前低于代码Im获得“未找到记录(S)”当我过滤。 [英] How To Filter>> From Date To Date? Current Below Code Im Get "No Record(S) Found" When I Filter.
问题描述
protected void Button7_Click(object sender, EventArgs e)
{
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM completeup WHERE Date_Time_Down BETWEEN @From AND @To", con))
{
using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
{
cmd.Parameters.AddWithValue("@From", Convert.ToDateTime(this.TextBox7.Text));
cmd.Parameters.AddWithValue("@To", Convert.ToDateTime(this.TextBox8.Text));
DataSet ds = new DataSet();
da.Fill(ds);
GridView2.DataSource = ds;
GridView2.EmptyDataText = "No Record(s) Found";
GridView2.DataBind();
}
}
}
}
推荐答案
从调试器开始,仔细看看你在两个文本框中有什么价值 - 请记住,BETWEEN假设第一个日期是最早的。
如果你的文本框是错误的方式 ,你不会得到任何价值:
Start with the debugger, and look at exactly what values you have in the two text boxes - remember that BETWEEN assumes that the first date is the earliest.
If your textboxes are the "wrong way round", you won't get any values:
SELECT * FROM MyTable
WHERE EnterDate BETWEEN '2015-11-01' AND '2015-11-05'
返回20个值,而
SELECT * FROM MyTable
WHERE EnterDate BETWEEN '2015-11-05' AND '2015-11-01'
返回无。
这是一个很好的例子,为什么你永远不应该使用Visual Studio默认名称 - 你可能还记得今天的TextBox6是手机号码,但是当你需要时在三周时间内修改它,你会吗?使用描述性名称 - 例如tbMobileNo - 您的代码变得更容易阅读,更自我记录,更易于维护 - 并且编码速度更快,因为Intellisense可以通过三次击键来tbMobile,其中TextBox8需要思考大概和8次击键...
Returns none.
This is a good example of why you should never use Visual Studio default names for anything - you may remember that "TextBox6" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
SELECT * FROM completeup
WHERE((@From和dateadd之间的Date_Time_Down(dd, 1,@ ToDate))或(@ FromDate = @ ToDate))
SELECT * FROM completeup
WHERE ((Date_Time_Down between @From and dateadd(dd,1,@ToDate)) or (@FromDate=@ToDate))
这篇关于如何过滤>>从日期到日期?当前低于代码Im获得“未找到记录(S)”当我过滤。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!