任務(wù)2.9 商品信息查詢修改界面功能設(shè)計(jì) (曾浩 2016270386)

1.效果圖

圖1.商品信息查詢界面

2.主要功能及數(shù)據(jù)表結(jié)構(gòu)

2.1-主要功能

后臺(tái)管理人員查詢系統(tǒng)已有商品信息,然后對(duì)某條信息進(jìn)行相應(yīng)操作(如:修改或者刪除)。查詢信息也是信息管理系統(tǒng)的一項(xiàng)基礎(chǔ)功能。

2.2-所需的后臺(tái)數(shù)據(jù)庫(kù)表結(jié)構(gòu)

表名:GOODS

列名 數(shù)據(jù)類型 允許null值
ID varchar(50) ×
NAME varchar(50)
SUPPLIER int
SPEC varchar(20)
REMARK varchar(100)
圖2.GOODS表

表名:SUPPLIER

列名 數(shù)據(jù)類型 允許null值
CODE int ×
NAME varchar(100)
LOCATION varchar(100)
CONTRACTS varchar(50)
PHONE varchar(15)
圖3.SUPPLIER表

3.ADO.NET刪除數(shù)據(jù)庫(kù)的流程

圖4.ADO.NET刪除
具體步驟:
  1. 導(dǎo)入命名空間;
  2. 定義數(shù)據(jù)庫(kù)連接字符串,運(yùn)用Connection對(duì)象建立與數(shù)據(jù)庫(kù)連接;
  3. 打開(kāi)連接;
  4. 利用Command對(duì)象的ExecuteNoQuery()方法執(zhí)行Delete語(yǔ)句;
  5. 通過(guò)ExecuteNoQuery()方法返回值判斷是否修改成功,并在界面上提示;
  6. 關(guān)閉連接。

4.迭代過(guò)程(無(wú)供應(yīng)商>>有供應(yīng)商)

4.1-添加comboBox控件

圖5.無(wú)供應(yīng)商
圖6.有供應(yīng)商

4.2-綁定ComboBox,初始化下拉列表

1.連接數(shù)據(jù)庫(kù);

String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
sqlConn.Open();

2.構(gòu)造查詢命令;

String sqlStr = "select * from SUPPLIER order by CODE";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);

3.將該查詢過(guò)程綁定到DataAdapter;

SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;

4.將DataSet和DataAdapter綁定;

DataSet ds = new DataSet();

5.自定義一個(gè)表(MySupplier)來(lái)標(biāo)識(shí)數(shù)據(jù)庫(kù)的SUPPLIER表;

adp.Fill(ds, "MySupplier");

6.指定ComboBox的數(shù)據(jù)源為DataSet的MySupplier表

this.cbb_Supplier.DataSource = ds.Tables["MySupplier"];
this.cbb_Supplier.DisplayMember = "NAME"; // ComboBox下拉列表顯示的內(nèi)容,這里顯示供應(yīng)商名稱
this.cbb_Supplier.ValueMember = "CODE";   // ComboBox另外還攜帶一個(gè)隱藏的值叫ValueMember,指定為供應(yīng)商代碼
this.cbb_Supplier.SelectedIndex = 0;

5.DataGridView數(shù)據(jù)綁定流程

  1. 構(gòu)造命令;
String sqlStr = "select * from GOODS where 1=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);
  1. 將該查詢過(guò)程綁定到DataAdapter;
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
  1. 將DataSet和DataAdapter綁定;
DataSet ds = new DataSet();
  1. 自定義一個(gè)表(MyGoods)來(lái)標(biāo)識(shí)數(shù)據(jù)庫(kù)的GOODS表;
adp.Fill(ds, "MyGoods");
  1. 指定DataGridView的數(shù)據(jù)源為DataSet的MyGoods表。
this.dgv_Goods.DataSource = ds.Tables["MyGoods"];

6.重要代碼片段

6.1-查詢商品信息

編寫商品信息查詢列表代碼:
1) 代碼編寫位置
“查詢”按鈕點(diǎn)擊后;
2) 代碼編寫
利用ADO .NET技術(shù)實(shí)現(xiàn)數(shù)據(jù)類別查詢。

具體步驟:
  1. 導(dǎo)入命名空間;
  2. 定義數(shù)據(jù)庫(kù)連接字符串,運(yùn)用Connection對(duì)象建立與數(shù)據(jù)庫(kù)連接;
  3. 打開(kāi)連接;
  4. 利用DataAdapter對(duì)象,建立與數(shù)據(jù)庫(kù)的連接橋;
  5. 通過(guò)DataAdapter橋,將查詢結(jié)果存儲(chǔ)到DataSet對(duì)象中;
  6. 利用DataGridView控件將DataSet中的查詢結(jié)果顯示出來(lái)
  7. 關(guān)閉連接。
                // 連接數(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"];

6.2-修改商品信息

(1)窗口加載后,顯示商品信息
運(yùn)用ADO.NET實(shí)現(xiàn)數(shù)據(jù)庫(kù)查詢;
(2)點(diǎn)擊修改后,修改商品信息
運(yùn)用ADO.NET實(shí)現(xiàn)數(shù)據(jù)庫(kù)修改。

具體步驟:
  1. 導(dǎo)入命名空間;
  2. 定義數(shù)據(jù)庫(kù)連接字符串,運(yùn)用Connection對(duì)象建立與數(shù)據(jù)庫(kù)連接;
  3. 打開(kāi)連接;
  4. 利用Command對(duì)象的ExecuteNoQuery()方法執(zhí)行Update語(yǔ)句;
  5. 通過(guò)ExecuteNoQuery()方法返回值判斷是否修改成功,并在界面上提示;
  6. 關(guān)閉連接。
                // 構(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("商品信息修改失敗");
                }

6.3-刪除商品信息

點(diǎn)擊刪除后,提示確認(rèn),確認(rèn)后刪除
運(yùn)用ADO.NET實(shí)現(xiàn)數(shù)據(jù)庫(kù)刪除。

具體步驟:
  1. 導(dǎo)入命名空間;
  2. 定義數(shù)據(jù)庫(kù)連接字符串,運(yùn)用Connection對(duì)象建立與數(shù)據(jù)庫(kù)連接;
  3. 打開(kāi)連接;
  4. 利用Command對(duì)象的ExecuteNoQuery()方法執(zhí)行Delete語(yǔ)句;
  5. 通過(guò)ExecuteNoQuery()方法返回值判斷是否修改成功,并在界面上提示;
  6. 關(guān)閉連接。
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();
                    }
                }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容