为什么只返回了一个值?而不是一个数组?
- 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, } }
[解决办法]
你这样是循环赋值,并没有追加,肯定最多只有一个了。
[解决办法]
[解决办法]
注意红色部分
private string OutputPrice()
{
string sql = "select * from YuyaoPrice where ClassId=1 and SmallId=435";
DataTable dt = DBclass.ExecSel(sql);
string c="";
for (int i = 0; i < dt.Rows.Count; i++)
{
string a = dt.Rows[i]["NewPrice"].ToString();
string b = ",";
c += a + b;
Price = c.ToString();
}
return Price;
}
[解决办法]
把 = 改为 +=
[解决办法]
private string OutputPrice()
{
string Price=string.Empty;
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++)
{
Price += dt.Rows[i]["NewPrice"].ToString()+",";
}
return Price;
}