获取列C#SQL的第二个值

时间:2018-08-23 07:16:11

标签: c# sql winforms

我正在研究成绩提交模块。如何获取表中列的第二行并将其放入label1,第三行将其放入label2,依此类推?

示例:

studentID | Subjects
 12345    | Mathematics 
 12345    | Science
 12345    | History

我只想获取列主题的第二行,即“科学”和“历史记录”,并将其放入标签中并保存到新表中。因为我总是得到列的第一行。

这是我的代码

string sql = "Select * from tbl_Subject where studentID like '" + studID.Text + "'";
cm = new SqlCommand(sql, cn);
dr = cm.ExecuteReader();
while (dr.Read())
{
    Subject.Text = dr["Subjects"].ToString();
}
dr.Close();

提前谢谢!

1 个答案:

答案 0 :(得分:1)

ROW_NUMBER()可用于跳过整行,如下所示:

string sql = "select * from (Select  ROW_NUMBER() OVER(ORDER BY Subjects) AS RoNum ,* from tbl_Subject) std where std.studentID like @Search  and std.RoNum > 1 ";
cm = new SqlCommand(sql, cn);
 cm .Parameters.AddWithValue("@Search", "%" + studID.Text + "%"); 
dr = cm.ExecuteReader();
while (dr.Read())
{
    Subject.Text = dr["Subjects"].ToString();
}
dr.Close();

您可以使用std.RoNum > 1跳过多达第几行,并按行OVER(ORDER BY Subjects)