我想根据SqlDataReader数据在一个字符串中组合2个或更多记录。
SqlDataReader data = cmd.ExecuteReader();
string category = "";
while (data.Read())
{
category = data["Column1"].ToString() + ", " + category;
}
ltCategory.Text = category.TrimEnd(',');
我有2条现有记录,而ltCategory.Text显示 Record1,Record2,
我想使用以下格式:
解
SqlDataReader data = cmd.ExecuteReader();
string category = "";
while (data.Read())
{
category += ", " + data["Column1"].ToString();
}
ltCategory.Text = category.TrimStart(',');
答案 0 :(得分:1)
将category = data["Column1"].ToString() + ", " + category;
更改为category += data["Column1"].ToString() + ", ";
ltCategory.Text = category.TrimEnd(',');
这可能需要更改为:
ltCategory.Text = category.TrimEnd(', ');