C#中的类列表
本文关键字:列表 | 更新日期: 2023-09-27 18:19:30
我的代码C#有问题。
在数据库Users
的表中,我有两个资费计划的用户(multitariff)
我需要将从数据库中提取的平面值填充到单个用户的Labelplane
中。
我想创建一个list of classes
,其中存储平面的值。
我尝试过使用这个解决方案,但没有成功,因为对于multitariff user
,输出只是平面的第一个值,而不是平面的所有值。
在解决这个问题时,如果你能给我任何帮助,我将不胜感激。
这是我的代码:
string plane;
List<string> planeList = new List<string>();
public class Lists
{
static void Main()
{
List<string> planeList = new List<string>();
}
}
protected void Users()
{
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
sql = " SELECT * FROM Users WHERE Id = ? ";
using (OdbcCommand command =
new OdbcCommand(sql, cn))
{
try
{
command.Parameters.AddWithValue("param1", Server.UrlDecode(Request.Cookies["Id"].Value));
command.Connection.Open();
using (OdbcDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
plane = reader["plane"].ToString();
planeList.Add(plane.ToString());
}
}
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
command.Connection.Close();
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Users();
if (Request.Cookies["Id"] != null)
{
foreach (string plane in planeList)
{
plane.Text = "User plane: " + plane.ToString().Trim() + "<br />";
}
}
}
}
您每次都会覆盖标签的值:
foreach (string plane in planeList)
{
plane.Text = "User plane: " + plane.ToString().Trim() + "<br />";
}
与其覆盖它,不如连接字符串:
plane.Text = "User plane: ";
foreach (string item in planeList)
{
plane.Text += item.ToString().Trim() + "<br />"; // I suppose plane is a label here, so your foreach variable should be called different (item in this case)
}