商品信息查詢修改界面功能設(shè)計(jì)
1.1 任務(wù)內(nèi)容
在智慧社區(qū)商超管理系統(tǒng)中厕九,后臺(tái)管理人員查詢系統(tǒng)已有商品信息忧换,然后對(duì)某條信息進(jìn)行相應(yīng)操作(如:修改或者刪除)盔憨。查詢信息也是信息管理系統(tǒng)的一項(xiàng)基礎(chǔ)功能洗搂。
1.2 任務(wù)目標(biāo)
(1)進(jìn)一步認(rèn)識(shí)ADO.NET热康;
(2)了解C#實(shí)現(xiàn)數(shù)據(jù)庫(kù)數(shù)據(jù)查詢沛申、修改、刪除的過程姐军;
(3)了解C#數(shù)據(jù)庫(kù)編程中DataGridView控件的使用方法铁材。
2.1 效果圖
223.gif
上圖實(shí)現(xiàn)了對(duì)數(shù)據(jù)庫(kù)的修改與刪除操作。
2.2 支持這些功能的后臺(tái)數(shù)據(jù)庫(kù)表結(jié)構(gòu)
[ID] [varchar](50) NOT NULL,
[NAME] [varchar](20) NULL,
[PRICE] [float] NULL,
[SPEC] [varchar](20) NULL,
[REMARK] [varchar](100) NULL,
2.3 ADO.NET刪除數(shù)據(jù)庫(kù)的流程
(1) 導(dǎo)入命名空間;
(2) 定義數(shù)據(jù)庫(kù)連接字符串奕锌,創(chuàng)建Connection對(duì)象著觉;
(3)打開連接;
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
(4)連接數(shù)據(jù)庫(kù)惊暴;
sqlConn.Open();
(5)刪除語句構(gòu)造命令饼丘;
String sqlStr = "delete from GOODS where ID=@id";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
(6)SQL字符串參數(shù)賦值
cmd.Parameters.Add(new SqlParameter("@id", goodsId));
(7)將命令發(fā)送給數(shù)據(jù)庫(kù)
int res = cmd.ExecuteNonQuery();
(8)根據(jù)返回值判斷是否修改成功
if (res != 0)
{
MessageBox.Show("刪除成功");
}
else
{
MessageBox.Show("刪除失敗");
}
(9)關(guān)閉連接;
finally
{
sqlConn.Close();
}
2.4 畫面迭代流程
223.gif
2.5 DataGridView數(shù)據(jù)綁定流程
14023516-01197db63ee082e8.gif
2.6主要代碼
{
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ù)庫(kù)
sqlConn.Open();
// 構(gòu)造命令
String sqlStr = "update GOODS set NAME=@name, PRICE=@price, SPEC=@spec, REMARK=@remark 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));
// 將命令發(fā)送給數(shù)據(jù)庫(kù)
int res = cmd.ExecuteNonQuery();
// 根據(jù)返回值判斷是否修改成功
if (res != 0)
{
MessageBox.Show("商品信息修改成功");
this.Close();
}
else
{
MessageBox.Show("商品信息修改失敗");
}
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫(kù)錯(cuò)誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
//數(shù)據(jù)的查詢
private void bt_Query_Click(object sender, EventArgs e)
{
// 連接字符串肄鸽,注意與實(shí)際環(huán)境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(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);
// 將該查詢過程綁定到DataAdapter
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
// 將DataSet和DataAdapter綁定
DataSet ds = new DataSet();
// 自定義一個(gè)表(MyGoods)來標(biāo)識(shí)數(shù)據(jù)庫(kù)的GOODS表
adp.Fill(ds, "MyGoods");
// 指定DataGridView的數(shù)據(jù)源為DataSet的MyGoods表
this.dgv_Goods.DataSource = ds.Tables["MyGoods"];
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫(kù)錯(cuò)誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
// 數(shù)據(jù)修改,刪除
private void dgv_Goods_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// 點(diǎn)擊修改鏈接
if (e.RowIndex != -1 && e.ColumnIndex == 0)
{
// 獲取所要修改關(guān)聯(lián)對(duì)象的主鍵
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)
{
if (MessageBox.Show("確認(rèn)刪除油啤?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
// 獲取所要?jiǎng)h除關(guān)聯(lián)對(duì)象的主鍵
string goodsId = 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", goodsId));
// 將命令發(fā)送給數(shù)據(jù)庫(kù)
int res = cmd.ExecuteNonQuery();
// 根據(jù)返回值判斷是否修改成功
if (res != 0)
{
MessageBox.Show("刪除成功");
}
else
{
MessageBox.Show("刪除失敗");
}
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫(kù)錯(cuò)誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
}