无法读取另一个DataTableReader中的DataTableReader

时间:2012-11-02 20:32:41

标签: c# ado.net

朋友们希望您的建议符合以下代码。它不是采用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++;


            }

        }

1 个答案:

答案 0 :(得分:1)

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

尝试指定您希望阅读器附加到的DataTable

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

using (DataTableReader dtr1 = ds.CreateDataReader(dtQsNoTbl))