0

正在研究一种从学生数据库中提取学生成绩的方法,其中所有年份和班级的所有科目成绩都存储在一个表中,因此此函数将提取特定部门班级的结果并将其存储在数据表中数据集,所以这是我到目前为止所做的代码

public void Button1_Click(object sender, EventArgs e)
{
        con.Open();
        cmd.CommandText = "SELECT Course.Course_code, Course.Course_name, courseyear.level FROM Course INNER JOIN courseyear ON Course.Course_code = courseyear.Course_code WHERE (courseyear.dep = '" + dep.SelectedValue + "') AND (courseyear.subdep = '" + subdep.SelectedValue + "') AND (courseyear.year = '" + year.SelectedValue + "') AND (courseyear.level = '" + level2.SelectedValue + "')";
        da = new SqlDataAdapter(cmd.CommandText, con);
        ds.Clear();
        da.Fill(ds, "junk");
        cmd.CommandText = "SELECT Course.Course_code, Course.Course_name, courseyear.level FROM Course INNER JOIN courseyear ON Course.Course_code = courseyear.Course_code WHERE (courseyear.dep = '" + dep.SelectedValue + "') AND (courseyear.subdep = '" + subdep.SelectedValue + "') AND (courseyear.year = '" + year.SelectedValue + "') AND (courseyear.level = '" + level2.SelectedValue + "')";
        da = new SqlDataAdapter(cmd.CommandText, con);
        da.Fill(ds, "subs");
        ds.Tables.Add("res");
        cnum =  ds.Tables["junk"].Rows.Count;

        //l7de hna kweseen


        ds.Tables["res"].Columns.Add(new DataColumn("index",typeof(string)));
        ds.Tables["res"].Columns.Add(new DataColumn("name", typeof(string)));
        //int rnum = ds.Tables["res"].Rows.Count;
        for (int x = 0; x < cnum-1; x++) {

            ds.Tables["res"].Columns.Add(new DataColumn(ds.Tables["junk"].Rows[x].ItemArray[0].ToString(), typeof(string)));

        }//done inserting subjects 

        dr = ds.Tables["res"].NewRow();
        ds.Tables["junk"].Clear();
        cmd.CommandText = "SELECT id, name FROM Students WHERE (dep ='"+dep.SelectedValue+"') AND (class = '"+level2.SelectedValue+"') AND (subDep ='"+subdep.SelectedValue+"')";
        da = new SqlDataAdapter(cmd.CommandText, con);
        da.Fill(ds, "junk");
        int snum = ds.Tables["junk"].Rows.Count;
        for (int x = 0; x < snum; x++)
        {
            dr = ds.Tables["res"].NewRow();
            dr.ItemArray[0] = ds.Tables["junk"].Rows[x].ItemArray[0];
            dr.ItemArray[1] = ds.Tables["junk"].Rows[x].ItemArray[1];
            //ds.Tables["res"].Rows.Add(dr);
        }//done inserting students

        ShowPopUpMsg(ds.Tables["res"].Rows.Count.ToString());//for  testing
        ShowPopUpMsg(snum.ToString());// for testing
        for (int x = 0; x < cnum ;x++ )//for subjects
        {
            for (int y =0; y < snum;y++)//for students 
            {
                ds.Tables["junk"].Clear();
                cmd.CommandText = "select course_work+final_exam as exp from results where student_id = '" + ds.Tables["res"].Rows[y ].ItemArray[0] + "' and course_code = '"+ds.Tables["subs"].Rows[x]+"' ";
                da.Fill(ds, "junk");
                ds.Tables["res"].Rows[y].ItemArray[x] = ds.Tables["junk"].Rows[0].ItemArray[0];                   
            }
        }
        ShowPopUpMsg(ds.Tables["junk"].Rows[0].ItemArray[0].ToString());
    }
    private void ShowPopUpMsg(string msg)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("alert('");
        sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
        sb.Append("');");
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
    }
}</i>

我的问题是它一直告诉我在学生 for 循环的位置 0 处没有行

4

1 回答 1

0

您可能希望使用两个不同的数据适配器来填充 2 个不同的数据集。如果您查看它,您正在重新实例化数据适配器,这将重置先前加载的“垃圾”数据集。因此,当您尝试访问“垃圾”时,您什么也看不到。尝试不实例化或使用不同的适配器。

于 2013-04-01T17:06:38.330 回答