⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datausers.vb

📁 《ASP.NET C#程序设计案例教程》
💻 VB
📖 第 1 页 / 共 2 页
字号:
    Public Article As Integer
    Public ReArticle As Integer

    '这是构造函数,在其中给每一个属性赋值
    Public Sub New(ByVal strUserId As String)
        _strConn = ConfigurationSettings.AppSettings("strConn")  '将数据库连接字符串赋值给私有变量_strConn
        Dim conn As New OleDbConnection(_strConn)       '建立Connection对象
        Dim strSql As String = "Select * From users Where UserId='" & strUserId & "'" '建立SQL语句
        Dim cmd As New OleDbCommand(strSql, conn)       '建立Command对象
        conn.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader()      '建立DataReader对象
        '下面读取数据,并给属性赋值
        dr.Read()
        UserId = dr.Item("UserId")
        RoleId = dr.Item("RoleId")
        UserPWD = dr.Item("UserPWD")
        UserName = dr.Item("UserName")
        Sex = dr.Item("Sex")
        Tel = dr.Item("Tel").ToString()        '因为可能是空值,所以加一个ToString
        Email = dr.Item("Email")
        QQ = dr.Item("QQ").ToString()
        Intro = dr.Item("Intro").ToString()
        SubmitDate = dr.Item("SubmitDate")
        Article = dr.Item("Article")
        Rearticle = dr.Item("ReArticle")
        conn.Close()
    End Sub

    '该过程用来将用户的发表文章数目加1
    Public Sub AddArticle()
        Dim conn As New OleDbConnection(_strConn)       '使用了私有变量的值
        '建立Command对象,注意这里使用了含有参数的SQL语句
        Dim strSql = "Update Users Set Article=Article+1 Where UserId='" & UserId & "'"
        Dim cmd As New OleDbCommand(strSql, conn)
        '下面执行操作
        conn.open()
        cmd.ExecuteNonQuery()
        conn.close()
    End Sub

    '该过程用来将用户的回复文章数目加1
    Public Sub AddReArticle()
        Dim conn As New OleDbConnection(_strConn)     '使用了私有变量的值
        '建立Command对象,注意这里使用了含有参数的SQL语句
        Dim strSql = "Update Users Set ReArticle=ReArticle+1 Where UserId='" & UserId & "'"
        Dim cmd As New OleDbCommand(strSql, conn)
        '下面执行操作
        conn.open()
        cmd.ExecuteNonQuery()
        conn.close()
    End Sub

    '该函数用来判断用户是否在线
    Public Function IsOnline() As Boolean
        Dim conn As New OleDbConnection(_strConn)       '使用了私有变量的值
        '建立Command对象,注意这里使用了含有参数的SQL语句
        Dim strSql = "Select Count(*) As total From OnlineUsers Where IsOnline=True And UserId='" & UserId & "'"
        Dim cmd As New OleDbCommand(strSql, conn)
        '下面执行操作
        conn.Open()
        Dim intTotal As Integer = cmd.ExecuteScalar()
        conn.Close()
        If intTotal > 0 Then
            Return (True)
        Else
            Return (False)
        End If
    End Function

    '该函数用来判断某用户是否是某栏目的版主
    Public Function IsForumManager(ByVal intForumId As Integer) As Boolean
        Dim conn As New OleDbConnection(_strConn)       '使用了私有变量的值
        '建立Command对象,注意这里使用了含有参数的SQL语句
        Dim strSql = "Select Count(*) As total From Forum Where ForumId=" & intForumId & " And ForumManager Like '" & UserId & "'"
        Dim cmd As New OleDbCommand(strSql, conn)
        '下面执行操作
        conn.Open()
        Dim intTotal As Integer = cmd.ExecuteScalar()
        conn.Close()
        If intTotal > 0 Then
            Return (True)
        Else
            Return (False)
        End If
        conn.close()
    End Function


End Class

'------------------------------------------------------------------------------------------------------
'第3个类用来对在线用户进行操作
Public Class DataOnlineUsers              'DataUser是自己定义的类的名称
    Private _strConn As String            '定义一个私有变量,用来设置数据库连接字符串

    '定义一个只读属性,用来返回在线总人数
    Public ReadOnly Property AllOnlineCount() As Integer
        Get
            Dim conn As New OleDbConnection(_strConn)     '这里使用了_strConn表示的数据库连接字符串
            Dim strSql As String = "Select Count(*) As total From OnlineUsers Where IsOnline=True"
            Dim cmd As New OleDbCommand(strSql, conn)     '建立Command对象
            conn.Open()
            Dim intCount As Integer = cmd.ExecuteScalar()   '返回第一行第一列的记录值,也就是在线总人数
            conn.Close()
            Return (intCount)
        End Get
    End Property

    '定义一个只读属性,用来返回在线过客人数
    Public ReadOnly Property GuestOnlineCount() As Integer
        Get
            Dim conn As New OleDbConnection(_strConn)     '这里使用了_strConn表示的数据库连接字符串
            Dim strSql As String = "Select Count(*) As total From OnlineUsers Where IsOnline=True And UserId='过客'"
            Dim cmd As New OleDbCommand(strSql, conn)     '建立Command对象
            conn.Open()
            Dim intCount As Integer = cmd.ExecuteScalar()   '返回第一行第一列的记录值,也就是在线总人数
            conn.Close()
            Return (intCount)
        End Get
    End Property

    '定义一个只读属性,用来返回在线注册会员总数
    Public ReadOnly Property RegisterOnlineCount() As Integer
        Get
            Dim conn As New OleDbConnection(_strConn)     '这里使用了_strConn表示的数据库连接字符串
            Dim strSql As String = "Select Count(*) As total From OnlineUsers Where IsOnline=True And UserId<>'过客'"
            Dim cmd As New OleDbCommand(strSql, conn)     '建立Command对象
            conn.Open()
            Dim intCount As Integer = cmd.ExecuteScalar()   '返回第一行第一列的记录值,也就是在线总人数
            conn.Close()
            Return (intCount)
        End Get
    End Property

    '这是构造函数,在其中给私有变量_strConn赋值
    Public Sub New()
        _strConn = ConfigurationSettings.AppSettings("strConn")
    End Sub

    '该函数用来判断某个用户是否在线
    Public Function IsOnline(ByVal strUserId As String) As Boolean
        Dim conn As New OleDbConnection(_strConn)       '使用了私有变量的值
        '建立Command对象,注意这里使用了含有参数的SQL语句
        Dim strSql = "Select Count(*) As total From OnlineUsers Where IsOnline=True And UserId='" & strUserId & "'"
        Dim cmd As New OleDbCommand(strSql, conn)
        '下面执行操作
        conn.Open()
        Dim intTotal As Integer = cmd.ExecuteScalar()
        conn.Close()
        If intTotal > 0 Then
            Return (True)
        Else
            Return (False)
        End If
    End Function

    '该过程用来处理过客登入,主要用在Global.asax中
    Public Sub LogIn(ByVal strSessionId As String)
        Dim conn As New OleDbConnection(_strConn)       '使用了私有变量的值
        '建立Command对象,注意这里使用了含有参数的SQL语句
        Dim strSql As String = "Insert Into OnlineUsers(SessionId,UserId,LogInTime,IsOnline) Values('" & strSessionId & "','过客',#" & Now() & "#,True)"
        Dim cmd As New OleDbCommand(strSql, conn)
        '下面执行操作
        conn.open()
        cmd.ExecuteNonQuery()
        conn.close()
    End Sub

    '该过程用来将过客转成注册会员
    Public Sub LogChange(ByVal strSessionId As String, ByVal strUserId As String)
        Dim conn As New OleDbConnection(_strConn)       '使用了私有变量的值
        '建立Command对象,注意这里使用了含有参数的SQL语句
        Dim strSql As String = "Update OnlineUsers Set UserId='" & strUserId & "' Where SessionId='" & strSessionId & "'"
        Dim cmd As New OleDbCommand(strSql, conn)
        '下面执行操作
        conn.open()
        cmd.ExecuteNonQuery()
        conn.close()
    End Sub

    '该过程用来处理用户登出,主要用在Global.asax中
    Public Sub LogOut(ByVal strSessionId As String)
        Dim conn As New OleDbConnection(_strConn)       '使用了私有变量的值
        '建立Command对象,注意这里使用了含有参数的SQL语句
        Dim strSql As String = "Update OnlineUsers Set IsOnline=False,LogOutTime=#" & Now() & "# Where SessionId='" & strSessionId & "' And IsOnline=True"
        Dim cmd As New OleDbCommand(strSql, conn)
        '下面执行操作
        conn.open()
        cmd.ExecuteNonQuery()
        conn.close()
    End Sub
End Class

'-------------------------------------------------------------------
'第4个类,用来判断该用户是否已经登录,是否具有适当的权限
Public Class DataUserLog
    '下面是一个共享方法,用来判断该用户是否是普通用户?
    Public Shared Function IsNormal() As Boolean
        If (Not HttpContext.Current.Session("UserId") Is Nothing) And HttpContext.Current.Session("RoleId") = "N" Then
            Return (True)
        Else
            Return (False)
        End If
    End Function

    '下面是一个共享方法,判断该用户是否是管理员?
    Public Shared Function IsAdmin() As Boolean
        If (Not HttpContext.Current.Session("UserId") Is Nothing) And HttpContext.Current.Session("RoleId") = "A" Then
            Return (True)
        Else
            Return (False)
        End If
    End Function

End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -