【发布时间】:2011-04-01 03:44:29
【问题描述】:
我正在尝试使用 C# 和 VS 2008 从 SQL Server 2005 中的 t_Food 表中选择 food_ItemName 和 food_UnitPrice。
我有以下代码:
私有 SqlConnection 连接; 私人无效GetDatabaseConnection() { string connectionString = @"服务器 = RZS-F839AD139AA\SQLEXPRESS;集成安全 = SSPI;数据库 = HotelCustomerManagementDatabase"; 连接 = 新的 SqlConnection(connectionString); 连接.打开(); } 公共食物 PopulateFoodItemListview() { 获取数据库连接(); string selectFoodItemQuery = @"SELECT food_ItemName, food_UnitPrice FROM t_Food"; SqlCommand command = new SqlCommand(selectFoodItemQuery, connection); SqlDataReader 阅读器 = command.ExecuteReader(); 食物食物=新食物(); 列出 foodList = new List(); 而(读者。读()) { food.ItemName.Add(reader.GetString(0)); MessageBox.Show("ItemName: "+ food.ItemName); food.UnitPrice.Add(reader.GetDouble(1)); MessageBox.Show("单价:" + food.UnitPrice); } 连接。关闭(); 返还食物; }在Food 类中,我有以下代码:
在 messageBox 中,它应该显示 Rice、Mutton、Beef 及其价格 50、100、150。但它显示 ItemName: System.Collections.Generic.List`1[System.String] 和
ItemName: System.Collections.Generic.List`1[System.Double]
有什么问题?
【问题讨论】:
-
您是否遇到任何错误?您的 SQL 查询是否返回了所需的数据?
-
请注意,最好尽可能使用泛型集合。最好不要在公开这些集合的属性上提供设置器。
-
@towhidulbashar - 欢迎来到 stackoverflow。只是为了让您知道,您可能想要检查所有问题并将您使用的答案标记为被接受。社区通常在工作,但社区的用户仍然会抽出时间在此站点上回答问题。因此,正确的做法是标记所有已解决的问题,并使用可接受的答案。
-
@JonH :: 我通过单击勾选标记选择了接受的答案。我需要做些什么来将我的问题标记为已解决的问题吗?其实我是 Stackoverflow 社区的新手。感谢您的建议。