c#窗体应用编写一个外卖系统
时间: 2024-06-08 21:11:48 浏览: 11
编写一个外卖系统需要以下几个步骤:
1. 设计数据库:需要设计一个合理的数据库结构用于存储外卖系统的数据,如订单、用户、菜品等信息。
2. 开发用户界面:使用C#的窗体应用开发界面,包括登录页面、主页面、购物车页面、订单页面等。
3. 编写业务逻辑:实现用户注册、登录、查询菜品、添加购物车、提交订单等功能的业务逻辑。
4. 数据库连接:使用ADO.NET连接数据库,对数据进行增删改查操作,确保数据的一致性和完整性。
以下是一个简单的外卖系统的代码示例:
```
//登陆界面
private void btnLogin_Click(object sender, EventArgs e)
{
string userName = txtUserName.Text.Trim();
string password = txtPassword.Text.Trim();
if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
{
MessageBox.Show("用户名和密码不能为空!");
return;
}
//验证用户名和密码
if (CheckUser(userName, password))
{
//跳转到主界面
MainForm mainForm = new MainForm();
mainForm.Show();
this.Hide();
}
else
{
MessageBox.Show("用户名或密码错误!");
}
}
//验证用户名和密码
private bool CheckUser(string userName, string password)
{
//连接数据库
string connectionString = "Data Source=.;Initial Catalog=Takeout;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
//查询用户信息
SqlCommand command = new SqlCommand($"SELECT COUNT(*) FROM Users WHERE UserName='{userName}' AND Password='{password}'", connection);
int count = (int)command.ExecuteScalar();
connection.Close();
return count > 0;
}
//查询菜品
private void btnSearch_Click(object sender, EventArgs e)
{
string keyword = txtKeyword.Text.Trim();
if (string.IsNullOrEmpty(keyword))
{
MessageBox.Show("请输入关键字!");
return;
}
//连接数据库
string connectionString = "Data Source=.;Initial Catalog=Takeout;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
//查询菜品信息
SqlCommand command = new SqlCommand($"SELECT * FROM Dishes WHERE Name LIKE '%{keyword}%'", connection);
SqlDataReader reader = command.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
dataGridView1.DataSource = dt;
connection.Close();
}
//添加购物车
private void btnAddToCart_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count <= 0)
{
MessageBox.Show("请先选择要添加的菜品!");
return;
}
//获取选中的菜品信息
int id = (int)dataGridView1.SelectedRows[0].Cells["ID"].Value;
string name = dataGridView1.SelectedRows[0].Cells["Name"].Value.ToString();
decimal price = (decimal)dataGridView1.SelectedRows[0].Cells["Price"].Value;
//添加到购物车
DataRow row = dtCart.NewRow();
row["ID"] = id;
row["Name"] = name;
row["Price"] = price;
row["Quantity"] = 1;
dtCart.Rows.Add(row);
//计算总价
decimal totalPrice = 0;
foreach (DataRow dr in dtCart.Rows)
{
totalPrice += (decimal)dr["Price"] * (int)dr["Quantity"];
}
lblTotalPrice.Text = $"总价:{totalPrice:C}";
MessageBox.Show("添加成功!");
}
//提交订单
private void btnSubmitOrder_Click(object sender, EventArgs e)
{
if (dtCart.Rows.Count <= 0)
{
MessageBox.Show("购物车为空!");
return;
}
//连接数据库
string connectionString = "Data Source=.;Initial Catalog=Takeout;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
//插入订单信息
SqlCommand command = new SqlCommand("INSERT INTO Orders(UserName, TotalPrice) VALUES(@UserName, @TotalPrice); SELECT @@IDENTITY;", connection);
command.Parameters.AddWithValue("@UserName", lblUserName.Text);
command.Parameters.AddWithValue("@TotalPrice", lblTotalPrice.Text.Substring(3));
int orderId = Convert.ToInt32(command.ExecuteScalar());
//插入订单明细信息
foreach (DataRow dr in dtCart.Rows)
{
command = new SqlCommand("INSERT INTO OrderDetails(OrderID, DishID, DishName, Price, Quantity) VALUES(@OrderID, @DishID, @DishName, @Price, @Quantity)", connection);
command.Parameters.AddWithValue("@OrderID", orderId);
command.Parameters.AddWithValue("@DishID", dr["ID"]);
command.Parameters.AddWithValue("@DishName", dr["Name"]);
command.Parameters.AddWithValue("@Price", dr["Price"]);
command.Parameters.AddWithValue("@Quantity", dr["Quantity"]);
command.ExecuteNonQuery();
}
connection.Close();
MessageBox.Show("订单提交成功!");
//清空购物车
dtCart.Rows.Clear();
lblTotalPrice.Text = "总价:¥0.00";
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)