之前三层的理论已经给大家讲的差不多 了,现在用我们的理论加上我们的实践,来实现一个三层的小例子。
现在来为大家实现一个登录的实例。首先,创建我们的数据库:
设计两个表:
创建此数据库的目的是:用户实现登陆后,要在Score表中自动添加积分。
之后运用三层,将项目分为下面四个:包括UI(界面)层,BLL(逻辑)层,DAL(数据访问)层和Entity(实体)层。
依次展示它们的代码片段:
B层:
Imports LoginEntity
'''
'''
'''
'''
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
'''
''' 创建数据库类的Dbconnstr,方便之后对数据库的调用。
'''
'''
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
Imports System.Data
Imports System.Data.SqlClient
'''
''' 创建访问对象Scores,访问数据表Score,返回符合条件的调用
'''
'''
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
Imports System.Data
Imports System.Data.SqlClient
Imports LoginEntity
'''
''' 创建访问对象Users ,访问数据表uses,返回符合条件的调用
'''
'''
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
Scores.vb:
'''
'''实体类Scores ,映射数据库表Score
'''
'''
Public Class Scores
#Region "定义Score表中的各个属性变量"
Private _id As Integer
Private _userName As String
Private _score As String
#End Region
#Region "定义Score表中的各个属性"
'''
''' ID号
'''
'''
'''
'''
Public Property ID() As Integer
Get
Return _id
End Get
Set(value As Integer)
_id = value
End Set
End Property
'''
''' 用户名
'''
'''
'''
'''
Public Property UserName() As String
Get
Return _userName
End Get
Set(value As String)
_userName = value
End Set
End Property
'''
''' 积分
'''
'''
'''
'''
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,映射数据库表Users
'''
'''
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表中的各个属性"
'''
''' ID 号
'''
'''
'''
'''
Public Property ID() As Integer
Get
Return _id
End Get
Set(value As Integer)
_id = value
End Set
End Property
'''
''' 用户名
'''
'''
'''
'''
Public Property UserName() As String
Get
Return _userName
End Get
Set(value As String)
_userName = value
End Set
End Property
'''
''' 密码
'''
'''
'''
'''
Public Property Password() As String
Get
Return _password
End Get
Set(value As String)
_password = value
End Set
End Property
'''
''' 用户级别
'''
'''
'''
'''
Public Property Level() As String
Get
Return _level
End Get
Set(value As String)
_level = value
End Set
End Property
#End Region
End Class
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
此项目的登录界面:
登录成功后,会弹出一个窗体,显示登录的用户名。
当然,你的数据库里也发生了改变,自己去看看吧。
实例虽小,但也需要认真完成,每个例子中都会有不同的体会的。