2018-12-03(第四組)

《2.8 商品信息查詢修改界面功能設計》 制作人:李健

1.

2.8.gif

2.主要功能:實現(xiàn)商品信息的修改和刪除

3.

(1)導入命名空間

(2)定義數(shù)據(jù)庫連接字符串,創(chuàng)建Connection對象

(3)打開連接

(4)利用Command對象的ExecuteNonQuery()方法執(zhí)行Delete語句

(5)通過ExecuteNonQuery()方法返回值判斷是否修改成功,并在界面上提示

(6)關閉連接

4.

構(gòu)造查詢命令
將該查詢過程綁定到DataAdapter
將DataSet和DataAdapter綁定
自定義一個表(MySupplier)來標識數(shù)據(jù)庫的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)容,這里顯示供應商名稱

this.cbb_Supplier.ValueMember = "CODE"; // ComboBox另外還攜帶一個隱藏的值叫ValueMember,指定為供應商代碼

this.cbb_Supplier.SelectedIndex = 0;

5.

(1)導入命名空間

(2)定義數(shù)據(jù)庫連接字符串,創(chuàng)建Connection對象

(3)打開連接

(4)構(gòu)造select查詢語句

(5)將該查詢過程綁定到DataAdapter
將DataSet和DataAdapter綁定
自定義一個表(MyGoods)來標識數(shù)據(jù)庫的GOODS表
指定DataGridView的數(shù)據(jù)源為DataSet的MyGoods表

(6)關閉連接

6.

這段代碼的功能是實現(xiàn)商品的查詢。
與數(shù)據(jù)庫連接 、構(gòu)造查詢語句 、把相應的條件添加進去 、將該查詢過程綁定到DataAdapter 、 將DataSet和DataAdapter綁定 、自定義一個表(MyGoods)來標識數(shù)據(jù)庫的GOODS表 、指定DataGridView的數(shù)據(jù)源為DataSet的MyGoods表、關閉連接。

        // 查詢數(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();

                // 構(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)來標識數(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();
            }
        }

這段代碼的功能是實現(xiàn)商品信息的修改和刪除。
首先要獲取刪除關聯(lián)對象的主鍵、然后與數(shù)據(jù)庫連接

        // 數(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();

                        // 構(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ù)庫
                        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();
                    }
                }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

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