实例介绍
该系统工作流程:超市工作人员在管理商品时,用户对商品的价格,折扣等进行管理,并进行增删改。(1)该管理软件将对商品进行管理和统计,对产品的价格进行录入管理和统计;(2)根据需要对商品名称、价格、是否打折等进行查询,并能显示详细信息;(3)为用户提供账号管理工具,以便用户对账号进行管理,具体为登录修改用户密码;(4)提供商品的增加,删除,修改,对商品进行管理,还可以分类查看打折商品和原价商品。数据库中需要存储的数据:用户登录名、密码、商品名称、价格、是否打折、商品类别、打折价格等。
【实例截图】
【核心代码】
private void frmEditCommodity_Load(object sender, EventArgs e)
{
if (commodityID==0)//添加
{
this.GetCommoditySort();
}
else//修改
{
this.GetCommodity();
this.btnSave.Text = "修改";
}
}
private void btnSave_Click(object sender, EventArgs e)//保存
{
if (CheckInput())
{
if (commodityID==0)//新增
{
InsertCommodity();
}
else//修改
{
UpdateCommodity();
}
}
}
private void chkIsPrice_CheckStateChanged(object sender, EventArgs e) //选择被更改
{
if ( chkIsPrice .Checked)
{
this.numReducedPrice.Enabled = true;
}
else
{
this.numReducedPrice.Enabled = false;
this.numReducedPrice.Value = this.numPrice.Value;
}
}
新增商品的方法:
private void InsertCommodity()
{
DBHelper db = new DBHelper();
try
{
//sql语句
string sql = string.Format("insert into Commodity(CommodityName,SortID,CommodityPrice,IsDiscount,ReducedPrice) values('{0}','{1}','{2}','{3}','{4}')",
txtName.Text.Trim(), Convert.ToInt32(cboSort.SelectedValue), this.numPrice.Value,
this.chkIsPrice.Checked ? 1 : 0, numReducedPrice.Value);
Console.WriteLine(sql);
SqlCommand cmd = new SqlCommand(sql, db.Connection);//执行工具
db.OpenConnection();//打开数据库连接
int result = cmd.ExecuteNonQuery(); //执行
if (result==1)
{
MessageBox.Show ("添加成功","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information );
this.Close();
}
}
catch (Exception )
{
MessageBox.Show("数据库操作失败, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
}
}
修改商品的方法:
private void UpdateCommodity()
{
DBHelper helper = new DBHelper();
try
{
string sql = string.Format("update Commodity set Commodity.CommodityName ='{0}',Commodity.SortID='{1}',Commodity.CommodityPrice ='{2}', Commodity.IsDiscount ='{3}',Commodity.ReducedPrice ='{4}' where Commodity.CommodityID ='{5}'",txtName.Text.Trim(),Convert.ToInt32 (cboSort .SelectedValue),this.numPrice.Value ,this.chkIsPrice.Checked?1:0,numReducedPrice .Value ,this.commodityID);
//执行工具
SqlCommand cmd = new SqlCommand(sql.ToString(), helper.Connection);
helper.OpenConnection();//打开数据库连接
int result = cmd.ExecuteNonQuery()//执行
if (result == 1)//判断
{
MessageBox.Show("修改成功", "系统提示?", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
}
catch (Exception )
{
MessageBox.Show("数据库操作发生错误", "系统提示",MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
helper.CloseConnection();
}
}
删除商品方法:
private void DeleteCommodityByID()
{
if (this.dgvCommodity.CurrentRow != null)
{
DialogResult dr = MessageBox.Show("确定要删除名称为" dgvCommodity.CurrentRow.Cells[1].Value, "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (dr == DialogResult.OK)
{
DBHelper helper = new DBHelper();
try
{
//sql语句
StringBuilder sb = new StringBuilder();
sb.AppendFormat("delete from Commodity where CommodityID={0}", Convert.ToInt32(dgvCommodity.CurrentRow.Cells[0].Value));
//执行
SqlCommand cmd = new SqlCommand(sb.ToString(), helper.Connection);
//打开数据库连接
helper.OpenConnection();
//执行
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
MessageBox.Show("删除成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//重新绑定dgv
this.FillCommodityInfo();
}
}
catch (Exception)
{
MessageBox.Show("数据库操作失败, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
helper.CloseConnection();
}
}
}
}
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)