美文网首页
2.8任务报告

2.8任务报告

作者: 1753110d427b | 来源:发表于2018-12-07 15:55 被阅读0次

商品信息查询修改界面功能设计

1.1 任务内容

在智慧社区商超管理系统中,后台管理人员查询系统已有商品信息,然后对某条信息进行相应操作(如:修改或者删除)。查询信息也是信息管理系统的一项基础功能。

1.2 任务目标

(1)进一步认识ADO.NET;

(2)了解C#实现数据库数据查询、修改、删除的过程;

(3)了解C#数据库编程中DataGridView控件的使用方法。

2.1 效果图

223.gif

上图实现了对数据库的修改与删除操作。

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 画面迭代流程

223.gif

2.5 DataGridView数据绑定流程

14023516-01197db63ee082e8.gif

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();
                    }
                }
            }

相关文章

  • 2.8任务报告

    商品信息查询修改界面功能设计 1.1 任务内容 在智慧社区商超管理系统中,后台管理人员查询系统已有商品信息,然后对...

  • 任务报告

    “我是唐峻川,现在开始任务阶段汇报。” “81196突击小队已经抵达沃斯卡娅工业区中心,正在进入核电站区域。从安全...

  • 第六组总结

    第六组总结 小组分工 杜将军 《任务2.8》、《第六组总结》 李易林《任务2.7》 兰小雯《任务2.3》、《任务2...

  • 项目总结汇报

    第一章 小组成员分工 李想:完成任务2.3、2.4、2.10刘宇:完成任务2.8、2.9王俊杰:完成任务2.5王子...

  • 【产品经理第一课】APP消息推送のWhy

    根据市场分析公司 Localytics 基于 15 亿部设备和 2.8 万个应用的调研报告,如果用户打开了一款应用...

  • same产品分析报告

    same产品分析报告 主要内容 市场环境 产品定位 产品主要功能分析 产品建议 展望 测试环境 产品版本:2.8....

  • 2018-12-03

    任务2.8 商品信息查询界面功能设计 制作人:第二组 徐黎明 1.任务介绍 基于智慧社区开发的智慧商超系统 在智...

  • iTrade产品分析报告|第三方独立理财师信赖的全球资产配置工作

    这是一篇关于iTrade APP的产品分析报告,体验版本为iOS2.8版本,仅供参考。 分析目录: 1.产品概况 ...

  • 2018-09-12

    9月12日任务 2.6 相对和绝对路径 2.7 cd命令 2.8 创建和删除目录mkdir/rmdir 2.9 r...

  • 目录操作

    9月12日任务2.6 相对和绝对路径2.7 cd命令2.8 创建和删除目录mkdir/rmdir2.9 rm命令 ...

网友评论

      本文标题:2.8任务报告

      本文链接:https://www.haomeiwen.com/subject/wupecqtx.html