循环操作时,DataReader老是出错,没有关闭--在线等……

类别:Asp 点击:0 评论:0 推荐:

我要用while循环进行操作,而下一个DataReader的创建又用到上一个DataReader中的值,这样如果关闭上一个连接的话,下一个连接就无法创建,但不关闭的话,又创建不了下一个DataReader,高手快帮下忙

我的代码如下:

constr="Server=(local);database=university;uid=sa;pwd=153426";
con1=new SqlConnection(constr);
con1.Open();
con2=new SqlConnection(constr);
con2.Open();
con3=new SqlConnection(constr);
con3.Open();
sql1="select school_area_cname,school_area_ename from tb_school_area";
mycmd1=new SqlCommand(sql1,con1);
myreader1=mycmd1.ExecuteReader();
if(myreader1.HasRows)
{
  Response.Write("<table border=1>");  //表格1 表头开始 
   while(myreader1.Read())              //表格1 行1  开始  打印表1 的名称
    {Response.Write("<tr><td colspan=2>"+myreader1["school_area_cname"].ToString().Trim()+"</td></tr>");
     sql2="select school_cname,school_ename from tb_school_area_name where school_area_ename='"+myreader1["school_area_ename"].ToString().Trim()+"'";
     mycmd2=new SqlCommand(sql2,con2);  //以下两个Reader会在上个Reader执行后获取条件吧,在这里写可以吗
     //myreader1.Close();            //关闭Reader1  以供下面的循环使用  但就出错了
     myreader2=mycmd2.ExecuteReader();
       if(myreader2.HasRows)
 {Response.Write("<tr><td>&nbsp;</td><td><table border=1>"); //表格2 表头开始 开头空一格
   while(myreader2.Read())                            //表格1 行1  开始   打印表2的名称
     {Response.Write("<tr><td>|_</td><td>"+myreader2["school_name"].ToString().Trim()+"</td></tr>");
             sql3="select school_ccontenttype,school_econtenttype from tb_school_area_name_all where school_ename='"+myreader2["school_ename"].ToString().Trim()+"'";
              mycmd3=new SqlCommand(sql3,con3);
       //myreader2.Close();          //关闭Reader3以供下面的Reader使用
              myreader3=mycmd3.ExecuteReader();
  if(myreader3.HasRows)
     {Response.Write("<tr><td>&nbsp;</td><td><table>");
        while(myreader3.Read())
           {Response.Write("<tr><td>|_</td><td>"+myreader3["school_ccontenttype"].ToString().Trim()+"</td></tr>");
    }
                     Response.Write("</table>");
       //myreader2=mycmd2.ExecuteReader();想重新执行但也不行
      }
      }
    Response.Write("</table>");
           //myreader1=mycmd1.ExecuteReader();
   }
     }
    Response.Write("</table>"); 

本文地址:http://com.8s8s.com/it/it7165.htm