我如何从表中获得最新的6行限制

时间:2013-02-11 10:08:42

标签: c#

我正在尝试从我的数据库表中检索最新的6行,我使用的是最大值,但是如何使用 我从表中获得限制的6行,并检索用于for循环的结果,以在面板中显示超链接

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();

1 个答案:

答案 0 :(得分:2)

select top 6 title from up_song order by song_id desc