有这么一个dataset 为myDs
手机号码     消息内容
123456      你好
123654      你们好
315454      大家好
我现在想把手机号码这一列的值取出来
      DataTable dt = myDs.Tables[0];
        foreach (DataRow dr in dt.Rows)
        {            mobile = dr[0].ToString();
            msgcotent = dr[1].ToString();
        }  
response.write(mobile);执行这段代码后只有一个值123456 
这是怎么回事  ,怎么样才能把所有的号码都打印出来了?

解决方案 »

  1.   


    DataTable dt = ds.Tables[0];
    string[] s = new string[dt.Rows.Count]; ;
                int i=0;
                foreach (DataRow dr in dt.Rows)
                {
                    s[i] = dr[0].ToString();
                    i++;
                }
                for (int j = 0; j < s.Length; j++)
                {
                    Response.Write(s[j]);
                }
      

  2.   

    string  mobile="";
    for(int i=0;i<myDs.Tables[0].rows.count;i++)
    {
       mobile +=myDs.Tables[0].rows[i][0].tostring()+"  ";
    }
    response.write(mobile);
      

  3.   

    可以循环的时候就输出或处理
    DataTable dt = myDs.Tables[0]; 
            foreach (DataRow dr in dt.Rows) 
            { 
                mobile = dr[0].ToString(); 
                msgcotent = dr[1].ToString(); 
                response.write(mobile+"<br>");        }  
    最好还是用数组存,
    要不就加分隔符。
      

  4.   

          DataTable dt = myDs.Tables[0]; 
            foreach (DataRow dr in dt.Rows) 
            {             mobile = dr[0].ToString(); 
                msgcotent = dr[1].ToString(); 
            }  
    response.write(mobile);要授人与渔你的mobile只输出了一遍,但是却三次获得数据
         楼上的写法不错   呵呵
      

  5.   

    response.write放到里面去
    或者 mobile = dr[0].ToString(); 该为  mobile += dr[0].ToString()+"分隔";