2018-12-04 2.7 商品信息錄入界面功能設(shè)計(jì)

1.貼效果圖,最好是GIF文件

1.gif

2.描述畫(huà)面主要功能,并列出支持這些功能的后臺(tái)數(shù)據(jù)庫(kù)表結(jié)構(gòu)

導(dǎo)入命名空間;

定義數(shù)據(jù)庫(kù)連接字符串,創(chuàng)建Connection對(duì)象;

打開(kāi)連接;

利用Command對(duì)象的ExecuteNonQuery()方法執(zhí)行Insert語(yǔ)句;

通過(guò)ExecuteNonQuery()方法返回值判斷是否修改成功,并在界面上提示;

關(guān)閉連接。

3.ADO.NET插入數(shù)據(jù)庫(kù)的流程

訪問(wèn)SQL Server數(shù)據(jù)庫(kù)?

添加數(shù)據(jù)庫(kù)數(shù)據(jù).png

4.畫(huà)面功能是如何迭代的,描述迭代過(guò)程(無(wú)外鍵?有外鍵)

在商品錄入界面添加一個(gè)供應(yīng)商及ComboBox控件 編寫(xiě)代碼讓ComboBox的數(shù)據(jù)源為DataSet的MySupplier表

// 將該查詢過(guò)程綁定到DataAdapter

SqlDataAdapter adp =newSqlDataAdapter();? ? ?

?? ? ? ? ? adp.SelectCommand = cmd;

// 將DataSet和DataAdapter綁定

DataSet ds =newDataSet();

// 自定義一個(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";

// ComboBox下拉列表顯示的內(nèi)容,這里顯示供應(yīng)商名稱

this.comboBox1.ValueMember ="CODE";

// ComboBox另外還攜帶一個(gè)隱藏的值叫ValueMember,指定為供應(yīng)商代碼? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

this.comboBox1.SelectedIndex = 0;

5.ComboBox數(shù)據(jù)綁定流程

1)連接數(shù)據(jù)庫(kù)

privatevoidForm1_Load(object sender, EventArgs e)

{

? ? ? ? ? ? String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;? ? ? ? ? ? SqlConnection sqlConn =newSqlConnection(connStr);

try{

// 連接數(shù)據(jù)庫(kù)

sqlConn.Open();

// 構(gòu)造查詢命令

String sqlStr ="select * from SUPPLIER order by CODE";? ? ?

?? ? ? ? ? SqlCommand cmd =newSqlCommand(sqlStr, sqlConn);

2)將該查詢過(guò)程綁定到DataAdapter

? ? ?DataAdapterSqlDataAdapter adp =newSqlDataAdapter();? ? ? ??

? ? ? adp.SelectCommand = cmd;

? ? ? // 將DataSet和DataAdapter綁定DataSet?

? ? ? ?ds =newDataSet();

3)自定義一個(gè)表(MySupplier)來(lái)標(biāo)識(shí)數(shù)據(jù)庫(kù)的SUPPLIER表

// 自定義一個(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";

// ComboBox下拉列表顯示的內(nèi)容,這里顯示供應(yīng)商名稱

this.comboBox1.ValueMember ="CODE";

// ComboBox另外還攜帶一個(gè)隱藏的值叫ValueMember,指定為供應(yīng)商代碼? ? ? ? ? ? ? ? this.comboBox1.SelectedIndex =0;??

? ? ? ? ? }

catch(Exceptionexp)? ? ? ??

? ? {? ? ? ? ?

?? MessageBox.Show("訪問(wèn)數(shù)據(jù)庫(kù)錯(cuò)誤:"+exp.Message);? ??

? ? ? ? }? ? ? ?

?? ? finally? ??

? ? ? ? {? ? ? ? ? ?

?? ? sqlConn.Close();? ? ? ??

? ? }? ??

? ? }

privatevoidcomboBox1_SelectedIndexChanged(object sender, EventArgs e

){? ? ??

? ? MessageBox.Show(this.comboBox1.Text.ToString() +", "+this.comboBox1.SelectedValue.ToString());

}

6,貼入重要代碼片段,并進(jìn)行詳細(xì)描述

// 點(diǎn)擊“確認(rèn)”按鈕

privatevoidbt_Ok_Click(object sender, EventArgs e)? ??

? ? {

StringuserName =this.tb_User.Text.Trim();

StringnewPwd =this.tb_NewPwd.Text.Trim();

StringconfPwd =this.tb_ConfirmPwd.Text.Trim();

修改密碼代碼

// 驗(yàn)證輸入信息

if(newPwd.Equals(""))? ? ??

? ? ? {? ? ? ? ?

?? ? ? MessageBox.Show("請(qǐng)輸入新密碼","提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

return;? ? ?

?? ? ? }

elseif(confPwd.Equals(""))? ? ? ?

?? ? {? ? ? ??

? ? ? ? MessageBox.Show("請(qǐng)輸入確認(rèn)密碼","提示", MessageBoxButtons.OK,MessageBoxIcon.Warning);

return;? ? ??

? ? ? }

elseif

(newPwd != confPwd)? ? ?

?? ? ? {? ? ? ?

?? ? ? ? MessageBox.Show("兩次密碼不一致","提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

return;? ??

? ? ? ? }

判斷是否修改成功

// 連接字符串,注意與實(shí)際環(huán)境保持一致StringconnStr=ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;? ? ? ? ? SqlConnection sqlConn =newSqlConnection(connStr);

try{// 連接數(shù)據(jù)庫(kù)sqlConn.Open();

// 構(gòu)造命令String sqlStr ="update EMPLOYEE set PASSWORD=@pwd where ID=@id";? ? ? ??

? ? ? SqlCommand cmd =newSqlCommand(sqlStr, sqlConn);

// SQL字符串參數(shù)賦值? ? ??

? ? ? ? ? cmd.Parameters.Add(new SqlParameter("@pwd",newPwd));

cmd.Parameters.Add(newSqlParameter("@id", UserInfo.userId));

// 將命令發(fā)送給數(shù)據(jù)庫(kù)intres = cmd.ExecuteNonQuery();

// 根據(jù)返回值判斷是否修改成功if(res !=0)? ? ? ?

?? ? ? {? ? ? ? ? ??

? ? ? MessageBox.Show("密碼修改成功");

this.Close();?

?? ? ? ? ? ? }

else{? ? ? ??

? ? ? ? ? MessageBox.Show("密碼修改錯(cuò)誤");? ? ? ? ?

?? ? }? ? ? ? ? }catch(Exceptionexp)? ??

? ? ? {? ? ? ? ??

? ? MessageBox.Show("訪問(wèn)數(shù)據(jù)庫(kù)錯(cuò)誤:"+exp.ToString());? ? ?

?? ? }? ? ? ?

?? finally? ? ??

? ? {? ? ? ??

? ? ? sqlConn.Close();? ??

? ? ? }? ??

? }

回車直接修改密碼

// 在“新密碼”輸入框中按“回車”,光標(biāo)跳轉(zhuǎn)到“確認(rèn)密碼”輸入框

privatevoidtb_NewPwd_KeyPress(object sender, KeyPressEventArgs e)

{

if(e.KeyChar == (char)Keys.Enter)??

? ? ? ? ? {? ? ? ??

? ? ? ? SendKeys.Send("{tab}");? ? ? ?

?? ? }? ? ?

?? }

// 在“確認(rèn)密碼”輸入框中按“回車”,則直接修改密碼

privatevoidtb_ConfirmPwd_KeyPress(object sender, KeyPressEventArgs e)

{

if(e.KeyChar == (char)Keys.Enter)? ? ? ?

?? ? {

this.bt_Ok_Click(sender, e);? ? ? ?

?? ? }? ? ?

?? }??

? }

錄入商品代碼

// 點(diǎn)擊“確認(rèn)”按鈕,則錄入商品

privatevoidbt_Ok_Click(object sender, EventArgs e)

{? ? ? ? ?

?? String id =this.tb_Id.Text.Trim();??

? ?String name =this.tb_Name.Text.Trim();

? ? floatprice =float.Parse(this.tb_Price.Text.Trim());? ? ?

? ? String spec =this.tb_Spec.Text.Trim();? ? ??

? ? String remark =this.tb_Remark.Text.Trim();

// 連接字符串,注意與實(shí)際環(huán)境保持一致

StringconnStr=ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;? ? ? ? ? ? SqlConnection sqlConn =newSqlConnection(connStr);

try{// 連接數(shù)據(jù)庫(kù)sqlConn.Open();

// 構(gòu)造命令

String sqlStr ="insert into GOODS2(ID, NAME, PRICE, SPEC, REMARK) values(@id, @name, @price, @spec, @remark)";? ? ? ? ? ??

? ? SqlCommand cmd =newSqlCommand(sqlStr, sqlConn);

// SQL字符串參數(shù)賦值

cmd.Parameters.Add(newSqlParameter("@id", id));? ?

cmd.Parameters.Add(newSqlParameter("@name", name));? ? ? ? ? ? ? ? cmd.Parameters.Add(newSqlParameter("@price", price));? ? ? ? ? ? ? ? cmd.Parameters.Add(newSqlParameter("@spec", spec));? ? ? ? ? ? ? ? cmd.Parameters.Add(newSqlParameter("@remark", remark));

// 將命令發(fā)送給數(shù)據(jù)庫(kù)

intres = cmd.ExecuteNonQuery();

判斷商品是否錄入成功代碼

// 根據(jù)返回值判斷是否錄入成功

if(res !=0)? ? ? ??

? ? ? ? {? ? ? ? ?

?? ? ? ? ? MessageBox.Show("商品信息錄入成功");? ? ? ?

?? ? ? ? }

else{? ? ? ? ??

? ? ? ? ? MessageBox.Show("商品信息錄入失敗");? ? ? ?

?? ? ? ? }? ? ? ??

? ? }

catch(Exceptionexp)? ? ??

? ? ? {? ? ? ? ? ?

?? ? MessageBox.Show("訪問(wèn)數(shù)據(jù)庫(kù)錯(cuò)誤:"+exp.ToString());? ? ? ??

? ? }? ? ??

? ? ? finally??

? ? ? ? ? {? ? ? ?

?? ? ? ? sqlConn.Close();? ? ?

?? ? ? }??

? ? ? }? ?

?}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容