2018-05-23

2018-05-23  本文已影响0人  眼中的星星

2.8 商品信息录入界面功能设计

2.8.1 GIF图片

2.8.1.gif

2.8.2 主要功能

2.8.2.1 快速登入录入商品信息
2.8.2.2 快速修改其密码

2.8.3 数据库表结构

数据库表1.PNG 数据库表2.PNG

2.8.4 ADO.NET插入数据库的流程

ADO.NET.PNG

具体步骤:

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

2.8.5 迭代过程

(1)无外键


无外键.png

(2)有外键


有外键.PNG

代码

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;

2.8.6ComboBox数据绑定流程

ComboBox.PNG

ComboBox数据源绑定的三个要素:
1) 设置DataSource属性
2) 设置DisplayMember属性
3) 设置ValueManager属性
代码

this.comboBox1.DataSource = ds.Tables["MySupplier"];
this.comboBox1.DisplayMember = "NAME";
this.comboBox1.ValueMember = "CODE";

2.8.7 重要代码片段

(1)向数据库中插入数据

 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 = "insert into GOODSINFO(ID, NAME, PRICE, SPEC, REMARK) values(@id, @name, @price, @spec, @remark)";
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("商品信息录入成功");
}
else
{
    MessageBox.Show("商品信息录入失败");
}


}
catch (Exception exp)
{
    MessageBox.Show(“访问数据库错误:” + exp.Message);
}
finally
{
    sqlConn.Close();
}

(2)ComboBox数据源绑定实践

private void Form1_Load(object sender, EventArgs e)
{
    String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
    SqlConnection sqlConn = new SqlConnection(connStr);
    try
    {
        // 连接数据库
        sqlConn.Open();

        // 绑定数据源
    }
    catch (Exception exp)
    {
        MessageBox.Show("访问数据库错误:" + exp.Message);
    }
    finally
    {
        sqlConn.Close();
    }
}
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.comboBox1.DataSource = ds.Tables["MySupplier"];
this.comboBox1.DisplayMember = "NAME";
this.comboBox1.ValueMember = "CODE";
this.comboBox1.SelectedIndex
上一篇下一篇

猜你喜欢

热点阅读