三层从理论到实践

这几天画图,很迷茫也不知道怎么画。昨天天找了一个三层的例子画了一下图。有了点感觉,话就不多说了这接上图如下图:

三层从理论到实践_第1张图片

包图

三层从理论到实践_第2张图片

 

类图

三层从理论到实践_第3张图片

 

用户查询时序图

三层从理论到实践_第4张图片

用户删除时序图

三层从理论到实践_第5张图片

用户修改时序图

三层从理论到实践_第6张图片

用户注册时序图

三层从理论到实践_第7张图片

界面:

三层从理论到实践_第8张图片

代码:

实体层代码。

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层代码。

ImportsSystem.Data.SqlClient

 

 

 

'DAL包中的DALuser类。

Public Class DALUser

    '数据库连接字符串

    Dim connStr As String = "DataSource=192.168.24.63;Initial Catalog=PCharge_Sys;User ID=sa;Password=sa"

    ''' <summary>

    ''' 添加一条用户信息

    ''' </summary>

    ''' <paramname="EntityUserInfo">一个用户对象</param>

    ''' <returns>是否添加成功</returns>

    ''' <remarks>2012年1月19号20:31</remarks>

    Public Function DALUserAdd(ByValEntityUserInfo As Entity.UserInfo) As Boolean

        Dim sql As String = "Insert intoUserInfo(UserID,UserName,UserActor,UserPwd,UserRegDate,UserFlag)" _

                          & " values(@UserID,@UserName,@UserActor,@UserPwd,@UserRegDate,@UserFlag) "

 

        Dim conn As SqlConnection = NewSqlConnection(connStr)

 

 

        Dim cmd As SqlCommand = NewSqlCommand(sql, conn)

        Dim sqlParam As New SqlParameter

 

        sqlParam = NewSqlParameter("@UserID", SqlDbType.Int)

        sqlParam.Value = EntityUserInfo.UserID

        cmd.Parameters.Add(sqlParam)

 

        sqlParam = NewSqlParameter("@UserName", SqlDbType.VarChar)

        sqlParam.Value =EntityUserInfo.UserName

        cmd.Parameters.Add(sqlParam)

 

        sqlParam = NewSqlParameter("@UserActor", SqlDbType.VarChar)

        sqlParam.Value =EntityUserInfo.UserActor

        cmd.Parameters.Add(sqlParam)

 

        sqlParam = NewSqlParameter("@UserPwd", SqlDbType.VarChar)

        sqlParam.Value = EntityUserInfo.UserPwd

        cmd.Parameters.Add(sqlParam)

 

        sqlParam = NewSqlParameter("@UserRegDate", SqlDbType.DateTime)

        sqlParam.Value =EntityUserInfo.UserRegDate

        cmd.Parameters.Add(sqlParam)

 

        sqlParam = NewSqlParameter("@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>

    ''' <paramname="EntityUserInfo"></param>

    ''' <returns></returns>

    ''' <remarks></remarks>

    Public Function BLLUserAdd(ByValEntityUserInfo As Entity.UserInfo) As Boolean

        Dim DALUserInfo As New DAL.DALUser

 

        ReturnDALUserInfo.DALUserAdd(EntityUserInfo)

    End Function

End Class

 

UI层代码。

界面层。

 

'界面层。

Public ClassUserAddForm

    ''' <summary>

    ''' 用户注册,并对用户的输入加以判断。

    ''' </summary>

    ''' <paramname="sender"></param>

    ''' <paramname="e"></param>

    ''' <remarks></remarks>

 

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

 

      

 

        If txtUserID.Text = "" OrtxtUserName.Text = "" Or txtUserPwd.Text = "" OrtxtUserRegDate.Text = "" Or txtUserActor.Text = "" OrtxtUserFlag.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 NewEntity.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 NewBLL.BLLUser

               IfBLLUserInfo.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() AsDataSet

        Dim sql As String = "select * fromUserInfo"

        Dim conn As SqlConnection = NewSqlConnection(connStr)

        Dim cmd As SqlCommand = NewSqlCommand(sql, conn)

        Dim dap As SqlDataAdapter = NewSqlDataAdapter(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>

    ''' <paramname="UserID"></param>

    ''' <returns>一个用户的对象</returns>

    ''' <remarks></remarks>

    Public Function DALUserGetObject(ByValUserID As Integer) As Entity.UserInfo

        Dim sql As String = "selectUserName,UserActor,UserPwd,UserRegDate,UserFlag from UserInfo whereUserID=@UserID"

        Dim conn As SqlConnection = NewSqlConnection(connStr)

        Dim cmd As SqlCommand = NewSqlCommand(sql, conn)

        Dim sqlParam As NewSqlParameter("@UserID", SqlDbType.Int)

        sqlParam.Value = UserID

        Dim sdr As SqlDataReader = Nothing

 

        Dim DALEntityUserinfo As NewEntity.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(ByValDALEntityUserinfo As Entity.UserInfo) As Boolean

        Dim sql As String = "delete fromUserInfo where UserID=@UserID"

        Dim conn As SqlConnection = NewSqlConnection(connStr)

        Dim cmd As SqlCommand = NewSqlCommand(sql, conn)

        Dim sqlParam As NewSqlParameter("@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

 

这个是来自一篇博客连接如下:http://www.cnblogs.com/loveyong/archive/2012/01/20/2330144.html

你可能感兴趣的:(sql,String,function,cmd,Integer,dataset)