vb.net 《机房收费系统》---②个人版技术总结

时间:2022-12-20 15:39:36

                  一直感觉自己没啥技术水平,在这里只写下自己这次做《机房收费系统》过程中感受最深的地方。

    这次《机房收费系统》用的语言是vb.net,真正的做完了发现vb.net实际上跟vb语言差不哪去,或许vb.net比vb语言更高级了一些吧。因为这次做系统主要脱离了原来面向过程的思想,向完全面向对象的方向靠拢,那么就用到了三层架构思想、设计模式,利用三层架构思想将每一层封装成一个类,自己干自己的活,不用去管别的层是怎么实现的;就像螺丝钉和螺丝母之间的关系,只要是符合规格任何公司产的螺丝母都可以套到螺丝钉上。  

    既然是运用三层架构的思想来实现的,那么每层的任务及三层之间的调用关系一定需要清楚。在"vb.net《机房收费系统》---①个人版思想总结"网易博客中已提到了三层之间的引用关系,这里不再详述。

    从理论上明白每层的任务,可是用代码实现的时候不免做的不合理,现在想想自己做的机房收费系统,我写的BLL层貌似只起到了传递数据的作用,B层很少进行业务逻辑判断,逻辑判断都挪到UI层了。  

    在这里想主要说说我的DAL层,DAL只提供基本的数据访问,不包含任何与业务相关的逻辑处理。DAL主要是跟数据库相连的,这里sql语句自然是少不了的,主要谈谈增、删、改、查的实现。"查"是最简单的了,而进行其他三种操作都少不了查操作.

  

"查询操作(select)":这里我暂时分了四类

①单条件查询操作(一个参数):例如"学生查看余额",利用卡号,从学生信息表中查询信息。

从U层只需传卡号的参数,D层查询语句是:

  Dim strSql As String = "select * from Student_Info where CardNo='" & stuInfo.CardNo & "'"

②时间段查询操作(两个参数):例如"金额退还信息查询",输入"起始日期"和"终止日期",利用时间段查询存在的记录信息。

从U层对日期格式及日期合理性进行判断,D层查询语句是:

 Dim sqlUserMoneyreturn As String = "select * from CardReturn_Info where returnDate between'" & dtpStartdate & "' and '" & dtpEnddate & "'"

③无条件查询操作(无参数):例如"正在值班教师",查询整张教师值班表中的信息。

从U层无需传参数,D层查询语句是:

Dim sqlTchOnworkinfo As String = "select * from teacherOnwork_Info"

④组合条件查询操作,这里我用的是sql语句拼接:例如"学生上机状态查询",利用不同的查询条件从学生上机状态表中查询记录信息。

从U层传递查询条件的参数,在D层查询语句是:

 Dim sqlStuCondition As String
sqlStuCondition = Nothing

If stuOnCondition.Comment1 <> "" And stuOnCondition.Comment2 = "" And stuOnCondition.Comment3 = "" Then
sqlStuCondition = "select CardNo as '卡号',StuNo as '学号',StuName as '学生姓名',Department as '系别',onDate as '上机日期', onTime as '上机时间',Computer as '机子名' from OnLine_Info where " & _
stuOnCondition.Field1 & stuOnCondition.Operater1 & "'" & stuOnCondition.Comment1 & "'"
ElseIf stuOnCondition.Comment1 = "" And stuOnCondition.Comment2 <> "" And stuOnCondition.Comment3 = "" Then
sqlStuCondition = "select CardNo as '卡号',StuNo as '学号',StuName as '学生姓名',Department as '系别',onDate as '上机日期', onTime as '上机时间',Computer as '机子名' from OnLine_Info where " & _
stuOnCondition.Field2 & stuOnCondition.Operater2 & "'" & stuOnCondition.Comment2 & "'"
ElseIf stuOnCondition.Comment1 = "" And stuOnCondition.Comment2 = "" And stuOnCondition.Comment3 <> "" Then
sqlStuCondition = "select CardNo as '卡号',StuNo as '学号',StuName as '学生姓名',Department as '系别',onDate as '上机日期', onTime as '上机时间',Computer as '机子名' from OnLine_Info where " & _
stuOnCondition.Field3 & stuOnCondition.Operater3 & "'" & stuOnCondition.Comment3 & "'"
ElseIf stuOnCondition.Comment1 <> "" And stuOnCondition.Comment2 <> "" And stuOnCondition.Comment3 = "" Then
sqlStuCondition = "select CardNo as '卡号',StuNo as '学号',StuName as '学生姓名',Department as '系别',onDate as '上机日期', onTime as '上机时间',Computer as '机子名' from OnLine_Info where " & _
stuOnCondition.Field1 & stuOnCondition.Operater1 & "'" & stuOnCondition.Comment1 & "' " & stuOnCondition.GroupRelationship1 & " " & stuOnCondition.Field2 & stuOnCondition.Operater2 & "'" & stuOnCondition.Comment2 & "'"
ElseIf stuOnCondition.Comment1 = "" And stuOnCondition.Comment2 <> "" And stuOnCondition.Comment3 <> "" Then
sqlStuCondition = "select CardNo as '卡号',StuNo as '学号',StuName as '学生姓名',Department as '系别',onDate as '上机日期', onTime as '上机时间',Computer as '机子名' from OnLine_Info where " & _
stuOnCondition.Field2 & stuOnCondition.Operater2 & "'" & stuOnCondition.Comment2 & "' " & stuOnCondition.GroupRelationship2 & " " & stuOnCondition.Field3 & stuOnCondition.Operater3 & "'" & stuOnCondition.Comment3 & "'"
ElseIf stuOnCondition.Comment1 <> "" And stuOnCondition.Comment2 = "" And stuOnCondition.Comment3 <> "" Then
sqlStuCondition = "select CardNo as '卡号',StuNo as '学号',StuName as '学生姓名',Department as '系别',onDate as '上机日期', onTime as '上机时间',Computer as '机子名' from OnLine_Info where " & _
stuOnCondition.Field1 & stuOnCondition.Operater1 & "'" & stuOnCondition.Comment1 & "' " & stuOnCondition.GroupRelationship1 & " " & stuOnCondition.Field3 & stuOnCondition.Operater3 & "'" & stuOnCondition.Comment3 & "'"
ElseIf stuOnCondition.Comment1 <> "" And stuOnCondition.Comment2 <> "" And stuOnCondition.Comment3 <> "" Then
sqlStuCondition = "select CardNo as '卡号',StuNo as '学号',StuName as '学生姓名',Department as '系别',onDate as '上机日期', onTime as '上机时间',Computer as '机子名' from OnLine_Info where " & _
stuOnCondition.Field1 & " " & stuOnCondition.Operater1 & "'" & stuOnCondition.Comment1 & "' " & stuOnCondition.GroupRelationship1 & " " & stuOnCondition.Field2 & stuOnCondition.Operater2 & _
" '" & stuOnCondition.Comment2 & "' " & stuOnCondition.GroupRelationship2 & " " & stuOnCondition.Field3 & stuOnCondition.Operater3 & "'" & stuOnCondition.Comment3 & "'"
End If

"删除操作(delete)"

①根据某一条件,从数据库中直接删除:例如"退卡",先判断此卡是否在上机,上机则先结账;从学生信息表查询有无此卡信息,符合条件才能删除。

②从datagridview控件中删除记录,数据库中信息也要跟着删除:其实这里也是先从DataGridView控件中提取某一个字段值,根据此值从数据库中查询记录并删除的。

"修改操作(update)"

例如"充值金额",更新学生信息表中的"余额"项。

Dim strSqlStuInfo As String = "update Student_Info set Balance='" & stuInfoRecharge.Balance & "' where CardNo='" & stuInfoRecharge.CardNo & "'" '更新学生信息表的sql语句

"增加操作(insert)"

①按顺序添加表中的所有字段值:例如"添加用户信息",D层添加操作的Sql语句为

  Dim sqlUserinfoAdd As String = "insert into User_Info values('" & userinfoAdd.UserId & "','" & userinfoAdd.UserName & "','" & userinfoAdd.UserPassword & "','" & userinfoAdd.UserLevel & "')"

②只添加表中的部分字段,那么就需要指出要添加的哪个字段,并指明顺序:例如"用户登录"后,将用户信息写入工作记录表中。

 Dim sqlUserworklog As String = "insert teacherWorklog_Info(UserName,UserLevel,LoginDate,LoginTime) values('" & userWorklog.UserName & "','" & userWorklog.UserLevel & "','" & userWorklog.LoginDate & "','" & userWorklog.LoginTime & "')"  '用户上机后,将用户信息写入工作记录中的sql语句


       =========================分割线===========================


            说说自己需要改进的地方:

   ①连接数据库,我用的是配置配文件,只是会简单的运用,具体有什么深意还真没体会出来。

   ②返回值部分做的不太好,或者说我还不是很理解吧,很多地方我用的都是返回DataTable表,返回值这儿块需要近一步研究。

   ③对SqlDataAdapter 和SqlDataReader的使用我理解的也是不够深刻,系统中主要用的是SqlDataReader来逐条读取数据。

   ④对抛出异常、捕获异常部分理解不深刻。

   ⑤没有用SqlHelper、存储过程,代码自我感觉写的冗余量较大。


    vb.net 《机房收费系统》---②个人版技术总结