日期:2014-05-17  浏览次数:20491 次

为什么只返回了一个值?而不是一个数组?
C# code
       
 private string OutputPrice()
        {
            string sql = "select * from YuyaoPrice where ClassId=1 and SmallId=435";
            DataTable dt = DBclass.ExecSel(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string a = dt.Rows[i]["NewPrice"].ToString();
                string b = ",";
                string c = a + b;
                Price = c.ToString();
                
            }
            return Price;
          
        }
 public string Price = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            Price = OutputPrice();
            Response.Write(Price.ToString());//读出来只有:15900,
        }




以上代码只返回了一个值:15900,正确的话应该是:15900,16000,16000,16000,

------解决方案--------------------
应该不是代码的问题,去数据库里看一下,也许就真的只有一行数据返回。
------解决方案--------------------
Price = c.ToString(); 当然只返回一个值了

Price += c.ToString(); 而且可能你for循环里算法还得改改
------解决方案--------------------
for (int i = 0; i < dt.Rows.Count; i++)
{
object a = dt.Rows[i]["NewPrice"];
if(a != null)
{
Price += a.ToString();
}
if(i < dt.Rows.Count - 1)
{
Price += ",";
}
}

for循环改成这样试试,我没测试过
------解决方案--------------------
这样:
private string OutputPrice()
{
string _price="";
string sql = "select * from YuyaoPrice where ClassId=1 and SmallId=435";
DataTable dt = DBclass.ExecSel(sql);
 
for (int i = 0; i < dt.Rows.Count; i++)
{
string a = dt.Rows[i]["NewPrice"].ToString();
string b = ",";
string c = a + b;
_price += c.ToString();

}
return _price;

}
------解决方案--------------------
应该用list<>
------解决方案--------------------
C# code

 private List<string> OutputPrice()
        {
            string sql = "select * from YuyaoPrice where ClassId=1 and SmallId=435";
            DataTable dt = DBclass.ExecSel(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string a = dt.Rows[i]["NewPrice"].ToString();
                string b = ",";
                string c = a + b;
                Prices.Add(c.ToString());
            }
            return Prices;

        }
        public List<string> Prices = new List<string>();

        protected void Page_Load(object sender, EventArgs e)
        {
            Prices = OutputPrice();

            foreach (string Price in Prices)
            {
                Response.Write(Price.ToString());//读出来只有:15900,
            }
        }

------解决方案--------------------
你这样是循环赋值,并没有追加,肯定最多只有一个了。
------解决方案--------------------
探讨
都不行。。。。

------解决方案--------------------
注意红色部分