zoukankan      html  css  js  c++  java
  • 三层实例--添加信息

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

          首先我们需要建一张数据表,以我的为例,建一张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("请输入用户名")
                Exit Sub
            End If
            If Trim(txtPassword.Text) = "" Then
                MessageBox.Show("请输入密码")
                Exit Sub
            End If
            If Trim(txtPassword2.Text) = "" Then
                MessageBox.Show("请确认密码")
                Exit Sub
            End If
            If Trim(txtPassword.Text) <> Trim(txtPassword2.Text) Then
                MessageBox.Show("两次密码不相同,请重新输入")
                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
    

            执行程序:

          

          

        总结:

           通过这个小例子,让自己对三层的理解更加深刻,也知道了该如何运用它。原先停留在表面上的理解,至此也升华到了操作上。虽然先前有很多不理解的地方,但是通过实践,自己得到了理解。所以,当我们遇到问题的时候,不要仅仅去理解,要通过实践去做,只有真正做过之后才能真正理解。


  • 相关阅读:
    【luogu3768】简单的数学题【杜教筛】【欧拉函数】
    【bzoj3589】动态树【树链剖分】【线段树】
    【bzoj4386】[POI2015]Wycieczki【矩阵快速幂】【倍增】
    【bzoj2186】[Sdoi2008]沙拉公主的困惑 【线性筛】【容斥原理】
    【bzoj3884】上帝与集合的正确用法 【欧拉函数】
    【bzoj4417】[Shoi2013]超级跳马 【矩阵快速幂】
    【bzoj3435】【uoj#55】[WC2014]紫荆花之恋 【动态树分治】【平衡树】
    【bzoj3681】Arietta 【网络流】【主席树】【启发式合并】
    【bzoj1532】[POI2005]Kos-Dicing 【网络流】【二分】
    【bzoj1565】[NOI2009]植物大战僵尸 【网络流】【最大权闭合子图】
  • 原文地址:https://www.cnblogs.com/victor-grace/p/7253819.html
Copyright © 2011-2022 走看看