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

描述画面主要功能以及数据库后台结构

主要功能以及后台数据库结构.gif

上图的主要界面是对数据的修改,把数据查询出来然后进行修改,然后是数据库的数据以及后台的结构。

ADO.NET删除数据的流程

ADO.NET删除数据的流程.gif

上图是删除数据的流程

画面的迭代过程

画面的迭代过程.gif

上图是画面的迭代过程。

DataGridView数据绑定流程

DataGridView数据绑定流程.gif

上图是数据绑定的流程。

重要代码片段

    private void bt_Ok_Click(object sender, EventArgs e)
        {
            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();
                    }
                }
            }

数据的删除、修改、查询的重要代码进行了整合,最后得到这里的介绍。

你可能感兴趣的:(商品信息查询修改界面功能设计)