1.
2.主要功能是實(shí)現(xiàn)商品信息的修改和刪除。
3.
(1)導(dǎo)入命名空間
(2)定義數(shù)據(jù)庫(kù)連接字符串,創(chuàng)建Connection對(duì)象
(3)打開(kāi)連接
(4)利用Command對(duì)象的ExecuteNonQuery()方法執(zhí)行Delete語(yǔ)句
(5)通過(guò)ExecuteNonQuery()方法返回值判斷是否修改成功,并在界面上提示
(6)關(guān)閉連接
4.
構(gòu)造查詢命令
將該查詢過(guò)程綁定到DataAdapter
將DataSet和DataAdapter綁定
自定義一個(gè)表(MySupplier)來(lái)標(biāo)識(shí)數(shù)據(jù)庫(kù)的SUPPLIER表
指定ComboBox的數(shù)據(jù)源為DataSet的MySupplier表
// 指定ComboBox的數(shù)據(jù)源為DataSet的MySupplier表
this.cbb_Supplier.DataSource = ds.Tables["MySupplier"];
this.cbb_Supplier.DisplayMember = "NAME"; // ComboBox下拉列表顯示的內(nèi)容,這里顯示供應(yīng)商名稱(chēng)
this.cbb_Supplier.ValueMember = "CODE"; // ComboBox另外還攜帶一個(gè)隱藏的值叫ValueMember,指定為供應(yīng)商代碼
this.cbb_Supplier.SelectedIndex = 0;
5.
(1)導(dǎo)入命名空間
(2)定義數(shù)據(jù)庫(kù)連接字符串,創(chuàng)建Connection對(duì)象
(3)打開(kāi)連接
(4)構(gòu)造select查詢語(yǔ)句
(5)將該查詢過(guò)程綁定到DataAdapter
將DataSet和DataAdapter綁定
自定義一個(gè)表(MyGoods)來(lái)標(biāo)識(shí)數(shù)據(jù)庫(kù)的GOODS表
指定DataGridView的數(shù)據(jù)源為DataSet的MyGoods表
(6)關(guān)閉連接
6.
這段代碼的功能是實(shí)現(xiàn)商品的查詢。
與數(shù)據(jù)庫(kù)連接 、構(gòu)造查詢語(yǔ)句 、把相應(yīng)的條件添加進(jìn)去 、將該查詢過(guò)程綁定到DataAdapter 、 將DataSet和DataAdapter綁定 、自定義一個(gè)表(MyGoods)來(lái)標(biāo)識(shí)數(shù)據(jù)庫(kù)的GOODS表 、指定DataGridView的數(shù)據(jù)源為DataSet的MyGoods表、關(guān)閉連接。
// 查詢數(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);
// 將該查詢過(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"];
}
catch (Exception exp)
{
MessageBox.Show("訪問(wèn)數(shù)據(jù)庫(kù)錯(cuò)誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
這段代碼的功能是實(shí)現(xiàn)商品信息的修改和刪除。
首先要獲取刪除關(guān)聯(lián)對(duì)象的主鍵、然后與數(shù)據(jù)庫(kù)連接、構(gòu)造命令、給參數(shù)賦值、將命令發(fā)送到數(shù)據(jù)庫(kù)、判斷數(shù)據(jù)庫(kù)的返回值、關(guān)閉連接。
// 數(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("訪問(wèn)數(shù)據(jù)庫(kù)錯(cuò)誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}