初接触三层

  1. 三层是指显示层,业务逻辑层,数据访问层,是为“高内聚,低耦合”服务的
  2. 除了上述三层之外,一个程序中必须要有的还有实体层,我的理解是实体层中的实体是与中的表相对的,而实体的属性是与数据表中字段相对的,大家还可以根据需要加入外观层,数据接口,抽象工厂层这都是后话,要根据程序的需要进行添加。就像我们今天要实现的登陆这个就不需要外观,数据接口,抽象共厂,其实就连业务逻辑层也可以省略,因为这里并没有逻辑运算。
  3. 显示层引用业务逻辑层,业务逻辑层引用数据访问层,三层均可根据需要引用实体层
  4. 最初敲登陆这个小例子,犯了一个小错误,就是

    1. 这个问题花了我一晚上的时间,也花了我旁边大神半个小时,但都无果。就在我要心灰意冷,将要咬舌自尽之时,我忽然注意到:
        1. 问:解决方案管理器中这四个项目是什么关系?答案是:它们是四个不同的程序集,从属于解决方案,但他们同时也可以被其它解决方案通过添加现有项目添加。(在vb.net中,无论是解决方案,程序集,类,窗体,没有被添加前都被称为项目)
          1. 通过右击解决方案添加的现有项目是程序集。
          2. 通过右击程序集添加的现有项目是类或窗体。
          3. 点击菜单栏文件—>-新建的项目或打开的项目是解决方案
            1. 大家知道我犯的错误了吗?就知道你没看出来,我犯的错误就是,我将上述四个程序集的名称都改为了login,所以出现了上述的错误。
              1. 下面来看一下我的代码,大家一定要理解我开头写的“初接触”三个字,写出这样的代码已经很不错了,大家不要嫌弃。

                显示层<喎? f/ware/vc/”=”” target=”_blank” class=”keylink”>vc3ryb25npjwvcd4kphbyzsbjbgfzcz0=”brush:java;”>imports login.bll imports login.model public class frmlogin public sub btnlogin_click(byval sender as system.object, byval e as system.eventargs) handles btnlogin.click dim admininfo as admininfoentity dim manager as new adminbll dim _name as string dim _password as string _name = txtadminname.text _password = txtadminpwd.text admininfo = manager.selectadmin_info(_name, _password) ‘调用逻辑层,返回的admininfo可能为空,也可能不为空 if admininfo is nothing then messagebox.show(“登陆失败”) else messagebox.show(“登陆用户” + admininfo.name) end if end sub private sub btncancel_click(byval sender as system.object, byval e as system.eventargs) handles btncancel.click me.close() end sub end class 业务逻辑层

                imports login.model
                imports login.dal
                
                
                public class adminbll
                    dim admininfo as admininfoentity
                    dim admindal as new admindal
                
                    '验证用户登录信息是否正确
                    public function selectadmin_info(byval name as string, byval password as string) as admininfoentity
                
                        admininfo = admindal.getadmininfo(name, password) '调用数据访问层,返回的admininfo可能为空,也可能不为空
                
                        return admininfo
                
                    end function
                
                
                end class

                数据访问层

                imports system.data.sqlclient
                imports system.data
                imports system.data.oledb
                imports system
                
                public class connectiontodb
                    public function connectiontodb() as sqlconnection
                        dim con as new sqlconnection("data source=刘颖-pc;initial catalog=room_charge_system;user id=sa;password=123456") '连接数据库
                        con.open()
                        return con
                    end function
                end class

                imports system
                imports system.data
                imports system.data.oledb
                imports system.data.sqlclient
                imports login.model
                
                public class admindal
                    dim admininfo as admininfoentity
                    dim con as new connectiontodb
                    '得到用户信息
                    public function getadmininfo(byval name as string, byval password as string) as admininfoentity
                        '查表中的记录
                        dim cmd as new sqlcommand("select*from t_admin_info where name=" & "'" & name & "'" & "and password=" & "'" & password & "'", con.connectiontodb())
                
                        dim reader as sqldatareader
                        reader = cmd.executereader()
                
                        while (reader.read()) '如果数据表中有用户名和密码都与文本框对应的记录,则利用new 关键字调用admininfoentity构造函数,使admininfo不为空,并对其属性赋值
                            admininfo = new admininfoentity
                
                            admininfo.id = reader.getstring(0)
                            admininfo.name = reader.getstring(1)
                            admininfo.level = reader.getstring(2)
                            admininfo.password = reader.getstring(3)
                
                
                        end while
                
                        return admininfo
                
                    end function
                
                end class

                实体层

                '用户类,分别提供了其他类可以调用的公有属性,也提供了供类内部成员调用的字段,其他类通过属性可以限制对字段的无条件进行操作,例如只读,只写,限定范围赋值。
                public class admininfoentity
                
                    dim _name as string
                    public property name() as string
                        get
                            return _name
                        end get
                        set(byval value as string)
                            _name = value
                        end set
                    end property
                    dim _id as string
                    public property id() as string
                        get
                            return _id
                        end get
                        set(byval value as string)
                            _id = value
                        end set
                    end property
                    dim _level as string
                    public property level() as string
                        get
                            return _level
                        end get
                        set(byval value as string)
                            _level = value
                        end set
                    end property
                    dim _password as string
                    public property password() as string
                        get
                            return _password
                        end get
                        set(byval value as string)
                            _password = value
                        end set
                    end property
                
                end class