当前位置:首页 > 行业动态 > 正文

c# 数据库 点菜系统

在C#中开发数据库点菜系统,需先设计数据库表结构存储菜品、订单等信息,再用C#结合相关数据库操作技术实现功能,如连接数据库、增删改查等。

C#数据库点菜系统

在餐饮行业中,点菜系统是餐厅运营的重要组成部分,一个高效、便捷的点菜系统不仅能够提升顾客的用餐体验,还能提高餐厅的服务效率和管理水平,本文将详细介绍如何使用C#语言结合数据库技术来开发一个点菜系统。

一、系统需求分析

1、用户管理:包括用户注册、登录、权限管理等功能。

2、菜品管理:能够添加、修改、删除菜品信息,包括菜品名称、价格、图片等。

3、餐桌管理:可以对餐桌进行编号、状态(空闲、占用、预订)管理。

4、点菜功能:顾客可以通过系统点菜,选择菜品数量、规格等。

c# 数据库 点菜系统

5、订单管理:生成订单,记录订单详情,包括菜品、数量、总价等。

6、结账功能:支持多种支付方式,如现金、银行卡、微信支付等。

7、报表统计:提供销售报表、库存报表等统计功能,方便餐厅管理者了解经营情况。

二、数据库设计

1、用户表(Users)

c# 数据库 点菜系统

字段名 数据类型 描述
UserID int 用户编号,主键
UserName nvarchar(50) 用户名
Password nvarchar(50) 密码
Role nvarchar(20) 角色(管理员、服务员等)

2、菜品表(Dishes)

字段名 数据类型 描述
DishID int 菜品编号,主键
DishName nvarchar(50) 菜品名称
Price decimal(10, 2) 菜品价格
ImagePath nvarchar(200) 菜品图片路径

3、餐桌表(Tables)

字段名 数据类型 描述
TableID int 餐桌编号,主键
TableStatus nvarchar(20) 餐桌状态(空闲、占用、预订)

4、订单表(Orders)

字段名 数据类型 描述
OrderID int 订单编号,主键
UserID int 用户编号,外键关联 Users 表
TableID int 餐桌编号,外键关联 Tables 表
OrderTime datetime 下单时间
TotalAmount decimal(10, 2) 订单总金额

5、订单详情表(OrderDetails)

字段名 数据类型 描述
OrderDetailID int 订单详情编号,主键
OrderID int 订单编号,外键关联 Orders 表
DishID int 菜品编号,外键关联 Dishes 表
Quantity int 菜品数量
Subtotal decimal(10, 2) 小计金额

三、C#代码实现

c# 数据库 点菜系统

1、数据库连接类

using System;
using System.Data.SqlClient;
public class DBHelper
{
    private static string connectionString = "Server=服务器地址;Database=数据库名;User Id=用户名;Password=密码;";
    public static SqlConnection GetConnection()
    {
        SqlConnection conn = new SqlConnection(connectionString);
        return conn;
    }
}

2、用户登录功能

using System;
using System.Data;
using System.Windows.Forms;
public class LoginForm : Form
{
    private TextBox txtUserName;
    private TextBox txtPassword;
    private Button btnLogin;
    public LoginForm()
    {
        InitializeComponent();
    }
    private void InitializeComponent()
    {
        this.txtUserName = new TextBox();
        this.txtPassword = new TextBox();
        this.btnLogin = new Button();
        this.btnLogin.Text = "登录";
        this.btnLogin.Click += new EventHandler(this.BtnLogin_Click);
        // 设置控件位置和大小等属性...
    }
    private void BtnLogin_Click(object sender, EventArgs e)
    {
        string userName = txtUserName.Text;
        string password = txtPassword.Text;
        bool isValidUser = ValidateUser(userName, password);
        if (isValidUser)
        {
            MessageBox.Show("登录成功!");
            // 打开主界面等操作...
        }
        else
        {
            MessageBox.Show("用户名或密码错误!");
        }
    }
    private bool ValidateUser(string userName, string password)
    {
        string query = "SELECT * FROM Users WHERE UserName = @UserName AND Password = @Password";
        using (SqlConnection conn = DBHelper.GetConnection())
        {
            SqlCommand cmd = new SqlCommand(query, conn);
            cmd.Parameters.AddWithValue("@UserName", userName);
            cmd.Parameters.AddWithValue("@Password", password);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

3、菜品管理功能(添加菜品示例)

using System;
using System.Data;
using System.Windows.Forms;
public class DishManagementForm : Form
{
    private TextBox txtDishName;
    private TextBox txtPrice;
    private PictureBox picDishImage;
    private Button btnAddDish;
    public DishManagementForm()
    {
        InitializeComponent();
    }
    private void InitializeComponent()
    {
        this.txtDishName = new TextBox();
        this.txtPrice = new TextBox();
        this.picDishImage = new PictureBox();
        this.btnAddDish = new Button();
        this.btnAddDish.Text = "添加菜品";
        this.btnAddDish.Click += new EventHandler(this.BtnAddDish_Click);
        // 设置控件位置和大小等属性...
    }
    private void BtnAddDish_Click(object sender, EventArgs e)
    {
        string dishName = txtDishName.Text;
        decimal price = Convert.ToDecimal(txtPrice.Text);
        string imagePath = picDishImage.ImageLocation; // 假设已经设置了图片路径
        AddDish(dishName, price, imagePath);
    }
    private void AddDish(string dishName, decimal price, string imagePath)
    {
        string query = "INSERT INTO Dishes (DishName, Price, ImagePath) VALUES (@DishName, @Price, @ImagePath)";
        using (SqlConnection conn = DBHelper.GetConnection())
        {
            SqlCommand cmd = new SqlCommand(query, conn);
            cmd.Parameters.AddWithValue("@DishName", dishName);
            cmd.Parameters.AddWithValue("@Price", price);
            cmd.Parameters.AddWithValue("@ImagePath", imagePath);
            conn.Open();
            int rowsAffected = cmd.ExecuteNonQuery();
            if (rowsAffected > 0)
            {
                MessageBox.Show("菜品添加成功!");
            }
            else
            {
                MessageBox.Show("菜品添加失败!");
            }
        }
    }
}

4、点菜功能

using System;
using System.Data;
using System.Windows.Forms;
public class OrderingForm : Form
{
    private DataGridView dgvDishes;
    private ComboBox cboTableNumber;
    private TextBox txtQuantity;
    private Button btnAddToCart;
    private DataGridView dgvOrderDetails;
    private Button btnCheckout;
    public OrderingForm()
    {
        InitializeComponent();
    }
    private void InitializeComponent()
    {
        this.dgvDishes = new DataGridView();
        this.cboTableNumber = new ComboBox();
        this.txtQuantity = new TextBox();
        this.btnAddToCart = new Button();
        this.btnAddToCart.Text = "加入购物车";
        this.dgvOrderDetails = new DataGridView();
        this.btnCheckout = new Button();
        this.btnCheckout.Text = "结账";
        this.btnCheckout.Click += new EventHandler(this.BtnCheckout_Click);
        this.btnAddToCart.Click += new EventHandler(this.BtnAddToCart_Click);
        // 设置控件位置和大小等属性...
    }
    private void BtnAddToCart_Click(object sender, EventArgs e)
    {
        int dishId = (int)dgvDishes.CurrentRow.Cells[0].Value;
        int quantity = int.Parse(txtQuantity.Text);
        AddToCart(dishId, quantity);
    }
    private void AddToCart(int dishId, int quantity)
    {
        string query = "SELECT * FROM Dishes WHERE DishID = @DishID";
        using (SqlConnection conn = DBHelper.GetConnection())
        {
            SqlCommand cmd = new SqlCommand(query, conn);
            cmd.Parameters.AddWithValue("@DishID", dishId);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
                {
                    string dishName = reader["DishName"].ToString();
                    decimal price = Convert.ToDecimal(reader["Price"]);
                    DataGridViewRow row = new DataGridViewRow();
                    row.Cells.Add(new DataGridViewTextBoxCell().Value = dishId.ToString());
                    row.Cells.Add(new DataGridViewTextBoxCell().Value = dishName);
                    row.Cells.Add(new DataGridViewTextBoxCell().Value = quantity.ToString());
                    row.Cells.Add(new DataGridViewTextBoxCell().Value = (quantity * price).ToString());
                    dgvOrderDetails.Rows.Add(row);
                }
                reader.Close();
            }
        }
    }
    private void BtnCheckout_Click(object sender, EventArgs e)
    {
        // 结账逻辑,包括计算总金额、生成订单等操作...
    }
}

只是一个简单的示例,实际的点菜系统还需要考虑更多的细节和功能完善,如菜品的筛选、购物车的修改、订单状态的跟踪等,还可以使用更先进的技术框架和设计模式来提高系统的可维护性和扩展性,希望这个示例能对你有所帮助!