簡體   English   中英

SqlDataReader C#,SQL Server 2005,VS 2008

[英]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-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;
        }

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中,它應該顯示大米,羊肉,牛肉及其價格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(string f in food.itemName)
 MessageBox.Show(f);

除此之外,我有點擔心這一堂課的措辭。 如果Food對象表示食物的一個實體,那么為什么itemName和price 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]

與單價相同:

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

ItemName和UnitPrice是List對象,而不是單個項目。 當您將對象傳遞給MessageBox函數時,它將在對象上調用ToString()以獲取可顯示的字符串。 列表上的ToSting()返回您看到的字符串。 您應該使用索引來訪問列表中的項目:

MessageBox(ItemName(1));

要么

MessageBox(UnitPrice(1));

問題是food.Item名稱返回一個List<string>而您正在隱式調用List<string>::ToString ,它返回有關類型的詳細信息。 您應該將最后輸入的商品請求為food.ItemName[food.ItemName.Count - 1]或者可以遍歷列表中的所有商品以輸出所有名稱/價格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM