为什么我的while循环取出的值不是第一项就是最后一项重复出现啊???是不是跟没有跳出循环有关啊???但是我看不出来。请教!!!
              SqlConnection conn1 = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
            conn1.Open();
            SqlCommand comm1 = new SqlCommand("select * from ExtendInfo where UserID='" + i + "' and ExtendInfoType = 0", conn1);
            SqlDataReader read1 = comm1.ExecuteReader();
            string faxlist = "";
            while (read1.Read())            
            {
                faxlist = "或"+read1["ExtendInfo"].ToString();
                SqlConnection con1 = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
                SqlCommand com1 = new SqlCommand("select * from ExtendInfo where UserID='" + i + "' and ExtendInfoType = 0", con1);
                con1.Open();
                SqlDataReader rd1 = com1.ExecuteReader();
                while (rd1.Read())
                {
                    faxlist =faxlist+"或" +rd1["ExtendInfo"].ToString();
                }
                rd1.Close();
                con1.Close();
                break;
            }
            lblFax.Text = faxlist;
            read1.Close();
            conn1.Close();

解决方案 »

  1.   

    感觉你的变量定义 就有问题啊,怎么出现 两个 con1 了, 你要是想 用 faxlist 连接所有 rd1["ExtendInfo"].ToString得话 应该如下:   SqlConnection conn1 = new SqlConnection(ConfigurationManager.AppSettings["conStr"]); 
                conn1.Open(); 
                SqlCommand comm1 = new SqlCommand("select * from ExtendInfo where UserID='" + i + "' and ExtendInfoType = 0", conn1); 
                SqlDataReader read1 = comm1.ExecuteReader(); 
                string faxlist = ""; 
                while (read1.Read())            
                { 
                    faxlist =faxlist +  "或"+read1["ExtendInfo"].ToString(); 
                } 
                lblFax.Text = faxlist; 
                read1.Close(); 
                conn1.Close();
      

  2.   

    LZ为什么要写两次下面的语句
    SqlCommand com1 = new SqlCommand("select * from ExtendInfo where UserID='" + i + "' and ExtendInfoType = 0", con1); 
    还有那个i是怎么传的.
    把问题描述清楚.还没明白LZ的意思.
      

  3.   

    为什么要用   break;  ??
      

  4.   

    1,为什么要执行2次
     SqlCommand com1 = new SqlCommand("select * from ExtendInfo where UserID='" + i + "' and ExtendInfoType = 0", con1); 
    这个语句呢?2,赋值语句是不是有问题
    while (read1.Read())            
                { 
                    faxlist = "或"+read1["ExtendInfo"].ToString(); 
                   //如果是想把全部数据显示出来,那应该是  faxlist += "或"+read1["ExtendInfo"].ToString();