三层数据库四大法宝之“删”
实体层和上一篇一样,这里就不多说了。
U层:
Public Class Form1
'删除
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Try
'获得表现层的数据
Dim muser As New LoginMODEL.User '实例化一个新的实体,用来传递B层的实体
muser.UserName = txtUserName.Text.Trim '将用户名传递给实体层的UserName
muser.PassWord = txtPassWord.Text.Trim '将密码传递给实体层的PassWord
'调用B层,登录判断
Dim mgr As New LoginBLL.UserMananger
Call mgr.DeleteBLL(muser)
Catch ex As Exception
MessageBox.Show(ex.Message.ToString()) '错误处理
End Try
End Sub
End Class
B层:
'删
Public Class UserMananger
Public Sub DeleteBLL(ByVal User As LoginMODEL.User)
Dim uDAO As New LoginDAL.SqlserverUser
'查询用户是否存在,如果存在修改密码,如果不存在提示框
Dim mUser As LoginMODEL.User
mUser = uDAO.SelectDAL(User)
If IsNothing(mUser.UserName) Then
MsgBox("用户不存在,请重新输入", , "警告")
Else
Call uDAO.DeleteDAL(User) '调用删除处功能
End If
End Sub
End Class
D层:
<span style="font-family:KaiTi_GB2312;font-size:24px;">' 引用命名空间
Imports System.Data
Imports System.Data.SqlClient
Imports LoginMODEL
'D层,主要是对原始数据(数据库或者文本文件等存放数据的形式)的操作层,需要引用实体层
Public Class SqlserverUser
Public conn As New SqlConnection("server=ZY-PC;database=Login;user id=sa;password =1") '创建数据库的连接
'删
Public Sub DeleteDAL(ByVal User As User)
'引用化一个实体
Dim sql As String = "delete from T_Users where UserName=@UserName and PassWord=@PassWord " '数据库删除语句
Dim cmd As New SqlCommand(sql, conn)
cmd.CommandText = sql
cmd.CommandType = CommandType.Text
cmd.Parameters.Add(New SqlParameter("@UserName", User.UserName)) '给变量@UserName赋值
cmd.Parameters.Add(New SqlParameter("@PassWord", User.PassWord)) '给@PassWord赋值
conn.Open()
cmd.ExecuteNonQuery() '执行对数据库的操作
conn.Close()
End Sub
End Class</span>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。