2012-04-24 193 views
-4

我已經創建了菜單項的類,並且我無法弄清楚某人某人從組合框中選擇比薩並選擇澆頭時如何獲得價格。我從數據庫中獲得比薩價格和最佳價格。這是我的比薩類如何從數據庫中檢索數據

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data.SqlClient; 
using System.Data; 


namespace ItalianoLIB.BLL 
{ 

    public class Pizza 
    { 

     public string pizzaName { get; set; } 
     public string toppingName { get; set; } 
     public double toppingPrice { get; set; } 
     public double pizzaPrice { get; set; } 



    } 
} 


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.SqlClient; 




namespace ItalianoWIN.PLL 
{ 

    public partial class PizzaMenu : Form 
    { 
     public string newPizzaName { get; set; } 
     public string newToppingName { get; set; } 
     public double newToppingPrice { get; set; } 
     public double newPizzaPrice { get; set; } 

     public PizzaMenu() 
     { 

      InitializeComponent(); 
     } 

     private void Pizza_Load(object sender, EventArgs e) 
     { 

      //new connection from the DButils class 
      SqlConnection con = new SqlConnection(ItalianoLIB.DLL.DButils.CONSTR); 
      con.Open(); 

      //fill Pizza type combo box 
      SqlDataAdapter da = new SqlDataAdapter("select * from pizza", con); 
      DataTable dt = new DataTable(); 
      da.Fill(dt); 



      for (int i = 0; i < dt.Rows.Count; i++) 
      { 
      cboPizzaType.Items.Add(dt.Rows[i]["PizzaType"]); 
      } 



      //fill toppings listbox 
      SqlDataAdapter da2 = new SqlDataAdapter("select * from Topping",con); 
      DataTable dt2 = new DataTable(); 
      da2.Fill(dt2); 

      for (int i = 0; i < dt2.Rows.Count; i++) 
      { 
       lstToppings.Items.Add(dt2.Rows[i]["ToppingName"]); 
      } 



      con.Close(); 


     } 

     private void cboPizzaType_SelectedIndexChanged(object sender, EventArgs e) 
     { 


     } 


     private void lstToppings_SelectedIndexChanged(object sender, EventArgs e) 
     { 

     } 

     private void bnPizOrd_Click(object sender, EventArgs e) 
     { 
      newPizzaName = cboPizzaType.Text.ToString(); 



      //Brings the user back to the main form 
      this.DialogResult = DialogResult.OK; 
     } 

     private void bnAddTop_Click(object sender, EventArgs e) 
     { 


      object obj = lstToppings.SelectedItem; 
      lstSelTop.Items.Add(obj); 
      lstToppings.Items.Remove(obj); 

     } 

     private void bnDelTop_Click(object sender, EventArgs e) 
     { 
      object obj = lstSelTop.SelectedItem; 
      lstToppings.Items.Add(obj); 
      lstSelTop.Items.Remove(obj); 

     } 
    } 
} 
+0

建議您寫一個只與數據庫交互的小命令行程序檢索你知道的東西已經存在,或者更新那裏的東西。試圖插入某些東西會變得更復雜。 – octopusgrabbus 2012-04-24 21:07:37

+4

你究竟在問什麼?你沒有說明任何不起作用 – 2012-04-24 21:08:41

+0

我有在sql服務器中的披薩表,它有一個pizzaName和披薩價格。我用sql server中的披薩表填充組合框。我試圖找出某人在組合框中選擇一個披薩時,我如何從所選項目的披薩價格字段中獲得價格。 – 2012-04-24 21:17:27

回答

0

你想這樣做嗎?

private void cboPizzaType_SelectedIndexChanged(object sender, EventArgs e)   
{   
SqlConnection con = new SqlConnection(ItalianoLIB.DLL.DButils.CONSTR);    
con.Open();    
SqlDataAdapter da = new SqlDataAdapter("select PizzaPrice from pizza WHERE PizzaType='" + cboPizzaType.Text + "'", con);    
DataTable dt = new DataTable();    
da.Fill(dt); 
con.Close();   
var oPrice = dt.Rows[0][0]; 
this.pizzaPrice = (double)oPrice; 
}   
+0

雖然這會起作用,但每次更改組合框時都需要往返數據庫。這是......不夠理想。 – 2012-04-24 21:54:28

+0

非常感謝你,我現在得到它 – 2012-09-29 01:18:33

4

有很多方法可以做到這一點這裏的四

  1. 既然你已經有了一個DataTable已經使它成爲一個私有字段。然後在ComboBox.SelectedIndex更改您可以檢索選定的值,然後使用DataTable.Select or DataTable.FindLinq To DataSet檢索價格值。

  2. 您可以設置組合框的DataSource,DisplayMemberValueMember屬性。 DataSource將是數據表,顯示成員的名稱和價值成員的價格。任何時候你想要的價格,你只需使用ComboBox.SelectedValue(在這種情況下不需要保留私人領域,因爲數據源會保留)

  3. 您也可以使用上述兩種組合。例如,您可以使用編號爲值的成員,您可以使用#1中描述的技術進行查找。

  4. 仍在設置任何你想要的值成員的數據源使用和使用組合框的DataManager並訪問所選項目的整行只要你需要它

順便多人們不喜歡使用DataTable,而是傾向於使用自定義類的集合。以上所有內容都適用於普通集合,除非您僅使用Linq或IEnumerable方法,而不是linq到數據集或數據表方法

+0

我能夠通過做pizzaName = cboPizzaType.SelectedValue.ToString()在pizzaName變量
中存儲選定的披薩。
但我不知道誰將價格存儲在pizzaprice變量中。 – 2012-04-25 01:24:55

+0

@PollyPoll我從你原來的問題中得到了答案。我的答案描述了四種方法來做到這一點。有什麼特別的問題可以解決你的問題嗎? – 2012-04-25 14:33:12