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

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

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

 

包图。

 

类图。

时序图。

用户注册。

用户删除。

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

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

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

实体层代码。

  1. Public Class UserInfo  
  2.     Private userID As Integer  
  3.     Private userName As String  
  4.     Private userActor As String  
  5.     Private userPwd As String  
  6.     Private userRegDate As DateTime  
  7.     Private userFlag As Boolean  
  8.     '用户编号属性   
  9.     Public Property UserID() As Integer  
  10.         Get  
  11.             Return userID  
  12.         End Get  
  13.         Set(ByVal value As Integer)  
  14.             userID = value  
  15.         End Set  
  16.     End Property  
  17.     '用户姓名属性  
  18.     Public Property UserName() As String  
  19.         Get  
  20.             Return userName  
  21.         End Get  
  22.         Set(ByVal value As String)  
  23.             userName = value  
  24.         End Set  
  25.     End Property  
  26.     '用户角色  
  27.     Public Property UserActor() As String  
  28.         Get  
  29.             Return userActor  
  30.   
  31.         End Get  
  32.         Set(ByVal value As String)  
  33.             userActor = value  
  34.         End Set  
  35.     End Property  
  36.     '用户密码  
  37.     Public Property UserPwd() As String  
  38.         Get  
  39.             Return userPwd  
  40.         End Get  
  41.         Set(ByVal value As String)  
  42.             userPwd = value  
  43.         End Set  
  44.     End Property  
  45.     '用户注册日期  
  46.     Public Property UserRegDate() As DateTime  
  47.         Get  
  48.             Return userRegDate  
  49.   
  50.         End Get  
  51.         Set(ByVal value As DateTime)  
  52.             userRegDate = value  
  53.         End Set  
  54.     End Property  
  55.     '用户合法标致  
  56.     Public Property UserFlag  
  57.         Get  
  58.             Return userFlag  
  59.   
  60.         End Get  
  61.         Set(ByVal value)  
  62.             userFlag = value  
  63.         End Set  
  64.     End Property  
  65.   
  66. End Class  


DAL层代码。

  1. Imports System.Data.SqlClient  
  2.   
  3.   
  4.   
  5. 'DAL包中的DALuser类。  
  6. Public Class DALUser  
  7.     '数据库连接字符串  
  8.     Dim connStr As String = "Data Source=192.168.24.63;Initial Catalog=PCharge_Sys;User ID=sa;Password=sa"  
  9.     ''' <summary>  
  10.     ''' 添加一条用户信息  
  11.     ''' </summary>  
  12.     ''' <param name="EntityUserInfo">一个用户对象</param>  
  13.     ''' <returns>是否添加成功</returns>  
  14.     ''' <remarks>2012年1月19号 20:31</remarks>  
  15.     Public Function DALUserAdd(ByVal EntityUserInfo As Entity.UserInfo) As Boolean  
  16.         Dim sql As String = "Insert into UserInfo(UserID,UserName,UserActor,UserPwd,UserRegDate,UserFlag)" _  
  17.                            & " values (@UserID,@UserName,@UserActor,@UserPwd,@UserRegDate,@UserFlag) "  
  18.   
  19.         Dim conn As SqlConnection = New SqlConnection(connStr)  
  20.   
  21.   
  22.         Dim cmd As SqlCommand = New SqlCommand(sql, conn)  
  23.         Dim sqlParam As New SqlParameter  
  24.   
  25.         sqlParam = New SqlParameter("@UserID", SqlDbType.Int)  
  26.         sqlParam.Value = EntityUserInfo.UserID  
  27.         cmd.Parameters.Add(sqlParam)  
  28.   
  29.         sqlParam = New SqlParameter("@UserName", SqlDbType.VarChar)  
  30.         sqlParam.Value = EntityUserInfo.UserName  
  31.         cmd.Parameters.Add(sqlParam)  
  32.   
  33.         sqlParam = New SqlParameter("@UserActor", SqlDbType.VarChar)  
  34.         sqlParam.Value = EntityUserInfo.UserActor  
  35.         cmd.Parameters.Add(sqlParam)  
  36.   
  37.         sqlParam = New SqlParameter("@UserPwd", SqlDbType.VarChar)  
  38.         sqlParam.Value = EntityUserInfo.UserPwd  
  39.         cmd.Parameters.Add(sqlParam)  
  40.   
  41.         sqlParam = New SqlParameter("@UserRegDate", SqlDbType.DateTime)  
  42.         sqlParam.Value = EntityUserInfo.UserRegDate  
  43.         cmd.Parameters.Add(sqlParam)  
  44.   
  45.         sqlParam = New SqlParameter("@UserFlag", SqlDbType.Bit)  
  46.         sqlParam.Value = EntityUserInfo.UserFlag  
  47.         cmd.Parameters.Add(sqlParam)  
  48.   
  49.         Try  
  50.             conn.Open()  
  51.             Return cmd.ExecuteNonQuery() > 0  
  52.   
  53.         Catch ex As Exception  
  54.             Return False  
  55.         End Try  
  56.         If Not IsNothing(conn) Then  
  57.             conn.Close()  
  58.             conn = Nothing  
  59.         End If  
  60.   
  61.   
  62.     End Function  
  63.   
  64. End Class  


BLL层代码。

  1. Imports DAL  
  2.   
  3. ''' <summary>  
  4. ''' BLL包中的BLLUser类。  
  5. ''' </summary>  
  6. ''' <remarks></remarks>  
  7. Public Class BLLUser  
  8.     ''' <summary>  
  9.     ''' 插入一条用户信息  
  10.     ''' </summary>  
  11.     ''' <param name="EntityUserInfo"></param>  
  12.     ''' <returns></returns>  
  13.     ''' <remarks></remarks>  
  14.     Public Function BLLUserAdd(ByVal EntityUserInfo As Entity.UserInfo) As Boolean  
  15.         Dim DALUserInfo As New DAL.DALUser  
  16.   
  17.         Return DALUserInfo.DALUserAdd(EntityUserInfo)  
  18.     End Function  
  19. End Class  


UI层代码。

界面层。

  1. '界面层。  
  2. Public Class UserAddForm  
  3.     ''' <summary>  
  4.     ''' 用户注册,并对用户的输入加以判断。  
  5.     ''' </summary>  
  6.     ''' <param name="sender"></param>  
  7.     ''' <param name="e"></param>  
  8.     ''' <remarks></remarks>  
  9.   
  10.     Private Sub RegisterBut_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles RegisterBut.Click  
  11.   
  12.          
  13.   
  14.         If txtUserID.Text = "" Or txtUserName.Text = "" Or txtUserPwd.Text = "" Or txtUserRegDate.Text = "" Or txtUserActor.Text = "" Or txtUserFlag.Text = "" Then  
  15.             MsgBox("输入不能为空!请重新输入!")  
  16.         Else  
  17.             If Not IsNumeric(txtUserID.Text) Then  
  18.                 MsgBox("输入的编号因为数字!请重新输入!")  
  19.             End If  
  20.             If Not IsDate(txtUserRegDate.Text) Then  
  21.                 MsgBox("输入的注册日期请为日期XXXX-XX-XX格式!")  
  22.             Else  
  23.                 Dim EntityUserInfo As New Entity.UserInfo  
  24.                 EntityUserInfo.UserID = txtUserID.Text  
  25.                 EntityUserInfo.UserName = txtUserName.Text  
  26.                 EntityUserInfo.UserActor = txtUserActor.Text  
  27.                 EntityUserInfo.UserPwd = txtUserPwd.Text  
  28.                 EntityUserInfo.UserRegDate = txtUserRegDate.Text  
  29.                 EntityUserInfo.UserFlag = txtUserFlag.Text  
  30.   
  31.                 Dim BLLUserInfo As New BLL.BLLUser  
  32.                 If BLLUserInfo.BLLUserAdd(EntityUserInfo) Then  
  33.                     MsgBox("添加成功!")  
  34.                 Else  
  35.                     MsgBox("添加失败!")  
  36.   
  37.                 End If  
  38.             End If  
  39.               
  40.   
  41.         End If  
  42.   
  43.     End Sub  
  44. End Class  

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

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

  1.   ''' <summary>  
  2.     ''' 获得所有用户  
  3.     ''' </summary>  
  4.     ''' <returns>所有用户的DataSet集合</returns>  
  5.     ''' <remarks></remarks>  
  6.     Public Function DALUserSelectAll() As DataSet  
  7.         Dim sql As String = "select * from UserInfo"  
  8.         Dim conn As SqlConnection = New SqlConnection(connStr)  
  9.         Dim cmd As SqlCommand = New SqlCommand(sql, conn)  
  10.         Dim dap As SqlDataAdapter = New SqlDataAdapter(cmd)  
  11.         Dim ds As New DataSet  
  12.   
  13.         Try  
  14.             conn.Open()  
  15.             dap.Fill(ds)  
  16.             Return ds  
  17.   
  18.         Catch ex As Exception  
  19.             Return Nothing  
  20.         Finally  
  21.             If Not IsNothing(cmd) Then  
  22.                 cmd.Dispose()  
  23.                 cmd = Nothing  
  24.   
  25.             End If  
  26.             If Not IsNothing(conn) Then  
  27.                 conn.Close()  
  28.                 conn = Nothing  
  29.   
  30.             End If  
  31.         End Try  
  32.     End Function  
  33.     ''' <summary>  
  34.     ''' 获得一个用户的信息。  
  35.     ''' </summary>  
  36.     ''' <param name="UserID"></param>  
  37.     ''' <returns>一个用户的对象</returns>  
  38.     ''' <remarks></remarks>  
  39.     Public Function DALUserGetObject(ByVal UserID As IntegerAs Entity.UserInfo  
  40.         Dim sql As String = "select UserName,UserActor,UserPwd,UserRegDate,UserFlag from UserInfo where UserID=@UserID"  
  41.         Dim conn As SqlConnection = New SqlConnection(connStr)  
  42.         Dim cmd As SqlCommand = New SqlCommand(sql, conn)  
  43.         Dim sqlParam As New SqlParameter("@UserID", SqlDbType.Int)  
  44.         sqlParam.Value = UserID  
  45.         Dim sdr As SqlDataReader = Nothing  
  46.   
  47.         Dim DALEntityUserinfo As New Entity.UserInfo  
  48.   
  49.         Try  
  50.             sdr = cmd.ExecuteReader()  
  51.             While sdr.Read  
  52.                 DALEntityUserinfo.UserID = UserID  
  53.                 DALEntityUserinfo.UserName = sdr.GetString(0)  
  54.                 DALEntityUserinfo.UserActor = sdr.GetString(1)  
  55.                 DALEntityUserinfo.UserPwd = sdr.GetString(2)  
  56.                 DALEntityUserinfo.UserRegDate = sdr.GetString(3)  
  57.                 DALEntityUserinfo.UserFlag = sdr.GetString(4)  
  58.   
  59.             End While  
  60.             Return DALEntityUserinfo  
  61.         Catch ex As Exception  
  62.             Return Nothing  
  63.         Finally  
  64.             If Not IsNothing(sdr) Then  
  65.                 sdr.Close()  
  66.                 sdr = Nothing  
  67.   
  68.             End If  
  69.             If Not IsNothing(cmd) Then  
  70.                 cmd.Dispose()  
  71.                 cmd = Nothing  
  72.   
  73.             End If  
  74.             If Not IsNothing(conn) Then  
  75.                 conn.Close()  
  76.                 conn = Nothing  
  77.             End If  
  78.         End Try  
  79.     End Function  
  80.   
  81.     Public Function DALUserDel(ByVal DALEntityUserinfo As Entity.UserInfo) As Boolean  
  82.         Dim sql As String = "delete from UserInfo where UserID=@UserID"  
  83.         Dim conn As SqlConnection = New SqlConnection(connStr)  
  84.         Dim cmd As SqlCommand = New SqlCommand(sql, conn)  
  85.         Dim sqlParam As New SqlParameter("@UserID", SqlDbType.Int)  
  86.         sqlParam.Value = DALEntityUserinfo.UserID  
  87.         Try  
  88.             conn.Open()  
  89.             Return cmd.ExecuteNonQuery() > 0  
  90.   
  91.         Catch ex As Exception  
  92.             Return False  
  93.         Finally  
  94.             If Not IsNothing(cmd) Then  
  95.                 cmd.Dispose()  
  96.                 cmd = Nothing  
  97.   
  98.             End If  
  99.             If Not IsNothing(conn) Then  
  100.                 conn.Close()  
  101.                 conn = Nothing  
  102.   
  103.             End If  
  104.   
  105.         End Try  
  106.     End Function  
  107.   
  108. End Class  


 


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值