数据增删改查之三层架构(理解三层续)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xhf55555/article/details/7211897

首先是画的图,感觉把图画完了,工程就完成了一大半,前提是画的思路是对的。还是在理解三层。

下面是画的关于数据的增删改查这个小例子的包图、类图和时序图。

 

包图。

 

类图。

时序图。

用户注册。

用户删除。

用户查询,一个是查选全部用户,一个是查询一个用户对象。

用户修改。也就是查询、删除和增加。

用户注册的代码,如下所示:

实体层代码。

Public Class UserInfo
    Private userID As Integer
    Private userName As String
    Private userActor As String
    Private userPwd As String
    Private userRegDate As DateTime
    Private userFlag As Boolean
    '用户编号属性 
    Public Property UserID() As Integer
        Get
            Return userID
        End Get
        Set(ByVal value As Integer)
            userID = value
        End Set
    End Property
    '用户姓名属性
    Public Property UserName() As String
        Get
            Return userName
        End Get
        Set(ByVal value As String)
            userName = value
        End Set
    End Property
    '用户角色
    Public Property UserActor() As String
        Get
            Return userActor

        End Get
        Set(ByVal value As String)
            userActor = value
        End Set
    End Property
    '用户密码
    Public Property UserPwd() As String
        Get
            Return userPwd
        End Get
        Set(ByVal value As String)
            userPwd = value
        End Set
    End Property
    '用户注册日期
    Public Property UserRegDate() As DateTime
        Get
            Return userRegDate

        End Get
        Set(ByVal value As DateTime)
            userRegDate = value
        End Set
    End Property
    '用户合法标致
    Public Property UserFlag
        Get
            Return userFlag

        End Get
        Set(ByVal value)
            userFlag = value
        End Set
    End Property

End Class


DAL层代码。

Imports System.Data.SqlClient



'DAL包中的DALuser类。
Public Class DALUser
    '数据库连接字符串
    Dim connStr As String = "Data Source=192.168.24.63;Initial Catalog=PCharge_Sys;User ID=sa;Password=sa"
    ''' <summary>
    ''' 添加一条用户信息
    ''' </summary>
    ''' <param name="EntityUserInfo">一个用户对象</param>
    ''' <returns>是否添加成功</returns>
    ''' <remarks>2012年1月19号 20:31</remarks>
    Public Function DALUserAdd(ByVal EntityUserInfo As Entity.UserInfo) As Boolean
        Dim sql As String = "Insert into UserInfo(UserID,UserName,UserActor,UserPwd,UserRegDate,UserFlag)" _
                           & " values (@UserID,@UserName,@UserActor,@UserPwd,@UserRegDate,@UserFlag) "

        Dim conn As SqlConnection = New SqlConnection(connStr)


        Dim cmd As SqlCommand = New SqlCommand(sql, conn)
        Dim sqlParam As New SqlParameter

        sqlParam = New SqlParameter("@UserID", SqlDbType.Int)
        sqlParam.Value = EntityUserInfo.UserID
        cmd.Parameters.Add(sqlParam)

        sqlParam = New SqlParameter("@UserName", SqlDbType.VarChar)
        sqlParam.Value = EntityUserInfo.UserName
        cmd.Parameters.Add(sqlParam)

        sqlParam = New SqlParameter("@UserActor", SqlDbType.VarChar)
        sqlParam.Value = EntityUserInfo.UserActor
        cmd.Parameters.Add(sqlParam)

        sqlParam = New SqlParameter("@UserPwd", SqlDbType.VarChar)
        sqlParam.Value = EntityUserInfo.UserPwd
        cmd.Parameters.Add(sqlParam)

        sqlParam = New SqlParameter("@UserRegDate", SqlDbType.DateTime)
        sqlParam.Value = EntityUserInfo.UserRegDate
        cmd.Parameters.Add(sqlParam)

        sqlParam = New SqlParameter("@UserFlag", SqlDbType.Bit)
        sqlParam.Value = EntityUserInfo.UserFlag
        cmd.Parameters.Add(sqlParam)

        Try
            conn.Open()
            Return cmd.ExecuteNonQuery() > 0

        Catch ex As Exception
            Return False
        End Try
        If Not IsNothing(conn) Then
            conn.Close()
            conn = Nothing
        End If


    End Function

End Class


BLL层代码。

Imports DAL

''' <summary>
''' BLL包中的BLLUser类。
''' </summary>
''' <remarks></remarks>
Public Class BLLUser
    ''' <summary>
    ''' 插入一条用户信息
    ''' </summary>
    ''' <param name="EntityUserInfo"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function BLLUserAdd(ByVal EntityUserInfo As Entity.UserInfo) As Boolean
        Dim DALUserInfo As New DAL.DALUser

        Return DALUserInfo.DALUserAdd(EntityUserInfo)
    End Function
End Class


UI层代码。

界面层。

'界面层。
Public Class UserAddForm
    ''' <summary>
    ''' 用户注册,并对用户的输入加以判断。
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>

    Private Sub RegisterBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RegisterBut.Click

       

        If txtUserID.Text = "" Or txtUserName.Text = "" Or txtUserPwd.Text = "" Or txtUserRegDate.Text = "" Or txtUserActor.Text = "" Or txtUserFlag.Text = "" Then
            MsgBox("输入不能为空!请重新输入!")
        Else
            If Not IsNumeric(txtUserID.Text) Then
                MsgBox("输入的编号因为数字!请重新输入!")
            End If
            If Not IsDate(txtUserRegDate.Text) Then
                MsgBox("输入的注册日期请为日期XXXX-XX-XX格式!")
            Else
                Dim EntityUserInfo As New Entity.UserInfo
                EntityUserInfo.UserID = txtUserID.Text
                EntityUserInfo.UserName = txtUserName.Text
                EntityUserInfo.UserActor = txtUserActor.Text
                EntityUserInfo.UserPwd = txtUserPwd.Text
                EntityUserInfo.UserRegDate = txtUserRegDate.Text
                EntityUserInfo.UserFlag = txtUserFlag.Text

                Dim BLLUserInfo As New BLL.BLLUser
                If BLLUserInfo.BLLUserAdd(EntityUserInfo) Then
                    MsgBox("添加成功!")
                Else
                    MsgBox("添加失败!")

                End If
            End If
            

        End If

    End Sub
End Class

上面是在数据库表中插入一条记录,其他的删除和修改,查询类似,就是DAL和BLL层返回值可能不同。

DAL层删除查询代码,保存备后用。

  ''' <summary>
    ''' 获得所有用户
    ''' </summary>
    ''' <returns>所有用户的DataSet集合</returns>
    ''' <remarks></remarks>
    Public Function DALUserSelectAll() As DataSet
        Dim sql As String = "select * from UserInfo"
        Dim conn As SqlConnection = New SqlConnection(connStr)
        Dim cmd As SqlCommand = New SqlCommand(sql, conn)
        Dim dap As SqlDataAdapter = New SqlDataAdapter(cmd)
        Dim ds As New DataSet

        Try
            conn.Open()
            dap.Fill(ds)
            Return ds

        Catch ex As Exception
            Return Nothing
        Finally
            If Not IsNothing(cmd) Then
                cmd.Dispose()
                cmd = Nothing

            End If
            If Not IsNothing(conn) Then
                conn.Close()
                conn = Nothing

            End If
        End Try
    End Function
    ''' <summary>
    ''' 获得一个用户的信息。
    ''' </summary>
    ''' <param name="UserID"></param>
    ''' <returns>一个用户的对象</returns>
    ''' <remarks></remarks>
    Public Function DALUserGetObject(ByVal UserID As Integer) As Entity.UserInfo
        Dim sql As String = "select UserName,UserActor,UserPwd,UserRegDate,UserFlag from UserInfo where UserID=@UserID"
        Dim conn As SqlConnection = New SqlConnection(connStr)
        Dim cmd As SqlCommand = New SqlCommand(sql, conn)
        Dim sqlParam As New SqlParameter("@UserID", SqlDbType.Int)
        sqlParam.Value = UserID
        Dim sdr As SqlDataReader = Nothing

        Dim DALEntityUserinfo As New Entity.UserInfo

        Try
            sdr = cmd.ExecuteReader()
            While sdr.Read
                DALEntityUserinfo.UserID = UserID
                DALEntityUserinfo.UserName = sdr.GetString(0)
                DALEntityUserinfo.UserActor = sdr.GetString(1)
                DALEntityUserinfo.UserPwd = sdr.GetString(2)
                DALEntityUserinfo.UserRegDate = sdr.GetString(3)
                DALEntityUserinfo.UserFlag = sdr.GetString(4)

            End While
            Return DALEntityUserinfo
        Catch ex As Exception
            Return Nothing
        Finally
            If Not IsNothing(sdr) Then
                sdr.Close()
                sdr = Nothing

            End If
            If Not IsNothing(cmd) Then
                cmd.Dispose()
                cmd = Nothing

            End If
            If Not IsNothing(conn) Then
                conn.Close()
                conn = Nothing
            End If
        End Try
    End Function

    Public Function DALUserDel(ByVal DALEntityUserinfo As Entity.UserInfo) As Boolean
        Dim sql As String = "delete from UserInfo where UserID=@UserID"
        Dim conn As SqlConnection = New SqlConnection(connStr)
        Dim cmd As SqlCommand = New SqlCommand(sql, conn)
        Dim sqlParam As New SqlParameter("@UserID", SqlDbType.Int)
        sqlParam.Value = DALEntityUserinfo.UserID
        Try
            conn.Open()
            Return cmd.ExecuteNonQuery() > 0

        Catch ex As Exception
            Return False
        Finally
            If Not IsNothing(cmd) Then
                cmd.Dispose()
                cmd = Nothing

            End If
            If Not IsNothing(conn) Then
                conn.Close()
                conn = Nothing

            End If

        End Try
    End Function

End Class


 


以上只是为了理解而理解,所以做小例子,可能有很多缺陷,欢迎您指教。

展开阅读全文

没有更多推荐了,返回首页