如何从datarow获取前四个值以及如何索引每个数据行值

时间:2017-05-24 13:03:03

标签: c# sqlite

如何从datarow获取前四个值,因为我已经完成了选择部分,但是如何索引每个数据行值,以便我可以使用每个Teacher_Name字符串。我尝试过以下代码,但不起作用。请帮忙吗?感谢。

        cmd.CommandText = "SELECT DISTINCT Teacher_Name from " + myTeacher + " Order by Sum_Weekly_Credit desc LIMIT 4";

        DataTable dt = new DataTable();
        SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
        da.Fill(dt);

        string[] FourTeachers = new string[dt.Rows.Count];
        int i = 0;
        foreach (DataRow dr in dt.Rows)
        {
            FourTeachers[i] = dr["Teacher_Name"].ToString();
            string FT0 = FourTeachers[0];
            string FT1 = FourTeachers[1];
            string FT2 = FourTeachers[2];
            string FT3 = FourTeachers[3];
            //                MessageBox.Show("" + Convert.ToInt32(dr["Teacher_Name"].ToString()));
            i++;
        }

        for (int j = 1; j < 21; j++)
        {
            string FT0 = FourTeachers[0];
            string FT1 = FourTeachers[1];
            string FT2 = FourTeachers[2];
            string FT3 = FourTeachers[3];
            cmd2.CommandText = "SELECT FT0, FT1, FT2, FT3 FROM group_even_" + j + " Order by Sum(Weekly_Credit) desc";

            DataTable dt2 = new DataTable();
            SQLiteDataAdapter da2 = new SQLiteDataAdapter(cmd2);
            da2.Fill(dt2);
            int FT = dt2.Rows.Count;

1 个答案:

答案 0 :(得分:0)

那你为什么不改变SQL查询本身,如下面[假设SQL Server]

cmd.CommandText = "SELECT DISTINCT TOP 4 Teacher_Name from " + myTeacher + " Order by Sum_Weekly_Credit desc"