2.8 商品信息錄入界面功能設(shè)計(jì)
在智慧社區(qū)商超管理系統(tǒng)中蜂林,后臺(tái)管理人員為系統(tǒng)添加新的商品基本信息是一項(xiàng)基礎(chǔ)工作喊递。因此需設(shè)計(jì)并制作智慧社區(qū)商超管理系統(tǒng)的商品信息錄入界面兢仰,并實(shí)現(xiàn)其功能匠璧。
gif
主要功能
點(diǎn)擊錄入商品信息芽狗,進(jìn)入錄入商品信息界面狡蝶,輸入商品條碼庶橱、商品名稱、商品價(jià)格贪惹。商品規(guī)格苏章、供應(yīng)商以及備注等信息,點(diǎn)擊錄入按鈕即可錄入成功
相關(guān)后臺(tái)數(shù)據(jù)庫(kù)表結(jié)構(gòu)
PNG
ADO.NET插入數(shù)據(jù)庫(kù)的流程
具體步驟
1.導(dǎo)入命名空間馍乙;
2.定義數(shù)據(jù)庫(kù)連接字符串布近,運(yùn)用Connection對(duì)象建立與數(shù)據(jù)庫(kù)連接垫释;
3.打開(kāi)連接;
4.利用Command對(duì)象的ExecuteNoQuery()方法執(zhí)行Insert語(yǔ)句撑瞧;
5.通過(guò)ExecuteNoQuery()方法返回值判斷是否修改成功棵譬,并在界面上提示;
6.關(guān)閉連接预伺。
ComboBox數(shù)據(jù)綁定流程
{
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫(kù)
sqlConn.Open(); // 綁定數(shù)據(jù)源
}
catch (Exception exp)
{
MessageBox.Show("訪問(wèn)數(shù)據(jù)庫(kù)錯(cuò)誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
// 構(gòu)造查詢命令
String sqlStr = "select * from SUPPLIER order by CODE";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// 將該查詢過(guò)程綁定到DataAdapter
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
// 將DataSet和DataAdapter綁定
DataSet ds = new DataSet();
// 自定義一個(gè)表(MySupplier)來(lái)標(biāo)識(shí)數(shù)據(jù)庫(kù)的SUPPLIER表
adp.Fill(ds, "MySupplier");
// 指定ComboBox的數(shù)據(jù)源為DataSet的MySupplier表
this.comboBox1.DataSource = ds.Tables["MySupplier"];
this.comboBox1.DisplayMember = "NAME";
this.comboBox1.ValueMember = "CODE";
this.comboBox1.SelectedIndex = 0;
重要代碼
String sqlStr = "insert into GOODSINFO(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ù)庫(kù)
int res = cmd.ExecuteNonQuery();
// 根據(jù)返回值判斷是否插入成功
if (res != 0)
{
MessageBox.Show("商品信息錄入成功");
}
else
{
MessageBox.Show("商品信息錄入失敗");
}