2.7商品信息錄入界面功能設(shè)計(jì)
1.商品信息錄入界面效果圖

14042946-e13412b7f51a1b30.gif
2.商品信息錄入界面主要功能
在智慧社區(qū)商超管理系統(tǒng)中,后臺(tái)管理人員為系統(tǒng)添加新的商品基本信息
捕獲.PNG
3.ADO.NET插入數(shù)據(jù)庫的流程
具體步驟:
- 導(dǎo)入命名空間;
- 定義數(shù)據(jù)庫連接字符串,創(chuàng)建Connection對(duì)象;
- 打開連接;
- 利用Command對(duì)象的ExecuteNonQuery()方法執(zhí)行Insert語句;
- 通過ExecuteNonQuery()方法返回值判斷是否修改成功,并在界面上提示;
- 關(guān)閉連接
4.ComboBox數(shù)據(jù)綁定流程
連接數(shù)據(jù)庫
綁定數(shù)據(jù)源
構(gòu)造查詢命令
將該查詢過程綁定到DataAdapter
將DataSet和DataAdapter綁定
將DataSet和DataAdapter綁定將DataSet和DataAdapter綁定
指定ComboBox的數(shù)據(jù)源為DataSet的MySupplier
5.貼入重要代碼片段,并進(jìn)行詳細(xì)描述
編寫增強(qiáng)功能的錄入商品信息界面
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();
// 連接字符串,注意與實(shí)際環(huán)境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫
sqlConn.Open();
}
catch (Exception exp)
{
MessageBox.Show(“訪問數(shù)據(jù)庫錯(cuò)誤:” + exp.Message);
}
finally
{
sqlConn.Close();
// 構(gòu)造命令
String sqlStr = "insert into GOODS(ID, NAME, PRICE, SPEC, REMARK) values(@id, @name, @price, @spec, @remark)";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// SQL字符串參數(shù)賦值
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));
// 將命令發(fā)送給數(shù)據(jù)庫
int res = cmd.ExecuteNonQuery();
// 根據(jù)返回值判斷是否插入成功
if (res != 0)
{
MessageBox.Show("商品信息錄入成功");
}
else
{
MessageBox.Show("商品信息錄入失敗");