C#课程设计
1.开发环境
VS2019,SQL2012,winform窗体设计
2.项目主题
刚好完成了C#的课程设计,觉得该好好纪念一下((✪ω✪)),这是一个超市线上购物平台,虽然明明知道这种线上购物平台更适合做网站开发,但是莫得办法呀,谁叫网站也要做课程设计呢,懒得想新的主题了,就决定和网站的课程设计主题一样了.(项目代码在文末,有兴趣的朋友欢迎查看)
也有网站版的https://blog.csdn.net/weixin_44050161/article/details/113811896
3.项目部分效果图
4.主要功能
- 用户登录
- 用户注册
- 信息修改
- 将商品加入购物车或者收藏夹
- 商家可以查看订单(数据库的增删改查)
5.部分代码
- 添加收藏的代码
//添加收藏
void addFavour(string commID)
{
try
{
using (SqlConnection sqlcon = new SqlConnection(AppConst.DbPath))
{
//加载商品的数据
string sql = "select * from [commodity_table] where [comID]='" + commID + "'";//sda执行的sql语句、执行的数据库连接
var WarEData = new SqlDataAdapter(sql, sqlcon);//创建数据库连接
var WareTable = new DataTable();//DataTable是DataSet里的表,DataTable的对象dt就是一个表格。
WarEData.Fill(WareTable);//往dt里填充数据。
string name = WareTable.Rows[0]["comName"].ToString();
string introduction = WareTable.Rows[0]["comIntroduction"].ToString();
string price = WareTable.Rows[0]["comPrice"].ToString();
string imgUrl = WareTable.Rows[0]["imgUrl"].ToString();
SqlCommand sqlcmd = new SqlCommand($"select [comSurplus] from [FavouryTable] where [commID]='{commID}'", sqlcon);
sqlcon.Open();
var obj = sqlcmd.ExecuteScalar();
if (obj == null) //还没有添加过商品
{
//MessageBox.Show("未有数据!");
string strsql2 = "insert into [FavouryTable]([commID],[comName],[comIntroduction],[comPrice],[imgUrl]) values(@commID,@comName,@comIntroduction,@comPrice,@imgUrl)";
using (SqlCommand comm = new SqlCommand
{
CommandText = strsql2,
CommandType = CommandType.Text,
Connection = sqlcon
})
{
comm.Parameters.AddWithValue("@commID", commID);
comm.Parameters.AddWithValue("@comName", name);
comm.Parameters.AddWithValue("@comIntroduction", introduction);
comm.Parameters.AddWithValue("@comPrice", price);
comm.Parameters.AddWithValue("@imgUrl", imgUrl);
//sqlcon.Open();
if (Convert.ToInt32(comm.ExecuteNonQuery()) > 0)
{
MessageBox.Show("收藏成功");
}
else
{
MessageBox.Show("收藏失败!");
}
}
}
else
{
MessageBox.Show("已收藏成功!");
}
}
}
catch (SqlException sqlex)
{
MessageBox.Show("数据库连接错误,错误源是" + sqlex);
}
}
- 购物车显示的代码
//加载购物车
void getShopCart()
{
using (SqlConnection conn = new SqlConnection(AppConst.DbPath))
{
string imaurl, information, price, comID;
int splus;
//加载所有购物车的数据
string sql = "select * from [Shopping_Cart]";//sda执行的sql语句、执行的数据库连接
var WarEData = new SqlDataAdapter(sql, conn);//创建数据库连接
var WareTable = new DataTable();//DataTable是DataSet里的表,DataTable的对象dt就是一个表格。
WarEData.Fill(WareTable);//往dt里填充数据。
//cartList[] cartLists = new cartList[WareTable.Rows.Count];
//生成自定义控件
for (int i = 0; i < WareTable.Rows.Count; i++)
{
comID = WareTable.Rows[i]["commID"].ToString(); //商品ID
imaurl = WareTable.Rows[i]["imgUrl"].ToString(); //图片路径
information = WareTable.Rows[i]["comIntroduction"].ToString(); //简介
price = WareTable.Rows[i]["comPrice"].ToString();
price = Convert.ToDecimal(float.Parse(price).ToString()).ToString("F2"); //价钱
splus = int.Parse(WareTable.Rows[i]["comSurplus"].ToString()); //数量
//cartList cartList = (cartList)flowLayoutPanel1.Controls["cartList" + i.ToString()];
cartList cartList = new cartList(comID, imaurl, information, price, splus);
cartList.OnCheckedChanged += CartList_OnCheckedChanged;
cartList.Name = "cartList" + i.ToString();
flowLayoutPanel1.Controls.Add(cartList);
}
}
}
6.心得总结
这次勉勉强强把项目实现了,做完之后发现有很多不足的的地方,自己一开始想到的功能有部分没有实现,而且感觉winform更加适合做桌面开发,用来做这种线上购物商城很鸡肋,但是毕竟我一开始的定位就是错的.只能硬着头皮写完它了.
这次项目实现除了使用到基础的控件外,我还使用了较多的用户控件(为了实现页面切换).预想的功能是有用户上传图片作为个人头像的,但是最后只做到了用一个下拉框让用户选择已有的头像;预想是商家可以上传商品照片等商品信息的,但是做的时候忽略了,商家只能添加商品的名称,编号,价格等信息.
项目代码及数据库文件
代码及数据库文件
github:https://github.com/HXM-L/shopping_platform.git
gitee:https://gitee.com/HXM-L/shopping_platform.git
- 2021-02-12