2012-07-18 54 views
0

我試圖顯示日期明智的數據,但我沒有得到答案准確,請幫助我..錯誤的數據顯示按日期電網明智

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 (getdate()-1) and (getdate()+1)"; 
      SqlCommand cd = new SqlCommand(strview, con); 
      SqlDataReader reader = cd.ExecuteReader(); 

      GridView1.DataSource = reader; 
      GridView1.DataBind(); 
     } 
     con.Close(); 


    } 
+0

什麼是錯誤?你期望什麼? – rene 2012-07-18 11:11:50

+0

那個朋友沒有錯誤。我想顯示數據從數據庫到明智的日期到網格視圖(例如:如果我今天提交了一個數據,那麼它會在昨天或今天的標題下顯示,如明智的周,月等 – user1455232 2012-07-18 11:18:32

回答

1

SQL查詢似乎是不正確。所以你想在昨天和明天之間選擇記錄。

那麼這應該工作(假定SQL-Server因爲你使用GetDate):

WHERE PreparedDate BETWEEN DateAdd(d,-1,GetDate()) AND DateAdd(d,+1,GetDate()) 

編輯:除此之外,你應該總是使用using-statement(實施任何對象IDisposable)以確保連接即使在例外的情況下也能關閉:

const 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(d,-1,GetDate()) AND DateAdd(d,+1,GetDate())"; 
// don't use global connection objects 
using(var con = new SqlConnection(connectionString)) 
using(var cmd = new SqlCommand(strview, con)) 
{ 
    con.Open(); 
    GridView1.DataSource = cmd.ExecuteReader(); 
    GridView1.DataBind(); 
} 
+0

現在還沒有得到答案,它只是簡單地顯示空白空間 – user1455232 2012-07-18 11:31:30

+0

@ user1455232:你調試過代碼嗎?你檢查過sql是否在SQL-Server Management Studio中工作,它返回什麼? – 2012-07-18 11:35:00