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

2018-12-02  本文已影响0人  蜉蝣撼树浮生若梦

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

一、制作效果

制作效果.gif

二、描述界面主要功能和支持这些功能的后台数据库表结构

(一)主要功能

(1)商品查询
(2)商品修改
(3)商品删除
(二)后台数据表结构


数据库表结构.png 数据库表结构.png

三、删除数据库ADO.NETL流程

ADO.png

四、功能如何迭代(无供应商-----有供应商)

(1)在数据库中创建关于供应商的数据表并连接数据库
(2)在程序中构建查询语句
(3)将该查询过程绑定到DataAdapter
(4)将DataSet和DataAdapter绑定
(5)自定义一个表(MySupplier)来标识数据库的SUPPLIER表
(6)指定ComboBox的数据源为DataSet的MySupplier表

详见如下代码

               // 连接数据库
                sqlConn.Open();

                // 构造查询命令
                String sqlStr = "select * from SUPPLIER order by CODE";
                SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);

                // 将该查询过程绑定到DataAdapter
                SqlDataAdapter adp = new SqlDataAdapter();
                adp.SelectCommand = cmd;

                // 将DataSet和DataAdapter绑定
                DataSet ds = new DataSet();
                // 自定义一个表(MySupplier)来标识数据库的SUPPLIER表
                adp.Fill(ds, "MySupplier");

                // 指定ComboBox的数据源为DataSet的MySupplier表
                this.cbb_Supplier.DataSource = ds.Tables["MySupplier"];
                this.cbb_Supplier.DisplayMember = "NAME"; // ComboBox下拉列表显示的内容,这里显示供应商名称
                this.cbb_Supplier.ValueMember = "CODE";   // ComboBox另外还携带一个隐藏的值叫ValueMember,指定为供应商代码
                this.cbb_Supplier.SelectedIndex = 0;

五、DataGridView数据绑定流程

(1)连接数据库创建相应的数据表
(2)创建DataGridView控件
(2) 将要绑定的数据列表赋值给控件的DataPropertyName


DataGridView.png

六、重要代码

创建查询

 // 查询数据
        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();
                    }
                }

创建修改

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();
            }
        }
上一篇下一篇

猜你喜欢

热点阅读