2012-07-19 52 views
0

我試圖從數據庫中顯示我的數據。如果我今天保存了一些數據,那麼應該在GridView中顯示。如何在一天或一週或一個月或一年之間查詢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(); 


    } 
+1

我認爲這個問題是實際有關SQL查詢的日期範圍看是否[SO搜索(HTTP://計算器。 com/search?q = sql + command + for + date + range)幫助 – 2012-07-19 04:20:25

+0

據我所知,「(getdate() - 1)和(getdate()+ 1)之間」顯示今天的記錄,是嗎? – 2012-07-19 04:28:20

+0

我同意@MarkHall,GridView將顯示你綁定它的任何東西。這是關於SQL選擇過濾的。 – 2012-07-19 04:31:34

回答

0

你只需要改變BETWEEN語句進行WHERE條款。現在您有:

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

一個星期,你需要使用的東西是這樣的:通過改變DATEADD(DAY, -7, GETDATE())到適當的日期範圍

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

你可以做的年份和月份類似的事情。

月分

DATEADD(MONTH, -1, GETDATE()) 

而對於一年:

DATEADD(YEAR, -1, GETDATE()) 
相關問題