开发实践教程1:试卷生成系统6.17 用户管理(FormUserList)

Posted VB.Net

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开发实践教程1:试卷生成系统6.17 用户管理(FormUserList)相关的知识,希望对你有一定的参考价值。

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

仅管理员可用

操作员对普通用户的启用、禁用或停用、设置默认密码、设置用户备注信息。

窗体设计如下:

图1-27

具体代码如下:

Imports System.Data.SqlClient

Public Class FormUserList

    Dim connection As SqlConnection
    Dim ds As DataSet
    Dim adapter As SqlDataAdapter
    Dim dt As DataTable

    Const pagesize As Integer = 50
    Dim maxPage As Integer
    Dim currentPage As Integer

    Dim F_Main As FormMain

    Dim sqlMySelect As String
    Dim sqlMyFrom As String
    Dim sqlMyOrder As String
    Dim sqlMyWhere As String

    Dim sqlQuerySelect As String
    Dim sqlQueryFrom As String
    Dim sqlQueryOrder As String
    Dim sqlQueryWhere As String

    Dim sqlFlag As String

    Public customWhere As String

    Public currentRemark As String

    Dim dgvCanResize As Boolean = False


    Private Sub FormUserList_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        connection = New SqlConnection(databaseConnString)
        connection.Open()

        F_Main = CType(Me.MdiParent, FormMain)

        sqlMySelect = "SELECT 编号,账号,申请时间,真实姓名,身份证号码,状态,审核时间,备注信息 "
        sqlMyFrom = "FROM 用户表 "
        sqlMyWhere = "where 账号<>'admin' "
        sqlMyOrder = "order by 申请时间 desc"

        sqlQuerySelect = "SELECT 编号,账号,申请时间,真实姓名,身份证号码,状态,审核时间,备注信息 "
        sqlQueryFrom = "FROM 用户表 "
        sqlQueryWhere = ""
        sqlQueryOrder = "order by 申请时间 desc"

        sqlFlag = "my"

        Call showGrid(sqlFlag)
    End Sub


    Public Sub showGrid(ByVal tableFlag As String)
        ds = New DataSet(tableFlag)
        Dim command As New SqlCommand()

        Dim sqlSelect As String = ""
        Dim sqlFrom As String = ""
        Dim sqlWhere As String = ""
        Dim sqlOrder As String = ""

        Select Case tableFlag
            Case "my"
                sqlSelect = sqlMySelect
                sqlFrom = sqlMyFrom
                sqlWhere = sqlMyWhere
                sqlOrder = sqlMyOrder

            Case "custom"
                sqlSelect = sqlQuerySelect
                sqlFrom = sqlQueryFrom
                sqlWhere = sqlQueryWhere
                sqlOrder = sqlQueryOrder

        End Select
        command.CommandText = "select count(*) " & sqlFrom & sqlWhere
        command.Connection = connection

        Dim count As Integer = command.ExecuteScalar


        F_Main.tsslInfo.Text = "数据总数:" & count & " 条"

        If count < pagesize Then
            tsbFirstPage.Enabled = False
            tsbPrevPage.Enabled = False
            tsbNextPage.Enabled = False
            tsbLastPage.Enabled = False
        Else
            tsbFirstPage.Enabled = True
            tsbPrevPage.Enabled = True
            tsbNextPage.Enabled = True
            tsbLastPage.Enabled = True
        End If

        command.CommandText = sqlSelect &
                        sqlFrom &
                        sqlWhere &
                        sqlOrder

        adapter = New SqlDataAdapter(command)

        maxPage = Math.Ceiling(count / pagesize)
        currentPage = 0
        adapter.Fill(ds, currentPage * pagesize, pagesize, tableFlag)
        dgv.DataSource = Nothing
        dgv.DataSource = ds.Tables(tableFlag)
        tslRecord.Text = (currentPage + 1).ToString & "/" & maxPage.ToString

        dgv.Columns(0).Visible = False

        dgvCanResize = True

    End Sub

    Private Sub tsbAllUser_Click(sender As Object, e As EventArgs) Handles tsbAllUser.Click
        sqlFlag = "my"
        Call showGrid(sqlFlag)

    End Sub


    Private Sub tsbFirstPage_Click(sender As Object, e As EventArgs) Handles tsbFirstPage.Click
        currentPage = 0
        ds.Tables(sqlFlag).Clear()
        adapter.Fill(ds, currentPage * pagesize, pagesize, sqlFlag)
        dgv.DataSource = ds.Tables(sqlFlag)

        tslRecord.Text = (1).ToString & "/" & maxPage.ToString
    End Sub
    Private Sub tsbPrevPage_Click(sender As Object, e As EventArgs) Handles tsbPrevPage.Click
        If currentPage = 0 Then
            MessageBox.Show("已经是第一页了")
            Exit Sub
        End If
        currentPage -= 1
        ds.Tables(sqlFlag).Clear()
        adapter.Fill(ds, currentPage * pagesize, pagesize, sqlFlag)
        dgv.DataSource = ds.Tables(sqlFlag)
        tslRecord.Text = (currentPage + 1).ToString & "/" & maxPage.ToString
    End Sub
    Private Sub tsbNextPage_Click(sender As Object, e As EventArgs) Handles tsbNextPage.Click
        If currentPage = maxPage - 1 Then
            MessageBox.Show("已经是最后一页了")
            Exit Sub
        End If
        currentPage += 1
        ds.Tables(sqlFlag).Clear()
        Dim recordcount As Integer = adapter.Fill(ds, currentPage * pagesize, pagesize, sqlFlag)
        dgv.DataSource = ds.Tables(sqlFlag)
        tslRecord.Text = (currentPage + 1).ToString & "/" & maxPage.ToString
    End Sub
    Private Sub tsbLastPage_Click(sender As Object, e As EventArgs) Handles tsbLastPage.Click
        currentPage = maxPage - 1
        ds.Tables(sqlFlag).Clear()
        adapter.Fill(ds, currentPage * pagesize, pagesize, sqlFlag)
        dgv.DataSource = ds.Tables(sqlFlag)
        tslRecord.Text = (currentPage + 1).ToString & "/" & maxPage.ToString
    End Sub

    Private Sub tsbQuery_Click(sender As Object, e As EventArgs) Handles tsbQuery.Click
        Dim fUserQuery As New FormUserQuery
        fUserQuery.ShowDialog(Me)

        If customWhere = "" Then
            Exit Sub
        End If

        sqlQueryWhere = "where " & customWhere & " "
        sqlFlag = "custom"

        Call showGrid(sqlFlag)
    End Sub

    Private Sub tsbUse_Click(sender As Object, e As EventArgs) Handles tsbUse.Click
        If dgv.SelectedRows.Count = 0 Then
            MessageBox.Show("请先选择需要启用的用户")
            Exit Sub
        End If

        Dim UserId As Integer
        UserId = CType(dgv.SelectedRows(0).Cells(0).Value, Integer)

        Dim updateTime As String
        updateTime = Now.ToString("yyyy-MM-dd HH:mm:ss")

        Dim command As New SqlCommand()
        command.Connection = connection
        command.CommandText = "update 用户表 set 状态='启用',审核时间='" & updateTime & "' where 编号=" & UserId
        Try
            command.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show("更新操作时发生错误。错误原因:" & ex.Message)
            Exit Sub
        End Try
        dgv.SelectedRows(0).Cells(5).Value = "启用"
        dgv.SelectedRows(0).Cells(6).Value = updateTime

    End Sub

    Private Sub tsbNoUse_Click(sender As Object, e As EventArgs) Handles tsbNoUse.Click
        If dgv.SelectedRows.Count = 0 Then
            MessageBox.Show("请先选择需要禁用或停用的用户")
            Exit Sub
        End If

        Dim UserId As Integer
        UserId = CType(dgv.SelectedRows(0).Cells(0).Value, Integer)

        Dim updateTime As String
        updateTime = Now.ToString("yyyy-MM-dd HH:mm:ss")

        Dim updateState As String
        Select Case dgv.SelectedRows(0).Cells(5).Value
            Case "申请"
                updateState = "禁用"
            Case "启用"
                updateState = "停用"
            Case Else
                updateState = "停用"
        End Select


        Dim command As New SqlCommand()
        command.Connection = connection
        command.CommandText = "update 用户表 set 状态='" & updateState & "',审核时间='" & updateTime & "' where 编号=" & UserId
        Try
            command.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show("更新操作时发生错误。错误原因:" & ex.Message)
            Exit Sub
        End Try
        dgv.SelectedRows(0).Cells(5).Value = updateState
        dgv.SelectedRows(0).Cells(6).Value = updateTime

    End Sub

    Private Sub tsbResetPass_Click(sender As Object, e As EventArgs) Handles tsbResetPass.Click
        If dgv.SelectedRows.Count = 0 Then
            MessageBox.Show("请先选择需要恢复初始密码的用户")
            Exit Sub
        End If

        Dim UserId As Integer
        UserId = CType(dgv.SelectedRows(0).Cells(0).Value, Integer)

        Dim pass As String = clsMd5.toMD5("123456")

        Dim command As New SqlCommand()
        command.Connection = connection
        command.CommandText = "update 用户表 set 密码='" & pass & "' where 编号=" & UserId
        Try
            command.ExecuteNonQuery()
            MessageBox.Show("已经恢复初始密码。")
        Catch ex As Exception
            MessageBox.Show("更新操作时发生错误。错误原因:" & ex.Message)
            Exit Sub
        End Try


    End Sub

    Private Sub dgv_SelectionChanged(sender As Object, e As EventArgs) Handles dgv.SelectionChanged
        If dgv.SelectedRows.Count < 1 Then Exit Sub

        Dim SelectedRow As DataGridViewRow
        SelectedRow = dgv.SelectedRows(0)

        Select Case SelectedRow.Cells(5).Value.ToString
            Case "申请"
                tsbUse.Enabled = True
                tsbNoUse.Enabled = True
            Case "启用"
                tsbUse.Enabled = False
                tsbNoUse.Enabled = True
            Case "禁用"
                tsbUse.Enabled = True
                tsbNoUse.Enabled = False
            Case "停用"
                tsbUse.Enabled = True
                tsbNoUse.Enabled = False
        End Select

    End Sub

    Private Sub tsbHome_Click(sender As Object, e As EventArgs) Handles tsbHome.Click
        F_Main.tsslInfo.Text = ""
        Me.Close()
    End Sub

    Private Sub tsbInfo_Click(sender As Object, e As EventArgs) Handles tsbInfo.Click
        If dgv.SelectedRows.Count = 0 Then
            MessageBox.Show("请先选择需要恢复初始密码的用户")
            Exit Sub
        End If

        Dim UserId As Integer
        UserId = CType(dgv.SelectedRows(0).Cells(0).Value, Integer)

        Dim fUserMemo As New FormUserMemo(UserId, dgv.SelectedRows(0).Cells(1).Value, dgv.SelectedRows(0).Cells(3).Value, dgv.SelectedRows(0).Cells(4).Value, dgv.SelectedRows(0).Cells(7).Value)
        fUserMemo.ShowDialog(Me)

        If currentRemark = "" Then
            Exit Sub
        End If

        dgv.SelectedRows(0).Cells(7).Value = currentRemark

    End Sub
End Class

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供的参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

以上是关于开发实践教程1:试卷生成系统6.17 用户管理(FormUserList)的主要内容,如果未能解决你的问题,请参考以下文章

开发实践教程1:试卷生成系统6.3 主界面(FormMain)

开发实践教程1:试卷生成系统 1 前言和目录

开发实践教程1:试卷生成系统4 类

开发实践教程1:试卷生成系统6.4 考题管理(FormExam)

开发实践教程1:试卷生成系统6 窗体设计总览

开发实践教程1:试卷生成系统6.16 试卷类型管理(FormPaperType)