C# WinForm 实现增删改查等功能(Access版) 系列之四

本文介绍使用C# WinForm结合Access数据库实现删除记录的功能。通过DataGridView选择要删除的记录,确认后执行删除操作并更新数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C# WinForm 实现增删改查等功能(Access) 系列之四

删除一条记录模块设计与实现

/// <summary>

        /// 删除数据库中的一记录

        /// </summary>

        /// <returns></returns>

        public bool deletDataGridViewOneLine(object sender, EventArgs e)

        {

            bool result = false;

            Int32 selectedRowCount = dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected);   //单选模式

            int selectedRow = dataGridView1.SelectedRows[0].Index;    //获得选中的某行

            string MName =  dataGridView1.Rows[selectedRow].Cells[0].Value.ToString().Trim();

           // MessageBox.Show(MName.ToString());

            DialogResult dr = MessageBox.Show("确定要删除这条记录吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (dr == DialogResult.Yes)

            {

                if (MName != null && MName != "")

                {

                    OleDbConnection conn = GetConnection();

                    string sqlText = "delete from MResume where 姓名=@MName";

                    OleDbCommand cmd = new OleDbCommand(sqlText, conn);

                    cmd.Parameters.AddWithValue("@MName", MName);

                    try

                    {

                        conn.Open();

                        int i = cmd.ExecuteNonQuery();

                        result = true;

                    }

                    catch (Exception ex)

                    {

                        MessageBox.Show("发生异常:" + ex.ToString(), "提示");

                        result = false;

                    }

                    finally

                    {

                        conn.Close();

                    }

                }

            }

            return result;

        }

//右键选中行

//本方法来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/yshjshxxn/archive/2009/06/26/4300927.aspx

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)

        {

              //判断如果点击的是鼠标右键  

            if (e.Button == MouseButtons.Right)  

            {  

                //判断鼠标点击在数据行上  

                if (e.RowIndex >= 0)  

                {

                    dataGridView1.ClearSelection();

                    dataGridView1.Rows[e.RowIndex].Selected = true;

                    dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];  

                }  

            }

        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值