如何从数据库中检索数据

本文关键字:检索 数据 数据库 | 更新日期: 2023-09-27 18:32:05

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

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

如何从数据库中检索数据

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

  1. 由于您已经有一个数据表,因此使其成为私有字段。然后,在 ComboBox.SelectedIndex 更改时,您可以检索所选值,然后使用 DataTable.SelectDataTable.Find 或 Linq To DataSet 检索价格值。

  2. 您可以只设置组合框的DataSourceDisplayMemberValueMember属性。数据源是数据表,显示成员是名称,值成员是价格。无论何时您想要价格,您只需使用ComboBox.SelectedValue(在这种情况下无需保留私有字段,因为数据源将保留它)

  3. 您也可以使用上述两者的组合。例如,您可以使用 ID 作为值成员,您可以使用 #1 中描述的技术进行查找。

  4. 仍然设置数据源,使用任何您想要的值成员,并使用组合框的DataManager,并在需要时访问所选项目的整行

除此之外,许多人不喜欢使用DataTables,而是使用自定义类的集合。以上所有方法都适用于普通集合,除了你只使用 Linq 或 IEnumerable 方法,而不是 linq to dataset 或 datatable 方法

你想这样做吗?

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