1.效果圖
2.8效果圖.gif
2.描述畫面主要功能损趋,并列出支持這些功能的后臺數(shù)據(jù)庫表結(jié)構(gòu)
1.主要功能:
- 查詢商品信息功能
- 修改商品信息功能
- 刪除商品信息功能
2.后臺數(shù)據(jù)庫表結(jié)構(gòu):
1.PNG
2.PNG
3.ADO.NET刪除數(shù)據(jù)庫的流程
AN刪除數(shù)據(jù)庫流程.png
AN刪除數(shù)據(jù)庫具體步驟:
- 導(dǎo)入命名空間;
- 定義數(shù)據(jù)庫連接字符串,創(chuàng)建Connection對象纤泵;
- 打開連接;
- 利用Command對象的ExecuteNonQuery()方法執(zhí)行Delete語句炕倘;
- 通過ExecuteNonQuery()方法返回值判斷是否修改成功隐孽,并在界面上提示;
- 關(guān)閉連接躺坟。
4.畫面功能是如何迭代的,描述迭代過程(無供應(yīng)商——有供應(yīng)商)
try
{
// 連接數(shù)據(jù)庫
sqlConn.Open();
// 構(gòu)造查詢命令
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();
// 自定義一個(gè)表(MySupplier)來標(biāo)識數(shù)據(jù)庫的SUPPLIER表
adp.Fill(ds, "MySupplier");
// 指定ComboBox的數(shù)據(jù)源為DataSet的MySupplier表
this.cbb_Supplier.DataSource = ds.Tables["MySupplier"];
this.cbb_Supplier.DisplayMember = "NAME"; // ComboBox下拉列表顯示的內(nèi)容乳蓄,這里顯示供應(yīng)商名稱
this.cbb_Supplier.ValueMember = "CODE"; // ComboBox另外還攜帶一個(gè)隱藏的值叫ValueMember咪橙,指定為供應(yīng)商代碼
this.cbb_Supplier.SelectedIndex = 0;
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫錯(cuò)誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
從無供應(yīng)商到有供應(yīng)商的迭代流程:
連接數(shù)據(jù)庫,并構(gòu)建查詢語句對supplier表進(jìn)行查詢虚倒;
將查詢到的信息與DataAdapter綁定美侦;
將DataSet和DataAdapter綁定;
自定義一個(gè)表(MySupplier)來標(biāo)識數(shù)據(jù)庫的SUPPLIER表魂奥;
指定ComboBox的數(shù)據(jù)源菠剩。
5.DataGridView數(shù)據(jù)綁定流程
// 將該查詢過程綁定到DataAdapter
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
// 將DataSet和DataAdapter綁定
DataSet ds = new DataSet();
// 指定DataGridView的數(shù)據(jù)源為DataSet的MyGoods表
this.dgv_Goods.DataSource = ds.Tables["MyGoods"];
6.貼入重要代碼片段,并進(jìn)行詳細(xì)描述
- 構(gòu)建查詢語句以及添加查詢條件
// 構(gòu)造命令
String sqlStr = "select * from GOODS where 1=1 ";
// 添加查詢條件
if (!this.tb_Id.Text.Trim().Equals(""))
{
sqlStr += " and ID='" + this.tb_Id.Text.Trim() + "'";
}
if (!this.tb_Name.Text.Trim().Equals(""))
{
sqlStr += " and NAME like '%" + this.tb_Name.Text.Trim() + "%'";
}
- 自定義一個(gè)用以標(biāo)識數(shù)據(jù)庫的表
// 自定義一個(gè)表(MyGoods)來標(biāo)識數(shù)據(jù)庫的GOODS表
adp.Fill(ds, "MyGoods");
- 修改商品信息函數(shù)
// 點(diǎn)擊修改鏈接
if (e.RowIndex != -1 && e.ColumnIndex == 0)
{
// 獲取所要修改關(guān)聯(lián)對象的主鍵
string goodsId = this.dgv_Goods["Id", e.RowIndex].Value.ToString();
ModifyForm modifyForm = new ModifyForm(goodsId);
modifyForm.Show();
}
else if (e.RowIndex != -1 && e.ColumnIndex == 1)
- 刪除商品信息函數(shù)
// 構(gòu)造命令
String sqlStr = "delete from GOODS where ID=@id";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// SQL字符串參數(shù)賦值
cmd.Parameters.Add(new SqlParameter("@id", goodsId));
// 將命令發(fā)送給數(shù)據(jù)庫
int res = cmd.ExecuteNonQuery();
- 判斷是否修改成功
// 根據(jù)返回值判斷是否修改成功
if (res != 0)
{
MessageBox.Show("刪除成功");
}
else
{
MessageBox.Show("刪除失敗");
}