美文网首页
商品信息查询修改界面功能设计

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

作者: 小芳芳925 | 来源:发表于2018-05-23 21:07 被阅读0次

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

    1.效果图

    查询 修改.gif

    2.描述画面主要功能,并列出后台数据库表结构

    对商品信息进行查询修改
    300000 圆珠笔 3 902 1
    300001 海飞丝 25 900 中瓶
    300002 潘婷 21.2 900 中瓶
    300003 订书机 13 902 A4
    300004 矿泉水 1.5 901 500mL
    300005 矿泉水 5 901 2L
    300006 佳洁士 8 900 150mL
    300007 吉列 25 900 锋速三

    3.ADO.NET删除数据库的流程

    流程图.PNG

    1.导入命名空间
    2.定义数据库连接字符串,运行Connection对象建立与数据库连接
    3.打开连接
    4.利用Command对象的ExecuteNoQuery()方法执行Delete语句
    5.通过ExecuteNoQuery()方法返回值判断是否修改成功,并在界面上提示
    6.关闭连接

    4.描述迭代过程

    无.PNG
    有.png
    String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
                SqlConnection sqlConn = new SqlConnection(connStr);
                try
                {
                    // 连接数据库
                    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;
                }
                catch (Exception exp)
                {
                    MessageBox.Show("访问数据库错误:" + exp.Message);
                }
                finally
                {
                    sqlConn.Close();
                }
    

    5.DataGridView数据绑定流程

    // 指定DataGridView的数据源为DataSet的MyGoods表
    this.dgv_Goods.DataSource = ds.Tables["MyGoods"];
    

    6.重要代码

    修改

     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_Cancel_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }
    

    查询

    // 连接字符串,注意与实际环境保持一致
    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"];
    
    
        // DataGridView数据绑定
    }
    catch (Exception exp)
    {
        MessageBox.Show("访问数据库错误:" + exp.Message);
    }
    finally
    {
        sqlConn.Close();
    }
    

    相关文章

      网友评论

          本文标题:商品信息查询修改界面功能设计

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