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();
}
我認爲這個問題是實際有關SQL查詢的日期範圍看是否[SO搜索(HTTP://計算器。 com/search?q = sql + command + for + date + range)幫助 – 2012-07-19 04:20:25
據我所知,「(getdate() - 1)和(getdate()+ 1)之間」顯示今天的記錄,是嗎? – 2012-07-19 04:28:20
我同意@MarkHall,GridView將顯示你綁定它的任何東西。這是關於SQL選擇過濾的。 – 2012-07-19 04:31:34