商品信息查询修改界面功能设计
1.1 任务内容
在智慧社区商超管理系统中,后台管理人员查询系统已有商品信息,然后对某条信息进行相应操作(如:修改或者删除)。查询信息也是信息管理系统的一项基础功能。
1.2 任务目标
(1)进一步认识ADO.NET;
(2)了解C#实现数据库数据查询、修改、删除的过程;
(3)了解C#数据库编程中DataGridView控件的使用方法。
2.1 效果图
上图实现了对数据库的修改与删除操作。
2.2 支持这些功能的后台数据库表结构
[ID] [varchar](50) NOT NULL,
[NAME] [varchar](20) NULL,
[PRICE] [float] NULL,
[SPEC] [varchar](20) NULL,
[REMARK] [varchar](100) NULL,
2.3 ADO.NET删除数据库的流程
(1) 导入命名空间;
(2) 定义数据库连接字符串,创建Connection对象;
(3)打开连接;
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
(4)连接数据库;
sqlConn.Open();
(5)删除语句构造命令;
String sqlStr = "delete from GOODS where ID=@id";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
(6)SQL字符串参数赋值
cmd.Parameters.Add(new SqlParameter("@id", goodsId));
(7)将命令发送给数据库
int res = cmd.ExecuteNonQuery();
(8)根据返回值判断是否修改成功
if (res != 0)
{
MessageBox.Show("删除成功");
}
else
{
MessageBox.Show("删除失败");
}
(9)关闭连接;
finally
{
sqlConn.Close();
}
2.4 画面迭代流程
2.5 DataGridView数据绑定流程
2.6主要代码
{
String id = this.tb_Id.Text.Trim();
String name = this.tb_Name.Text.Trim();
float price = float.Parse(this.tb_Price.Text.Trim());
String spec = this.tb_Spec.Text.Trim();
String remark = this.tb_Remark.Text.Trim();
// 连接字符串,注意与实际环境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 连接数据库
sqlConn.Open();
// 构造命令
String sqlStr = "update GOODS set NAME=@name, PRICE=@price, SPEC=@spec, REMARK=@remark where ID=@id";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// SQL字符串参数赋值
cmd.Parameters.Add(new SqlParameter("@id", id));
cmd.Parameters.Add(new SqlParameter("@name", name));
cmd.Parameters.Add(new SqlParameter("@price", price));
cmd.Parameters.Add(new SqlParameter("@spec", spec));
cmd.Parameters.Add(new SqlParameter("@remark", remark));
// 将命令发送给数据库
int res = cmd.ExecuteNonQuery();
// 根据返回值判断是否修改成功
if (res != 0)
{
MessageBox.Show("商品信息修改成功");
this.Close();
}
else
{
MessageBox.Show("商品信息修改失败");
}
}
catch (Exception exp)
{
MessageBox.Show("访问数据库错误:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
//数据的查询
private void bt_Query_Click(object sender, EventArgs e)
{
// 连接字符串,注意与实际环境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 连接数据库
sqlConn.Open();
// 构造命令
String sqlStr = "select * from GOODS where 1=1 ";
// 添加查询条件
if (!this.tb_Id.Text.Trim().Equals(""))
{
sqlStr += " and ID='" + this.tb_Id.Text.Trim() + "'";
}
if (!this.tb_Name.Text.Trim().Equals(""))
{
sqlStr += " and NAME like '%" + this.tb_Name.Text.Trim() + "%'";
}
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// 将该查询过程绑定到DataAdapter
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
// 将DataSet和DataAdapter绑定
DataSet ds = new DataSet();
// 自定义一个表(MyGoods)来标识数据库的GOODS表
adp.Fill(ds, "MyGoods");
// 指定DataGridView的数据源为DataSet的MyGoods表
this.dgv_Goods.DataSource = ds.Tables["MyGoods"];
}
catch (Exception exp)
{
MessageBox.Show("访问数据库错误:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
// 数据修改,删除
private void dgv_Goods_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// 点击修改链接
if (e.RowIndex != -1 && e.ColumnIndex == 0)
{
// 获取所要修改关联对象的主键
string goodsId = this.dgv_Goods["Id", e.RowIndex].Value.ToString();
ModifyForm modifyForm = new ModifyForm(goodsId);
modifyForm.Show();
}
else if (e.RowIndex != -1 && e.ColumnIndex == 1)
{
if (MessageBox.Show("确认删除?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
// 获取所要删除关联对象的主键
string goodsId = this.dgv_Goods["Id", e.RowIndex].Value.ToString();
// 连接字符串,注意与实际环境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 连接数据库
sqlConn.Open();
// 构造命令
String sqlStr = "delete from GOODS where ID=@id";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// SQL字符串参数赋值
cmd.Parameters.Add(new SqlParameter("@id", goodsId));
// 将命令发送给数据库
int res = cmd.ExecuteNonQuery();
// 根据返回值判断是否修改成功
if (res != 0)
{
MessageBox.Show("删除成功");
}
else
{
MessageBox.Show("删除失败");
}
}
catch (Exception exp)
{
MessageBox.Show("访问数据库错误:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
}