本文主要是介绍三层 之 充值,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在三层的地方也炖了不少时间了,再炖就烂了,找好了时机,趁大家不注意,总结一下:
从上面的这幅导图中,就可以看到,由U层(表示层) 引用 B层(逻辑判断层)和 Entity(实体“层”),由 B层 引用 D层和实体层,由D层引用实体层,也就是
只能单向引用,不可反复
下面,还是由实例来说明耕层之间的关系:
充值之界面:
充值之U层:
<pre name="code" class="vb">Public Class Form1Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.ClickTryDim eUser2 As New EntityRecharge.RechargeInfoDim eUser3 As EntityRecharge.RechargeInfoeUser2.IDcard = Trim(txtIDcard.Text)eUser2.AddCash = Trim(txtAddCash.Text)Dim mgr As New RechargeBLL.RechargeManagereUser3 = mgr.RechargeSc(eUser2)Catch ex As ExceptionMessageBox.Show(ex.Message.ToString())End TryEnd SubPrivate Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.ClickMe.Close()End Sub
End Class
充值之B层:
<pre name="code" class="vb">Public Class RechargeManagerPublic Function RechargeSc(ByVal User As RechargeInfo) As EntityRecharge.RechargeInfoDim rDAO As New RechargeDAL.RechargeDAODim eUser1 As BooleaneUser1 = rDAO.SelectUser(User) '寻找卡号If eUser1 = False Then '返回FALSE 代表 查找失败,也就是卡号未注册Throw New Exception("卡号不存在,充值失败")ElseeUser1 = rDAO.UpdateCash(User) '查找成功,执行充值操作If eUser1 = True ThenMsgBox("充值成功")ElseThrow New Exception("充值失败")End IfEnd IfReturn UserEnd Function
End Class
充值之D层:
Public Class RechargeDAOPublic conn As New SqlConnection("server=.;database=Login;user id=sa;password=li")Public Function SelectUser(ByVal User As RechargeInfo) As BooleanDim eUser As New EntityRecharge.RechargeInfoDim reader As SqlDataReader'Dim intNum1 As IntegerDim 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语句的具体类型,在此为Selectcmd.Parameters.Add(New SqlParameter("@IDcard", User.IDcard)) '若用成eUser.UserName则会出现参数未传递的错误提示'MsgBox(conn.State)conn.Open()reader = cmd.ExecuteReaderWhile reader.ReadeUser.IDcard = reader.GetInt32(0)End Whileconn.Close()If eUser.IDcard = Nothing ThenReturn FalseElseReturn TrueEnd IfEnd FunctionPublic Function UpdateCash(ByVal User As RechargeInfo) As BooleanDim intNum As IntegerDim eUser As New EntityRecharge.RechargeInfoDim sql As String = "INSERT INTO Recharge (IDcard,AddCash,Cash) VALUES (@IDcard,@AddCash,@Cash)"Dim cmd As New SqlCommand(sql, conn)cmd.CommandText = sqlcmd.CommandType = CommandType.Textcmd.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 ThenReturn TrueElseReturn False '返回 处理结果End IfEnd FunctionEnd Class
Entity层:
<pre name="code" class="vb">Public Class RechargeInfoDim _idcard As IntegerDim _cash As IntegerDim _addcash As IntegerPublic Property IDcard As Integer '设置IDcard属性GetReturn _idcardEnd GetSet(value As Integer)_idcard = valueEnd SetEnd PropertyPublic Property AddCash As Integer '设置AddCash属性GetReturn _addcashEnd GetSet(value As Integer)_addcash = valueEnd SetEnd PropertyPublic Property Cash As Integer '设置Cash属性GetReturn _cashEnd GetSet(value As Integer)_cash = valueEnd SetEnd Property
End Class
其实吧,这篇和登录那片是差不多的,不一样的地方是,这次在查询的时候,我都用的是Boolean返回值,也就是说,我不需要你查询的数据,我只需要你查询的结果代表的意义,有没有这条数据,有就直接告诉我有,没有就直接告诉我没有,不需要再去比对
这篇关于三层 之 充值的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!