如何从动态日期范围中选择数据 [英] How do i select data from a dynamic date range

查看:77
本文介绍了如何从动态日期范围中选择数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码;我想从动态日期范围中搜索,从数据库中选择开始范围,结束范围将是该开始范围的3个月后的日期.

here is my code; i want to search from a dynamic date range where start range is selected from my DB and end range will be the date after 3 month of that starting range.

string sqlquery6 = "SELECT * FROM Bill_Details where PCID= ''"+PCNo+"'' order by Bill_Date";
            SqlDataAdapter adp6 = new SqlDataAdapter(sqlquery6, conn);
            DataTable tbl6 = new DataTable();
            adp6.Fill(tbl6);
            if (tbl6.Rows.Count > 0)
            {
                DateTime startrange = Convert.ToDateTime(tbl6.Rows[0][2].ToString());
                DateTime endrange = Convert.ToDateTime(startrange + 90);
                string sqlquery3 = "SELECT * FROM Bill_Details WHERE Bill_Date BETWEEN ''" + startrange + "'' AND ''" + endrange + "'' and PCID= ''" + PCNo + "''";
                SqlDataAdapter adp3 = new SqlDataAdapter(sqlquery3, conn);
                DataTable tbl3 = new DataTable();
                adp3.Fill(tbl3);
                if (tbl3.Rows.Count > 0)
                {
                    int totbill = 0;
                    for (int i = 0; i < tbl3.Rows.Count; i++)
                    {
                        totbill = totbill + Convert.ToInt32(tbl3.Rows[i][0].ToString());
                    }

                    txttot.Text = totbill.ToString();
                }
                else
                {
                    txttot.Text = "0"; ;
                }
            }

推荐答案

请参阅此

今天的System.DateTime = System.DateTime.Now;
System.DateTime答案= today.AddDays(36);
System.Console.WriteLine("{0:dddd}",answer);

来自: http://msdn.microsoft.com/en-us/library/system. datetime.adddays.aspx [ ^ ]
see this

System.DateTime today = System.DateTime.Now;
System.DateTime answer = today.AddDays(36);
System.Console.WriteLine("{0:dddd}", answer);

from : http://msdn.microsoft.com/en-us/library/system.datetime.adddays.aspx[^]


以这种方式修改您的查询
Modify your query in such fashion
"SELECT * FROM Bill_Details WHERE Bill_Date BETWEEN CONVERT(DATETIME,'" + startrange + "',103) AND CONVERT(DATETIME,'" + endrange  + "',103) and PCID= '" + PCNo + "'";



它将在您的查询中为您提供预期的结果



It will give you the expected result


,对于使用相同变量的范围..
in your query both for range you have used the same variable..
string sqlquery3 = "SELECT * FROM Bill_Details WHERE Bill_Date BETWEEN ''" + startrange + "'' AND ''" + endrange  + "'' and PCID= ''" + PCNo + "''";


这篇关于如何从动态日期范围中选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆