0

我试图从数据库中显示我的数据。如果我今天存储了一些数据,仅此一项就应该显示在GridView.

我希望能够按周、月和年显示。

我的代码:

public void gettoday()
    {
        con.Open();
        {

            //string strview = "select MRNO,MaterialCode,Description,Specification,Category as Cat,Source as Sor,Population as Pop, StockInStores as Stock,MRRating as Rating,PreparedBy,PreparedDate,CheckedBy,CheckedDate,ApprovedBy,ApprovedDate  from tbl_KKSMaterialRaise where PreparedDate between DateAdd(day,-1,GetDate()) AND DateAdd(day,+1,GetDate())";
            string strview = "select MRNO,MaterialCode,Description,Specification,Category as Cat,Source as Sor,Population as Pop, StockInStores as Stock,MRRating as Rating,PreparedBy,PreparedDate,CheckedBy,CheckedDate,ApprovedBy,ApprovedDate  from tbl_KKSMaterialRaise where PreparedDate between (getdate()-1) and (getdate()+1) order by PreparedDate";
            SqlCommand cmd = new SqlCommand(strview, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
                //SqlDataReader reader = cd.ExecuteReader();
            }

            else
            {
                GridView1.DataSource = null;
                GridView1.DataBind();
            }

        }
        con.Close();


    }
4

1 回答 1

1

您只需要更改子句BETWEEN中的WHERE语句。现在你有:

where PreparedDate between (getdate()-1) and (getdate()+1)

一个星期,你需要使用这样的东西:

WHERE PreparedDate BETWEEN DATEADD(DAY, -7, GETDATE()) AND DATEADD(DAY, 1, GETDATE())

DATEADD(DAY, -7, GETDATE())您可以通过更改到适当的日期范围来 对月份和年份执行类似的操作。

一个月

DATEADD(MONTH, -1, GETDATE())

和一年:

DATEADD(YEAR, -1, GETDATE())
于 2012-07-19T13:19:59.457 回答