Home > Database > Mysql Tutorial > body text

删除DataGridView选中行并更新数据库

WBOY
Release: 2016-06-07 15:58:46
Original
2168 people have browsed it

前面写过一篇文章是DataGridView控件显示数据的,DataGridView在与数据库打交道时会经常出现,也很实用。通过DataGridView对数据库进行更改和查询都比较方便。 这里我们需要用DataGridView数据,并通过选中行将数据从数据库中删除。 其原理是把选中记录的主

前面写过一篇文章是DataGridView控件显示数据的,DataGridView在与数据库打交道时会经常出现,也很实用。通过DataGridView对数据库进行更改和查询都比较方便。

这里我们需要用DataGridView数据,并通过选中行将数据从数据库中删除。

其原理是把选中记录的主键提取出来,然后传给实体,通过实体给D层传值实现对数据库的修改。

下面是各层代码,供大家参考。

接口层代码都是D层的父类方法,这里只给出D层代码:

<span style="background-color: rgb(255, 255, 255);"><span style="font-family:KaiTi_GB2312;font-size:18px;">    &#39;重写删除用户接口方法
    Public Function DelUser(user As Entity.LoginEntity) As Integer Implements IAddDel.DelUser
        Dim strSQL As String = "delete from User_info where userName=@username"
        Dim params() As SqlParameter = {New SqlParameter("@username", user.user_name)}
        Dim helper As New SqlHelper
        Dim int = helper.ExecuteNoQuery(strSQL, CommandType.Text, params)
        Return int
    End Function</span></span>
Copy after login

抽象工厂代码:
<span style="background-color: rgb(255, 255, 255);"><span style="font-family:KaiTi_GB2312;font-size:18px;">  Private Shared ReadOnly AssemblyName As String = "DAL" &#39;声明程序集名称
    Private Shared ReadOnly db As String = ConfigurationManager.AppSettings("DB") &#39;读取配置文件

    Public Function AddDel() As IAddDel
        Dim className As String = AssemblyName + "." + db + "AddDelDAL"
        Dim iadddel As IAddDel
        iadddel = CType(Assembly.Load(AssemblyName).CreateInstance(className), IAddDel) &#39;反射
        Return iadddel
    End Function
</span></span>
Copy after login
B层代码:
<span style="background-color: rgb(255, 255, 255);"><span style="font-family:KaiTi_GB2312;font-size:18px;">    &#39;判断是否删除成功
    Public Function IsDelUser(ByVal user As Entity.LoginEntity) As Boolean
        Dim int = iadddel.DelUser(user)
        If int = 1 Then
            Return True
        Else
            Return False
        End If
    End Function</span></span>
Copy after login
U层代码
<span style="background-color: rgb(255, 255, 255);"><span style="font-family:KaiTi_GB2312;font-size:18px;"> Dim k As Integer = gvwUser.SelectedRows.Count
        Dim thisUser As New Entity.LoginEntity
        Dim ub As New BLL.AddDelBLL
        &#39;判断是否有选择记录
        If k > 0 Then
            If MessageBox.Show("删除用户后将无法恢复!是否继续删除?", "提示", MessageBoxButtons.YesNo, _
                               MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                &#39;从下往上删,避免沙漏效应  
                For i As Integer = k To 1 Step -1
                    &#39;获取用户名
                    thisUser.user_name = gvwUser.SelectedRows(i - 1).Cells("userName").Value.ToString
                    &#39;判断选中用户是否为登录用户
                    If thisUser.user_name = UserName Then
                        MsgBox("当前用户不能被删除,请重新选择!", vbOKOnly + vbExclamation, "系统提示")
                        Exit Sub
                    Else
                        If ub.IsDelUser(thisUser) = True Then
                            MsgBox("删除成功!", vbOKOnly + vbInformation, "系统提示")
                        Else
                            MessageBox.Show("删除失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        End If
                        &#39;将从数据库中删除的信息从Datagridview1中删除  
                        gvwUser.Rows.RemoveAt(gvwUser.SelectedRows(i - 1).Index)
                    End If
                Next
            End If
        Else
            MsgBox("请选中要删除的行")
            Exit Sub
        End If
    End Sub</span></span>
Copy after login

效果如下:

删除前: 删除后:

\\

\

我们通过DataGridView对数据进行操作更加直观,但是数据无价,操作数据库时一定要谨慎!以免给我们带来不必要的麻烦。必要时删除前要给与提示,是否确定删除,或者提高操作权限,方便操作的前提是保证数据的安全性!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!