2012-04-24 194 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