首页 > 代码库 > 三层实例--添加信息

三层实例--添加信息

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

      首先我们需要建一张数据表,以我的为例,建一张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

        执行程序:

      

      

    总结:

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


三层实例--添加信息