0

期待您对以下代码提出建议的朋友。不是采用 dtr1(DatatableReader),而是采用外部循环 dtr(DataTableReader) 表。

protected void GetStudReport(Object o, EventArgs e)
{
    if (mycon.State != ConnectionState.Open)
    {
        List<string> lstQstn = new List<string>();

        mycon.Open();
        cmd = new MySqlCommand("SELECT * from scord_mark_table where stu_ID='" + drpDnSearch3.SelectedValue + "'", mycon);
        MySqlDataReader rdr1=cmd.ExecuteReader();
        DataSet ds=new DataSet();
        DataTable dtScrTbl=new DataTable();
        dtScrTbl.Load(rdr1);
        ds.Tables.Add(dtScrTbl);
        rdr1.Close();
        cmd = null;

        int i = 0;
        Dictionary<string, string> dctSub = new Dictionary<string, string>();
        **using (DataTableReader dtr = ds.CreateDataReader())**
        {
            while (dtr.Read())
            {
                lstQstn.Add(dtr["test_id"].ToString());

                while (i <= lstQstn.Count())
                {
                    MySqlCommand cmd2 = new MySqlCommand("SELECT test_id,subject_id from qution_no_table where test_id='" + lstQstn[i].ToString() + "'", mycon);
                    MySqlDataReader rdr2 = cmd2.ExecuteReader();
                    DataTable dtQsNoTbl = new DataTable();
                    dtQsNoTbl.Load(rdr2);
                    ds.Tables.Add(dtQsNoTbl);

                    **using (DataTableReader dtr1 = ds.CreateDataReader())** 
                    {

                        while (dtr1.Read())
                        {
                            dctSub.Add(dtr1["test_id"].ToString(), dtr1["subject_id"].ToString());    // **here it is taking table scord_mark_table instead of dtr1's qution_no_table** 
                        }
                        rdr2.Close();
                        break;
                    }
                }
                //cmd2 = null;
                i++;


            }

        }
4

1 回答 1

1

不,它不是 - 它返回一个新的DataTableReader挂钩到所有现有表(包括您在第一个循环之前创建的表)。

尝试指定DataTable您希望读者附加到的:

using (DataTableReader dtr = ds.CreateDataReader(dtScrTbl))

using (DataTableReader dtr1 = ds.CreateDataReader(dtQsNoTbl))
于 2012-11-02T21:01:52.350 回答