具體要求:
具體要求.png
具體要求.png
實(shí)現(xiàn)查詢功能
登錄服務(wù)器.gif
登錄服務(wù)器
控件.PNG
形成查詢界面的重要控件以及下圖的流程
查詢流程.PNG
代碼片段.PNG
代碼片段.PNG
一些重要的程序代碼
修改商品信息
修改商品信息.gif
再次查詢可以發(fā)現(xiàn)商品海飛絲的價(jià)格已經(jīng)發(fā)現(xiàn)改變
// 更新數(shù)據(jù)庫(kù)
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, 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("商品信息修改失敗");
}
}
catch (Exception exp)
{
MessageBox.Show("訪問(wèn)數(shù)據(jù)庫(kù)錯(cuò)誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
修改商品信息的流程和重要代碼
刪除商品信息
3.gif
商品刪除的過(guò)程
// 數(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 objectId = this.dgv_Goods["Id", e.RowIndex].Value.ToString();
ModifyForm modifyForm = new ModifyForm(objectId);
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 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();
}
捕獲8.PNG
刪除商品信息重要代碼和流程