2013-02-11 52 views
0

我想從我的數據庫表中檢索最新的6行,我正在使用最大值,但是如何從表中獲取6行,並且如何獲取用於for循環的檢索結果,顯示面板的超鏈接 我如何從表中獲取最新的6行

protected void Page_Load(object sender, EventArgs e) 
{ 

       String sql = "select title from up_song "; 
       SqlDataAdapter adpt = new SqlDataAdapter(sql, cn); 
       DataSet ds = new DataSet(); 
       adpt.Fill(ds, "title"); 
       if (ds.Tables["title"].Rows.Count > 0) 
       { 
        int m = ds.Tables["title"].Rows.IndexOf(ds.Tables["title"].Rows[8]); 
        int k = ds.Tables["title"].Rows.IndexOf(ds.Tables["title"].Rows[1]); 

        for (i=m; i >= k ; --i) 
        { 
         try 
         { 
          hp[i] = new HyperLink(); 
          hp[i].ID = "hp" + i; 
          hp[i].Text = ds.Tables["title"].Rows[i].ItemArray[0].ToString(); 
          hp[i].NavigateUrl = "Downloadpage.aspx"; 
          hp[i].ForeColor = System.Drawing.Color.White; 
          Panel1.Controls.Add(hp[i]); 
          Panel1.Controls.Add(new LiteralControl("<br>")); 
          HttpCookie coo = new HttpCookie("song"); 
          coo["sogtit"] = ds.Tables["title"].Rows[i].ItemArray[0].ToString(); 
          Response.Cookies.Add(coo); 
         } 
         catch(Exception ex) 
         { 
          Response.Write(ex.Message); 
         } 
        } 
       } 

       String sql1 = "select title from up_song where Song_type='Indian Pop Album'"; 
       SqlDataAdapter adpt1 = new SqlDataAdapter(sql1, cn); 
       DataSet ds1 = new DataSet(); 
       adpt1.Fill(ds1, "title"); 


       if (ds1.Tables["title"].Rows.Count > 0) 
       { 
        String query = "select max(song_id) from up_song;"; 
        SqlDataAdapter adpt2= new SqlDataAdapter(query,cn); 
        DataSet ds2= new DataSet(); 
        adpt2.Fill(ds2,"max"); 


        //int m = ds.Tables["title"].Rows.IndexOf(ds1.Tables["title"].Rows[query]); 
        //int k = ds.Tables["title"].Rows.IndexOf(ds.Tables["title"].Rows[1]); 

        for (i = 0; i <= ds2.Tables["max"].Rows.Count; ++i) 
        { 
         try 
         { 
          hp[i] = new HyperLink(); 
          hp[i].ID = "hp" + i; 
          hp[i].Text = ds1.Tables["title"].Rows[i].ItemArray[0].ToString(); 
          hp[i].NavigateUrl = "Downloadpage.aspx"; 
          hp[i].ForeColor = System.Drawing.Color.White; 
          Panel2.Controls.Add(hp[i]); 
          Panel2.Controls.Add(new LiteralControl("<br>")); 
          HttpCookie coo = new HttpCookie("song"); 
          coo["sogtit"] = ds.Tables["title"].Rows[i].ItemArray[0].ToString(); 
          Response.Cookies.Add(coo); 
         } 
         catch (Exception ex) 
         { 
          Response.Write(ex.Message); 
         } 
        } 

       } 
       cn.Close(); 
+2

你想在過去的6行,當您訂購由什麼數據?最高歌曲ID? – 2013-02-11 10:10:20

+1

使用描述性名稱,而不是'm','k','i','ds','hp [i]','ds'。維護變得很混亂 – 2013-02-11 10:15:20

回答

2
select top 6 title from up_song order by song_id desc 
+1

我要寫同樣的,但既然你已經做了,upvote會做:) – 2013-02-11 10:11:59