2.8商品信息查詢修改界面功能設計
一骑晶、制作效果
二、描述界面主要功能和支持這些功能的后臺數(shù)據(jù)庫表結構
(一)主要功能
(1)商品查詢
(2)商品修改
(3)商品刪除
(二)后臺數(shù)據(jù)表結構
三、刪除數(shù)據(jù)庫ADO.NETL流程
四安寺、功能如何迭代(無供應商-----有供應商)
(1)在數(shù)據(jù)庫中創(chuàng)建關于供應商的數(shù)據(jù)表并連接數(shù)據(jù)庫
(2)在程序中構建查詢語句
(3)將該查詢過程綁定到DataAdapter
(4)將DataSet和DataAdapter綁定
(5)自定義一個表(MySupplier)來標識數(shù)據(jù)庫的SUPPLIER表
(6)指定ComboBox的數(shù)據(jù)源為DataSet的MySupplier表
詳見如下代碼
// 連接數(shù)據(jù)庫
sqlConn.Open();
// 構造查詢命令
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();
// 自定義一個表(MySupplier)來標識數(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)容凡壤,這里顯示供應商名稱
this.cbb_Supplier.ValueMember = "CODE"; // ComboBox另外還攜帶一個隱藏的值叫ValueMember,指定為供應商代碼
this.cbb_Supplier.SelectedIndex = 0;
五臭杰、DataGridView數(shù)據(jù)綁定流程
(1)連接數(shù)據(jù)庫創(chuàng)建相應的數(shù)據(jù)表
(2)創(chuàng)建DataGridView控件
(2) 將要綁定的數(shù)據(jù)列表賦值給控件的DataPropertyName
六粤咪、重要代碼
創(chuàng)建查詢
// 查詢數(shù)據(jù)
private void bt_Query_Click(object sender, EventArgs e)
{
// 連接字符串,注意與實際環(huán)境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫
sqlConn.Open();
// 構造命令
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();
// 自定義一個表(MyGoods)來標識數(shù)據(jù)庫的GOODS表
adp.Fill(ds, "MyGoods");
// 指定DataGridView的數(shù)據(jù)源為DataSet的MyGoods表
this.dgv_Goods.DataSource = ds.Tables["MyGoods"];
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫錯誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
創(chuàng)建刪除
// 數(shù)據(jù)修改渴杆,刪除
private void dgv_Goods_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// 點擊修改鏈接
if (e.RowIndex != -1 && e.ColumnIndex == 0)
{
// 獲取所要修改關聯(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)
{
if (MessageBox.Show("確認刪除寥枝?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
// 獲取所要刪除關聯(lián)對象的主鍵
string goodsId = this.dgv_Goods["Id", e.RowIndex].Value.ToString();
// 連接字符串,注意與實際環(huán)境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫
sqlConn.Open();
// 構造命令
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("刪除失敗");
}
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫錯誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
創(chuàng)建修改
private void bt_Ok_Click(object sender, EventArgs e)
{
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();
// 連接字符串磁奖,注意與實際環(huán)境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫
sqlConn.Open();
// 構造命令
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ù)庫
int res = cmd.ExecuteNonQuery();
// 根據(jù)返回值判斷是否修改成功
if (res != 0)
{
MessageBox.Show("商品信息修改成功");
this.Close();
}
else
{
MessageBox.Show("商品信息修改失敗");
}
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫錯誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}