1.效果圖

圖1.商品信息查詢界面
2.主要功能及數(shù)據(jù)表結(jié)構(gòu)
2.1-主要功能
后臺(tái)管理人員查詢系統(tǒng)已有商品信息,然后對(duì)某條信息進(jìn)行相應(yīng)操作(如:修改或者刪除)。查詢信息也是信息管理系統(tǒng)的一項(xiàng)基礎(chǔ)功能。
2.2-所需的后臺(tái)數(shù)據(jù)庫(kù)表結(jié)構(gòu)
表名:GOODS
| 列名 | 數(shù)據(jù)類型 | 允許null值 |
|---|---|---|
| ID | varchar(50) | × |
| NAME | varchar(50) | √ |
| SUPPLIER | int | √ |
| SPEC | varchar(20) | √ |
| REMARK | varchar(100) | √ |
圖2.GOODS表
表名:SUPPLIER
| 列名 | 數(shù)據(jù)類型 | 允許null值 |
|---|---|---|
| CODE | int | × |
| NAME | varchar(100) | √ |
| LOCATION | varchar(100) | √ |
| CONTRACTS | varchar(50) | √ |
| PHONE | varchar(15) | √ |
圖3.SUPPLIER表
3.ADO.NET刪除數(shù)據(jù)庫(kù)的流程

圖4.ADO.NET刪除
具體步驟:
- 導(dǎo)入命名空間;
- 定義數(shù)據(jù)庫(kù)連接字符串,運(yùn)用Connection對(duì)象建立與數(shù)據(jù)庫(kù)連接;
- 打開(kāi)連接;
- 利用Command對(duì)象的ExecuteNoQuery()方法執(zhí)行Delete語(yǔ)句;
- 通過(guò)ExecuteNoQuery()方法返回值判斷是否修改成功,并在界面上提示;
- 關(guān)閉連接。
4.迭代過(guò)程(無(wú)供應(yīng)商>>有供應(yīng)商)
4.1-添加comboBox控件
圖5.無(wú)供應(yīng)商
圖6.有供應(yīng)商
4.2-綁定ComboBox,初始化下拉列表
1.連接數(shù)據(jù)庫(kù);
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
sqlConn.Open();
2.構(gòu)造查詢命令;
String sqlStr = "select * from SUPPLIER order by CODE";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
3.將該查詢過(guò)程綁定到DataAdapter;
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
4.將DataSet和DataAdapter綁定;
DataSet ds = new DataSet();
5.自定義一個(gè)表(MySupplier)來(lái)標(biāo)識(shí)數(shù)據(jù)庫(kù)的SUPPLIER表;
adp.Fill(ds, "MySupplier");
6.指定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;
5.DataGridView數(shù)據(jù)綁定流程
- 構(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() + "%'";
}
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
- 將該查詢過(guò)程綁定到DataAdapter;
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
- 將DataSet和DataAdapter綁定;
DataSet ds = new DataSet();
- 自定義一個(gè)表(MyGoods)來(lái)標(biāo)識(shí)數(shù)據(jù)庫(kù)的GOODS表;
adp.Fill(ds, "MyGoods");
- 指定DataGridView的數(shù)據(jù)源為DataSet的MyGoods表。
this.dgv_Goods.DataSource = ds.Tables["MyGoods"];
6.重要代碼片段
6.1-查詢商品信息
編寫商品信息查詢列表代碼:
1) 代碼編寫位置
“查詢”按鈕點(diǎn)擊后;
2) 代碼編寫
利用ADO .NET技術(shù)實(shí)現(xiàn)數(shù)據(jù)類別查詢。
具體步驟:
- 導(dǎo)入命名空間;
- 定義數(shù)據(jù)庫(kù)連接字符串,運(yùn)用Connection對(duì)象建立與數(shù)據(jù)庫(kù)連接;
- 打開(kāi)連接;
- 利用DataAdapter對(duì)象,建立與數(shù)據(jù)庫(kù)的連接橋;
- 通過(guò)DataAdapter橋,將查詢結(jié)果存儲(chǔ)到DataSet對(duì)象中;
- 利用DataGridView控件將DataSet中的查詢結(jié)果顯示出來(lái)
- 關(guān)閉連接。
// 連接數(shù)據(jù)庫(kù)
sqlConn.Open();
// 構(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() + "%'";
}
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// 將該查詢過(guò)程綁定到DataAdapter
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
// 將DataSet和DataAdapter綁定
DataSet ds = new DataSet();
// 自定義一個(gè)表(MyGoods)來(lái)標(biāo)識(shí)數(shù)據(jù)庫(kù)的GOODS表
adp.Fill(ds, "MyGoods");
// 指定DataGridView的數(shù)據(jù)源為DataSet的MyGoods表
this.dgv_Goods.DataSource = ds.Tables["MyGoods"];
6.2-修改商品信息
(1)窗口加載后,顯示商品信息
運(yùn)用ADO.NET實(shí)現(xiàn)數(shù)據(jù)庫(kù)查詢;
(2)點(diǎn)擊修改后,修改商品信息
運(yùn)用ADO.NET實(shí)現(xiàn)數(shù)據(jù)庫(kù)修改。
具體步驟:
- 導(dǎo)入命名空間;
- 定義數(shù)據(jù)庫(kù)連接字符串,運(yùn)用Connection對(duì)象建立與數(shù)據(jù)庫(kù)連接;
- 打開(kāi)連接;
- 利用Command對(duì)象的ExecuteNoQuery()方法執(zhí)行Update語(yǔ)句;
- 通過(guò)ExecuteNoQuery()方法返回值判斷是否修改成功,并在界面上提示;
- 關(guān)閉連接。
// 構(gòu)造命令
String sqlStr = "update GOODS set NAME=@name, PRICE=@price, SPEC=@spec, REMARK=@remark, SUPPLIER=@supplier where ID=@id";
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));
cmd.Parameters.Add(new SqlParameter("@supplier", supplier));
// 將命令發(fā)送給數(shù)據(jù)庫(kù)
int res = cmd.ExecuteNonQuery();
// 根據(jù)返回值判斷是否修改成功
if (res != 0)
{
MessageBox.Show("商品信息修改成功");
this.Close();
}
else
{
MessageBox.Show("商品信息修改失敗");
}
6.3-刪除商品信息
點(diǎn)擊刪除后,提示確認(rèn),確認(rèn)后刪除
運(yùn)用ADO.NET實(shí)現(xiàn)數(shù)據(jù)庫(kù)刪除。
具體步驟:
- 導(dǎo)入命名空間;
- 定義數(shù)據(jù)庫(kù)連接字符串,運(yùn)用Connection對(duì)象建立與數(shù)據(jù)庫(kù)連接;
- 打開(kāi)連接;
- 利用Command對(duì)象的ExecuteNoQuery()方法執(zhí)行Delete語(yǔ)句;
- 通過(guò)ExecuteNoQuery()方法返回值判斷是否修改成功,并在界面上提示;
- 關(guān)閉連接。
if (MessageBox.Show("確認(rèn)刪除?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
// 獲取所要?jiǎng)h除關(guān)聯(lián)對(duì)象的主鍵
string objectId = this.dgv_Goods["Id", e.RowIndex].Value.ToString();
// 連接字符串,注意與實(shí)際環(huán)境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫(kù)
sqlConn.Open();
// 構(gòu)造命令
String sqlStr = "delete from GOODS where ID=@id";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// SQL字符串參數(shù)賦值
cmd.Parameters.Add(new SqlParameter("@id", objectId));
// 將命令發(fā)送給數(shù)據(jù)庫(kù)
int res = cmd.ExecuteNonQuery();
// 根據(jù)返回值判斷是否修改成功
if (res != 0)
{
MessageBox.Show("刪除成功");
}
else
{
MessageBox.Show("刪除失敗");
}
}
catch (Exception exp)
{
MessageBox.Show("訪問(wèn)數(shù)據(jù)庫(kù)錯(cuò)誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}