SqlDataReader C#, SQL Server 2005, VS 2008
我尝试使用C和VS 2008从SQL Server 2005中的
我有以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | private SqlConnection connection; private void GetDatabaseConnection() { string connectionString = @"Server = RZS-F839AD139AA\SQLEXPRESS; Integrated Security = SSPI; Database = HotelCustomerManagementDatabase"; connection = new SqlConnection(connectionString); connection.Open(); } public Food PopulateFoodItemListview() { GetDatabaseConnection(); string selectFoodItemQuery = @"SELECT food_ItemName, food_UnitPrice FROM t_Food"; SqlCommand command = new SqlCommand(selectFoodItemQuery, connection); SqlDataReader reader = command.ExecuteReader(); Food food = new Food(); List foodList = new List(); while (reader.Read()) { food.ItemName.Add(reader.GetString(0)); MessageBox.Show("ItemName:"+ food.ItemName); food.UnitPrice.Add(reader.GetDouble(1)); MessageBox.Show("UnitPrice:" + food.UnitPrice); } connection.Close(); return food; } |
在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public class Food { private List itemName = new List(); private List unitPrice = new List(); private double itemUnit; private Customer foodCustomer = new Customer(); public List ItemName { get { return itemName; } set { itemName = value ; } } public List UnitPrice { get { return unitPrice; } set { unitPrice = value; } } public double ItemUnit { get { return itemUnit; } set { itemUnit = value; } } public double GetItemPrice(double itemUnit, double unitPrice) { double itemPrice = itemUnit*unitPrice; return itemPrice; } } |
在信息箱里,它应该显示大米、羊肉、牛肉和它们的价格50、100、150。但它展示了
有什么问题?
food.itemname是一个列表,而不是字符串,因此toString()返回类型。你想要的是:
1 | food.ItemName[food.ItemName.Count - 1] |
单价相同:
1 | food.UnitPrice[food.UnitPrice.Count - 1].ToString() |
您应该指定:
1 | private List<string> itemName; |
这样,当您添加名称时,可以执行以下操作:
1 2 |
您注意到的问题是food.itemname只是一个列表而不是字符串。因此,您可以通过以下方式从列表中获取字符串:
一旦你填写了你的食物项目名列表,你就应该能够提前得到它:
1 2 | foreach(string f in food.itemName) MessageBox.Show(f); |
除此之外,我对这门课的措辞有点担心。如果一个食物对象代表一个食物实体,那么为什么这个类的itemname和price是
1 2 3 4 5 | public sealed class Food { public string itemName {get; set;} public double unitPrice {get; set;} } |
你可以这样做:
1 2 3 4 5 6 7 8 9 10 11 12 | public sealed class Food { public string itemName {get; set;} public double unitPrice {get; set;} public double itemUnit {get; set;} public double getItemPrice() { return itemUnit * unitPrice; } public Food() : this("unknown food", 0); public Food(string item, double price) { itemName = item; unitPrice = price; } //you might want to rethink your customer object as well. Are //you associating one customer with one item of food ? } |
以及如何使用该类:
1 2 3 4 5 6 7 8 9 10 |
你的
那么,
更新:如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | public List<Food> PopulateFoodItemListview() { GetDatabaseConnection(); string selectFoodItemQuery = @"SELECT food_ItemName, food_UnitPrice FROM t_Food"; SqlCommand command = new SqlCommand(selectFoodItemQuery, connection); SqlDataReader reader = command.ExecuteReader(); List<Food> foods = new List<Food>(); List<string> foodList = new List<string>(); while (reader.Read()) { Food food = new Food(); food.ItemName = reader.GetString(0); MessageBox.Show("ItemName:"+ food.ItemName); food.UnitPrice = reader.GetDouble(1); MessageBox.Show("UnitPrice:" + food.UnitPrice); foods.Add(food); } connection.Close(); return foods; } public class Food { private string itemName =""; private double unitPrice = 0.0; private double itemUnit; private Customer foodCustomer = new Customer(); public string ItemName { get { return itemName; } set { itemName = value ; } } public double UnitPrice { get { return unitPrice; } set { unitPrice = value; } } public double ItemUnit { get { return itemUnit; } set { itemUnit = value; } } public double GetItemPrice(double itemUnit, double unitPrice) { double itemPrice = itemUnit*unitPrice; return itemPrice; } } |
问题是food.item name返回一个
itemname和unitprice是列表对象,而不是单个项目。当您将对象传递给MessageBox函数时,它将对该对象调用ToString()以获取可显示的字符串。列表上的tosting()返回您看到的字符串。应该使用索引来获取列表中的项:
1 | MessageBox(ItemName(1)); |
或
1 | MessageBox(UnitPrice(1)); |