在三层的地方也炖了不少时间了,再炖就烂了,找好了时机,趁大家不注意,总结一下:
从上面的这幅导图中,就可以看到,由U层(表示层) 引用 B层(逻辑判断层)和 Entity(实体“层”),由 B层 引用 D层和实体层,由D层引用实体层,也就是
只能单向引用,不可反复
下面,还是由实例来说明耕层之间的关系:
充值之界面:
充值之U层:
Public Class Form1 Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click Try Dim eUser2 As New EntityRecharge.RechargeInfo Dim eUser3 As EntityRecharge.RechargeInfo eUser2.IDcard = Trim(txtIDcard.Text) eUser2.AddCash = Trim(txtAddCash.Text) Dim mgr As New RechargeBLL.RechargeManager eUser3 = mgr.RechargeSc(eUser2) Catch ex As Exception MessageBox.Show(ex.Message.ToString()) End Try End Sub Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click Me.Close() End SubEnd Class
充值之B层:
Public Class RechargeManager Public Function RechargeSc(ByVal User As RechargeInfo) As EntityRecharge.RechargeInfo Dim rDAO As New RechargeDAL.RechargeDAO Dim eUser1 As Boolean eUser1 = rDAO.SelectUser(User) '寻找卡号 If eUser1 = False Then '返回FALSE 代表 查找失败,也就是卡号未注册 Throw New Exception("卡号不存在,充值失败") Else eUser1 = rDAO.UpdateCash(User) '查找成功,执行充值操作 If eUser1 = True Then MsgBox("充值成功") Else Throw New Exception("充值失败") End If End If Return User End FunctionEnd Class
充值之D层:
Public Class RechargeDAO Public conn As New SqlConnection("server=.;database=Login;user id=sa;password=li") Public Function SelectUser(ByVal User As RechargeInfo) As Boolean Dim eUser As New EntityRecharge.RechargeInfo Dim reader As SqlDataReader 'Dim intNum1 As Integer Dim sql As String = "SELECT * FROM Recharge WHERE IDcard=@IDcard" Dim cmd As New SqlCommand(sql, conn) cmd.CommandText = sql '获取SQL语句的具体内容 cmd.CommandType = CommandType.Text '获取上述SQL语句的具体类型,在此为Select cmd.Parameters.Add(New SqlParameter("@IDcard", User.IDcard)) '若用成eUser.UserName则会出现参数未传递的错误提示 'MsgBox(conn.State) conn.Open() reader = cmd.ExecuteReader While reader.Read eUser.IDcard = reader.GetInt32(0) End While conn.Close() If eUser.IDcard = Nothing Then Return False Else Return True End If End Function Public Function UpdateCash(ByVal User As RechargeInfo) As Boolean Dim intNum As Integer Dim eUser As New EntityRecharge.RechargeInfo Dim sql As String = "INSERT INTO Recharge (IDcard,AddCash,Cash) VALUES (@IDcard,@AddCash,@Cash)" Dim cmd As New SqlCommand(sql, conn) cmd.CommandText = sql cmd.CommandType = CommandType.Text cmd.Parameters.Add(New SqlParameter("@IDcard", User.IDcard)) cmd.Parameters.Add(New SqlParameter("@AddCash", User.AddCash)) cmd.Parameters.Add(New SqlParameter("@Cash", 11 + User.AddCash)) conn.Open() '打开连接 intNum = cmd.ExecuteNonQuery() '返回 受影响行数 conn.Close() '关闭连接 If intNum = 1 Then Return True Else Return False '返回 处理结果 End If End FunctionEnd ClassEntity层:
其实吧,这篇和登录那片是差不多的,不一样的地方是,这次在查询的时候,我都用的是Boolean返回值,也就是说,我不需要你查询的数据,我只需要你查询的结果代表的意义,有没有这条数据,有就直接告诉我有,没有就直接告诉我没有,不需要再去比对Public Class RechargeInfo Dim _idcard As Integer Dim _cash As Integer Dim _addcash As Integer Public Property IDcard As Integer '设置IDcard属性 Get Return _idcard End Get Set(value As Integer) _idcard = value End Set End Property Public Property AddCash As Integer '设置AddCash属性 Get Return _addcash End Get Set(value As Integer) _addcash = value End Set End Property Public Property Cash As Integer '设置Cash属性 Get Return _cash End Get Set(value As Integer) _cash = value End Set End PropertyEnd Class