三层VB.NET实例

前端之家收集整理的这篇文章主要介绍了三层VB.NET实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

之前三层的理论已经给大家讲的差不多 了,现在用我们的理论加上我们的实践,来实现一个三层的小例子。

现在来为大家实现一个登录的实例。首先,创建我们的数据库


设计两个表:



创建此数据库的目的是:用户实现登陆后,要在score表中自动添加积分。

之后运用三层,将项目分为下面四个:包括UI(界面)层,BLL(逻辑)层,DAL(数据访问)层和Entity(实体)层。


依次展示它们的代码片段:

B层:

Imports LoginEntity

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>

Public Class Login
    Public Function SelectUserLogin(ByVal user As LoginEntity.Users,ByVal forscore As LoginEntity.scores) As LoginEntity.Users
        Dim dUser As New LoginD.Users     '实例化访问users 表的对象
        Dim eUser As LoginEntity.Users  '定义一个用户
        Dim escore As New LoginD.scores
        ' Dim flag As Boolean
        Dim enscore As New LoginEntity.scores

        eUser = dUser.SelectUser(user)

        If IsNothing(eUser.UserName) Then
            Throw New Exception("登录失败,请检查用户名和密码")
        Else
            MsgBox("登录用户:" + eUser.UserName)
            escore.Updatescores(forscore)
            'escore.Updatescores(user.UserName,10)
            Return eUser

        End If

    End Function

End Class

D层:DbConnstr.vb
''' <summary>
''' 创建数据库类的Dbconnstr,方便之后对数据库调用。
''' </summary>
''' <remarks></remarks>
Public Class DbConnstr
    Public Shared Function connstring() As String
        connstring = "server=192.168.24.103;database=Login;user=sa;password=123456;"
    End Function

End Class


scores.vb:
Imports System.Data
Imports System.Data.sqlClient
''' <summary>
'''  创建访问对象scores,访问数据表score,返回符合条件的调用
''' </summary>
''' <remarks></remarks>
Public Class scores
    '定义更新积分表函数

    'Public Sub Updatescores(ByVal username As String,ByVal value As Integer)
    Public Function Updatescores(ByVal score As LoginEntity.scores) As LoginEntity.scores
        Dim conn As New sqlConnection
        Dim cmd As New sqlCommand
        conn = New sqlConnection(LoginD.DbConnstr.connstring())
        cmd.Connection = conn

        cmd.CommandText = "insert into score(username,score)values(@username,@score) "
        cmd.Parameters.Add(New sqlParameter("@username",score.UserName))
        cmd.Parameters.Add(New sqlParameter("@score",score.score))
        'cmd.Parameters.Add(New sqlParameter("@username",username))
        'cmd.Parameters.Add(New sqlParameter("@score",value))
        conn.Open()
        cmd.ExecuteNonQuery()


        conn.Close()
        Return Nothing

    End Function
End Class

Users.vb:
Imports System.Data
Imports System.Data.sqlClient
Imports LoginEntity

''' <summary>
''' 创建访问对象Users,访问数据表uses,返回符合条件的调用
''' </summary>
''' <remarks></remarks>
Public Class Users
    '定义选择用户函数,按传入参数返回查询的记录
    Public Function SelectUser(ByVal user As LoginEntity.Users) As LoginEntity.Users
        Dim conn As New sqlConnection '创建连接对象
        Dim cmd As New sqlCommand    '创建命令对象
        conn = New sqlConnection(LoginD.DbConnstr.connstring())
        cmd.Connection = conn

        '根据输入的用户名和密码查询记录
        '定义查询语句
        cmd.CommandText = "select * from users where UserName=@UserName and Password =@Password"
        '添加命令参数
        cmd.Parameters.Add(New sqlParameter("@UserName",user.UserName))
        cmd.Parameters.Add(New sqlParameter("@Password",user.Password))
        '类型,可以不写为默认值
        cmd.CommandType = CommandType.Text

        Dim users As New LoginEntity.Users
        Try
            conn.Open() '打开连接

            Dim reader As sqlClient.sqlDataReader
            reader = cmd.ExecuteReader    '执行查询


            While (reader.Read)
                If (users Is Nothing) Then    '为空时,延迟加载
                    users = New LoginEntity.Users
                End If
                users.ID = reader.GetInt32(reader.GetOrdinal("ID"))
                users.UserName = reader.GetString(reader.GetOrdinal("UserName"))
                users.Password = reader.GetString(reader.GetOrdinal("Password"))
                If (Not reader.IsDBNull(reader.GetOrdinal("Lever"))) Then
                    users.Level = reader.GetString(reader.GetOrdinal("Level"))
                End If
            End While
            conn.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Return users  '返回查询结果
    End Function
End Class

Entity层:

scores.vb:

''' <summary>
'''实体类scores,映射数据库score
''' </summary>
''' <remarks></remarks>
Public Class scores
#Region "定义score表中的各个属性变量"
    Private _id As Integer
    Private _userName As String
    Private _score As String
#End Region
#Region "定义score表中的各个属性"
    ''' <summary>
    ''' ID号
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set(value As Integer)
            _id = value
        End Set
    End Property
    ''' <summary>
    ''' 用户名
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property UserName() As String
        Get
            Return _userName
        End Get
        Set(value As String)
            _userName = value
        End Set
    End Property
    ''' <summary>
    ''' 积分
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property score() As Integer
        Get
            Return _score
        End Get
        Set(value As Integer)
            _score = value
        End Set
    End Property

#End Region
End Class

Users.vb:
''' <summary>
''' 实体类Users,映射数据库表Users
''' </summary>
''' <remarks></remarks>
Public Class Users
#Region "定义users表中的各个属性变量"
    Private _id As Integer
    Private _userName As String
    Private _password As String
    Private _level As String
#End Region
#Region "定义users表中的各个属性"
    ''' <summary>
    ''' ID 号
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set(value As Integer)
            _id = value
        End Set
    End Property
    ''' <summary>
    ''' 用户名
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property UserName() As String
        Get
            Return _userName
        End Get
        Set(value As String)
            _userName = value
        End Set
    End Property
    ''' <summary>
    ''' 密码
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Password() As String
        Get
            Return _password
        End Get
        Set(value As String)
            _password = value
        End Set
    End Property
    ''' <summary>
    ''' 用户级别
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Level() As String
        Get
            Return _level
        End Get
        Set(value As String)
            _level = value
        End Set
    End Property


#End Region


End Class

UI层:
Public Class Form1

    Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click
        Try
            Dim user1 As New LoginEntity.Users
            Dim user2 As New LoginEntity.Users   '选择User2来登录
            Dim escore As New LoginEntity.scores

            escore.score = 10
            escore.UserName = txtUserName.Text.Trim

            user2.UserName = txtUserName.Text.Trim
            user2.Password = txtPassword.Text


            Dim login As New LoginB.Login
            user1 = login.SelectUserLogin(user2,escore)   ' 将登录后的user2的结果存到user1中


            'MessageBox.Show("登录用户:" + user1.UserName)  '通知用户登录成功
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString)   '通知用户登录失败
        End Try
       
    End Sub

    Private Sub Button2_Click(sender As Object,e As EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

  
End Class
此项目的登录界面:

登录成功后,会弹出一个窗体,显示登录用户名

当然,你的数据库里也发生了改变,自己去看看吧。

实例虽小,但也需要认真完成,每个例子中都会有不同的体会的。

原文链接:https://www.f2er.com/vb/257962.html

猜你在找的VB相关文章