简体   繁体   English

如何使用c#.net将行索引用作数据集索引

[英]how to use row index as dataset index using c#.net

I am newbie to c# I am working on project i am trying to loop through data table containing distinct values and my database has song id like:1,2,3,4,6,8,9,10 but dataset takes this values as 0,1,2,3,4,5,6,7 respectively... thanks 我是新手c#我正在研究项目我试图循环包含不同值的数据表,我的数据库有歌曲ID如:1,2,3,4,6,8,9,10但数据集将此值视为0,1,2,3,4,5,6,7分别...谢谢

String sql = "select  title, song_id from up_song where Song_type='Mp3 Tracks' ";
    adpt = new SqlDataAdapter(sql, cn);
    ds = new DataSet();

    adpt.Fill(ds, "title");
    var maxvalue = ds.Tables["title"].AsEnumerable().Max(x => x.Field<int>("song_id"));
    var minvalue = ds.Tables["title"].AsEnumerable().Min(x => x.Field<int>("song_id"));
    for (i =maxvalue; i >= minvalue; --i)
        {
            try
            {
                hyperlink[i] = new HyperLink();
                hyperlink[i].ID = "hyperlink" + i;
                hyperlink[i].Text = ds.Tables["title"].Rows[i].ItemArray[0].ToString();
                hyperlink[i].NavigateUrl = "Downloadpage.aspx";
                hyperlink[i].ForeColor = System.Drawing.Color.White;
                Panel1.Controls.Add(hyperlink[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);
            }
        }

You are using the loop variable to acess the rows in the DataTable here: 您正在使用循环变量来访问DataTable的行:

coo["sogtit"] = ds.Tables["title"].Rows[i].ItemArray[0].ToString();

But the variable is initialized from the min and max ID values of your song_id . 但是变量是从song_idminmax ID值song_id

I don't know why you need these values at all, why don't you loop the DataRows : 我不知道为什么你需要这些值,为什么不循环DataRows

foreach(DataRow row in ds.Tables["title"].Rows)
{
    // ...
    int songID = row.Field<int>("song_id")
    Hyperlink hl = new HyperLink(); // you don't need the array of hyperlinks neither
    hl.ID = "hyperlink" + songID;
    string title = row.Field<string>("title);
    hl.Text = title;
    coo["sogtit"] = title;
    Panel1.Controls.Add(hl);
    // ...
}

Update 更新

i want to access those latest upload song so i use for loop and index as min and max values. 我想访问那些最新的上传歌曲,所以我用循环和索引作为最小值和最大值。 i mean want to access latest uploaded minimum 6 song 我的意思是想要访问最新上传的最少6首歌曲

You could use Linq to get the last 6 uploaded songs: 您可以使用Linq获取最近6首上传的歌曲:

var last6Uploaded = ds.Tables["title"].AsEnumerable()
    .OrderByDescending(r => r.Field<int>("song_id"))
    .Take(6);

foreach(DataRow row in last6Uploaded)
{
    // ...
}

Note that you should use a DateTime field instead of the primary-key. 请注意,您应该使用DateTime字段而不是主键。

The array index ( i ) and the value of song_id should have nothing to do with each other. 数组索引( i )和song_id的值应该彼此无关。 What if your song_id started at 1000? 如果你的song_id从1000开始怎么办? Or if your database indexed by song_id in descending order? 或者,如果您的数据库按song_id降序排列?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM