• 三层实例--加入信息


          对三层的基本知识已经有了一定的了解和掌握,可是要想真正去灵活运用它还须要我们去实践,仅仅有经过亲自己主动手操作实现后才干理解得更加透彻。

          首先我们须要建一张数据表,以我的为例,建一张Users表,加入UserName、Password、Email等字段。

          然后就開始在VS上进行操作,先建一些主要的项目,例如以下表:

          

          实体类是贯穿于三层之间,进行数据传递:

    Public Class UserInfo
        Public ID As Integer
        Public UserName As String
        Public Password As String
        Public Email As String
        Property UID As Integer
            Get
                Return ID
            End Get
            Set(value As Integer)
                ID = value
            End Set
        End Property
        Property UUserName As String
            Get
                Return UserName
            End Get
            Set(value As String)
                UserName = value
            End Set
        End Property
        Property UPassword As String
            Get
                Return Password
            End Get
            Set(value As String)
                Password = value
            End Set
        End Property
        Property UEmail As String
            Get
                Return Email
            End Get
            Set(value As String)
                Email = value
            End Set
        End Property
    End Class
    

          DAL层是对数据库进行操作:

    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports System.Data
    Imports System.Data.SqlClient
    Imports Add.Model
    
    Public Class UserDAO
        Public Function ConnectString() As String     '连接数据库
            ConnectString = "server=.;UID=sa;PWD=123456;database=Login"
    
        End Function
        Public Function AddUser(ByVal userinfo As UserInfo) As Boolean
            Dim con As SqlClient.SqlConnection
            Dim cmd As SqlClient.SqlCommand
    
            Dim intResult As Integer
    
            con = New SqlClient.SqlConnection(ConnectString())
            cmd = con.CreateCommand()
            cmd.CommandText = "insert into Users(UserName,Password,Email) values('" & userinfo.UserName & "','" & userinfo.Password & "','" & userinfo.Email & "')"
    
    
            cmd.CommandType = CommandType.Text
            con.Open()
            intResult = cmd.ExecuteNonQuery()
            If intResult < 0 Then
                Return True
            Else
                Return False
            End If
            con.Close()
            con = Nothing
    
        End Function
        Public Function IsExist(ByVal userinfo As Model.UserInfo) As Boolean
            Dim con As SqlClient.SqlConnection = New SqlClient.SqlConnection(ConnectString)
            Dim cmd As SqlClient.SqlCommand
            Dim dalReader As SqlClient.SqlDataReader
    
            cmd = con.CreateCommand
            cmd.CommandText = "select * from Users where UserName='" & userinfo.UserName & "'"
            cmd.CommandType = CommandType.Text
            con.Open()
            dalReader = cmd.ExecuteReader()
            '推断数据是否已经存在
            If dalReader.HasRows = True Then
                Return True
            Else
                Return False
    
            End If
            con.Close()
            con = Nothing
        End Function
    End Class
    

         BLL层进行逻辑推断,而且实现UI和DAL层的数据交互:

    Imports Add.DAL
    Imports Add.Model
    Public Class Manager
        Public Function Adduser(ByVal user As UserInfo) As Boolean
            Dim dao As New UserDAO
            Dim userinfo As New UserInfo
            If dao.IsExist(user) = True Then
                Return False
            Else
                dao.AddUser(user)
                Return True
    
            End If
        End Function
    End Class

          UI层是用户操作的界面,为用户提供输入的界面和返回信息:

    Imports Add.BLL
    Imports Add.Model
    Public Class Form1
    
        Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
            Dim AddStu As New Manager  '实例化B层
            Dim UStudent As New UserInfo   '实例化实体类
            If Trim(txtUserName.Text) = "" Then
                MessageBox.Show("请输入username")
                Exit Sub
            End If
            If Trim(txtPassword.Text) = "" Then
                MessageBox.Show("请输入password")
                Exit Sub
            End If
            If Trim(txtPassword2.Text) = "" Then
                MessageBox.Show("请确认password")
                Exit Sub
            End If
            If Trim(txtPassword.Text) <> Trim(txtPassword2.Text) Then
                MessageBox.Show("两次password不同样,请又一次输入")
                Exit Sub
            End If
            '将输入的数据存储到实体类,进行传值
            UStudent.UserName = txtUserName.Text
            UStudent.Password = txtPassword.Text
            UStudent.Email = txtEmail.Text
    
            If AddStu.Adduser(UStudent) Then  '用户不存在
                MessageBox.Show("加入成功")
            Else   '用户已经存在
                MessageBox.Show("该用户已注冊,请又一次加入")
            End If
        End Sub
    
        Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
            End
        End Sub
    End Class
    

            运行程序:

          

          

        总结:

           通过这个小样例,让自己对三层的理解更加深刻,也知道了该怎样运用它。原先停留在表面上的理解,至此也升华到了操作上。尽管先前有非常多不理解的地方,可是通过实践,自己得到了理解。所以,当我们遇到问题的时候,不要只去理解,要通过实践去做,唯独真正做过之后才干真正理解。


  • 相关阅读:
    spark基础(1)
    Homebrew的使用教程,镜像源的推荐,安装软件的方法
    Scala Trait(特征)
    P5308 [COCI2019] Quiz
    Vjudge contest 425291
    Vjudge contest 424925
    AT3558 Modern Painting
    Vjudge contest 425061
    Vjudge contest 423849
    Codeforces Round 704
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4233675.html
Copyright © 2020-2023  润新知