C# 如何从数据库检索数据

C# 如何从数据库检索数据,c#,sql,C#,Sql,我已经为菜单项创建了类,但当有人从组合框中选择比萨饼并选择配料时,我不知道如何获得价格。我正在从数据库中获取比萨饼的价格和最高价格。这是我的披萨课 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data; namespace ItalianoLIB.BLL { public c

我已经为菜单项创建了类,但当有人从组合框中选择比萨饼并选择配料时,我不知道如何获得价格。我正在从数据库中获取比萨饼的价格和最高价格。这是我的披萨课

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

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Data.SqlClient;
使用系统数据;
名称空间b.BLL
{
公共级比萨饼
{
公共字符串pizzaName{get;set;}
公共字符串toppingName{get;set;}
公共双顶级价格{get;set;}
公共双比萨饼价格{get;set;}
}
}
使用制度;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Data.SqlClient;
名称空间:owin.PLL
{
公共部分类比萨饼菜单:表单
{
公共字符串newPizzaName{get;set;}
公共字符串newToppingName{get;set;}
公共双newToppingPrice{get;set;}
公共双新比萨饼价格{get;set;}
公共比萨饼菜单()
{  
初始化组件();
}
私有无效PIZAU加载(对象发送方、事件参数e)
{
//来自DButils类的新连接
SqlConnection con=新的SqlConnection(意大利语olib.DLL.DButils.cont);
con.Open();
//填充比萨饼式组合盒
SqlDataAdapter da=新SqlDataAdapter(“从比萨饼中选择*”,con);
DataTable dt=新的DataTable();
da.填充(dt);
对于(int i=0;i
您想这样做吗

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

有很多方法可以做到这一点这里有四个

  • 因为您已经有了一个DataTable,所以将其设置为私有字段。然后在ComboBox.SelectedIndex change上,您可以检索所选值,然后使用或检索价格值

  • 您可以只设置组合框的、和属性。数据源是datatable,显示成员是Name,值成员是price。只要您想要价格,就可以使用(在这种情况下无需保留私有字段,因为数据源将保留该字段)

  • 您还可以将上述两种方法结合使用。例如,您可以为值成员使用ID,您可以使用#1中描述的技术进行查找

  • 仍然在设置数据源,对成员值使用您想要的任何内容,并在需要时使用组合框的名称并访问所选项目的整行


  • 另外,许多人不喜欢使用DataTables,而更喜欢使用自定义类的集合。上述所有方法都适用于正常的集合,除非您只使用Linq或IEnumerable方法,而不是Linq to DATASE或datatable方法

    建议您编写一个与数据库交互的小型命令行程序,或者检索已知的内容,或者更新现有的东西。尝试插入一些东西会变得更加棘手。你到底在问什么?您没有说明任何不起作用的内容我在sql server中有pizza表,它有pizzaName和pizzaPrice。我使用sql server中比萨饼表中的比萨饼名称填充组合框。我试图找出当有人在组合框中选择比萨饼时,如何从比萨饼价格字段中获取所选项目的价格。虽然这样做可行,但每次组合框更改时都需要往返数据库。那是。。。不太理想。我可以通过执行pizzaName=cboPizzaType.SelectedValue.ToString()将选定的比萨饼存储在pizzaName变量中

    但我不知道谁应该在pizzaprice变量中存储价格。@PollyPoll我是从您最初的问题中得到的。我的回答描述了四种方法。我的回答有什么特别的地方让你感到困扰吗?