2.9 商品信息查詢修改界面功能設(shè)計
修改流程
修改.gif
1.畫面主要功能
修改商品的各種信息,但是商品條碼不能修改笙各。
2.臺數(shù)據(jù)庫表結(jié)構(gòu)
數(shù)據(jù)表結(jié)構(gòu).png
3.ADO.NET刪除數(shù)據(jù)庫的流程
具體步驟:
- 導(dǎo)入命名空間;
- 定義數(shù)據(jù)庫連接字符串邻耕,運用Connection對象建立與數(shù)據(jù)庫連接弦叶;
- 打開連接芋浮;
- 利用Command對象的ExecuteNoQuery()方法執(zhí)行Delete語句科盛;
- 通過ExecuteNoQuery()方法返回值判斷是否修改成功帽衙,并在界面上提示;
-
關(guān)閉連接
圖片1.png
4.畫面功能迭代
將該查詢過程綁定到DataAdapter
將DataSet和DataAdapter綁定
自定義一個表(MySupplier)來標(biāo)識數(shù)據(jù)庫的SUPPLIER表
指定ComboBox的數(shù)據(jù)源為DataSet的MySupplier表
5.DataGridView數(shù)據(jù)綁定流程
- 定義一個類贞绵,存放dataGridView的數(shù)據(jù)厉萝;
- 在Form2中定義dataGridView數(shù)據(jù)列表,并將數(shù)據(jù)存放進去榨崩;
3.From2中定義返回綁定數(shù)據(jù)的函數(shù)谴垫,返回綁定數(shù)據(jù); - Form1中初始化Column母蛛;
- 在Form1中出發(fā)Form2翩剪,在結(jié)束后將From2返回的綁定數(shù)據(jù)傳到Form1中的dataGridView中,完成數(shù)據(jù)傳遞彩郊。
6.重要代碼
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫
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();
// 自定義一個表(MyGoods)來標(biāo)識數(shù)據(jù)庫的GOODS表
adp.Fill(ds, "MyGoods");
// 指定DataGridView的數(shù)據(jù)源為DataSet的MyGoods表
this.dgv_Goods.DataSource = ds.Tables["MyGoods"];
// DataGridView數(shù)據(jù)綁定
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫錯誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
private void dgv_Goods_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1 && e.ColumnIndex == 0)
{
string objectId = this.dgv_Goods["Id", e.RowIndex].Value.ToString();
MessageBox.Show(objectId);
}
else if (e.RowIndex != -1 && e.ColumnIndex == 1)
{
string objectId = this.dgv_Goods["Id", e.RowIndex].Value.ToString();
MessageBox.Show(objectId);
}
}
以上代碼實現(xiàn)商品信息列表查詢功能代碼