SqlDataReader C#,SQL Server 2005,VS 2008

我正尝试使用C#和VS 2008从SQL Server 2005中的t_Food表中选择food_ItemNamefood_UnitPrice

我有以下代码:

        private SqlConnection connection;

        private void GetDatabaseConnection()
        {
            string connectionString = @"Server = RZS-F839AD139AASQLEXPRESS; 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;
        }

Food类中,我有以下代码:

    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;
        }
}

在messageBox中,它应该显示Rice,Mutton,Beef和它们的价格50,100,150。但是它显示ItemName ItemName: System.Collections.Generic.List`1[System.String]ItemName: System.Collections.Generic.List`1[System.Double]

有什么问题?


你应该指定:

private List<string> itemName;

这样当你添加一个名字时你可以这样做:

itemName = new List<string>();
itemName.Add("Pizza");

你注意到的问题是food.itemName只是一个列表而不是一个字符串。 因此,您可以通过以下方式从列表中获取字符串: myList[someIndex],即List对象,后跟列表中项目的索引。

一旦你填充你的食物itemName列表,你应该能够对它进行foreach:

foreach(string f in food.itemName)
 MessageBox.Show(f);

除此之外,我对这堂课的用词有点担心。 如果食物对象表示食品的一个实体,那么为什么是ITEMNAME和价格list这一类的成员? 它们应该简单地分别是string类型和double类型的属性。 当您创建食物对象时,该对象包含名称和价格。

public sealed class Food
{
 public string itemName {get; set;}
 public double unitPrice {get; set;}
}

以下是你如何做到这一点:

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 ?
 }

以及如何使用这个类:

 public sealed class MyUsageOfFood
 {
 public static void main() {
  List<Food> f = new List<Food>;
  f.Add(new Food("Pizza", 1.50));
  f.Add(new Food("Hamburger", 2.00));

  foreach(food t in f)
      MessageBox.Show(t.itemName);
 }}

food.ItemName是一个List,而不是一个字符串,所以ToString()返回该类型。 你想要的是:

food.ItemName[food.ItemName.Count - 1]

和UnitPrice相同:

food.UnitPrice[food.UnitPrice.Count - 1].ToString()

你的Food类是为了代表一个单一的食物,所以你的itemNameunitPrice成员应该分别是stringdouble类型(不是List ,它用来存储多个值)。

然后, PopulateFoodItemListview应该返回List<Food> (不是Food )。 在你的reader.Read()循环中,你应该创建一个新的Food实例,用数据库中的适当值填充它,然后将它添加到List<Food>集合中(然后在方法结尾返回) 。

更新:像这样:

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;
    }
}
链接地址: http://www.djcxy.com/p/60283.html

上一篇: SqlDataReader C#, SQL Server 2005, VS 2008

下一篇: Passing parameters to web service method in c#