1.效果图
2.主要功能及其后台数据表库结构
2.1主要功能
录入商品信息并更新到数据库
2.2后台数据库表结构
3.ADO.NET插入数据库流程
14025070-2e483d9839558bfa.png
4.画面功能迭代过程
界面无外键的时候是没有供应商的选项,用的是GOODSINFO表在这个数据库表中是没有外键的,通过添加供应商这一列的外键实现从数据库GOODS表中直接用数据库中SUPPLIER表中的数据。
5.ComboBox数据绑定流程
6.贴入重要代码
1.连接数据并绑定
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();
}
catch (Exception exp)
{
MessageBox.Show(“访问数据库错误:” + exp.Message);
}
finally
{
sqlConn.Close();
}
2.向数据库中插入数据
// 构造命令
String sqlStr = "insert into GOODS(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("商品信息录入失败");
}