VB.Net登录问题(三层),不读取数据~~~
三层代码分别如下:
实体(Entity)层:
Public Class enUserInfo
'Users表,用于登录
'UserID属性
Private _userid As Integer
Public Property UserID As Integer
Set(ByVal value As Integer)
_userid = value
End Set
Get
Return _userid
End Get
End Property
'UserName属性
Private _username As String
Public Property UserName As String
Get
Return _username
End Get
Set(ByVal value As String)
_username = value
End Set
End Property
'PassWord属性
Private _password As String
Public Property PassWord As String
Get
Return _password
End Get
Set(ByVal value As String)
_password = value
End Set
End Property
'Level属性
Private _level As String
Public Property Level As String
Get
Return _level
End Get
Set(ByVal value As String)
_level = value
End Set
End Property
End Class
Public Class DBConnectDAL
Public Shared Connstring As String = "server=192.168.24.200; database=SchoolRecharge_sys;user id=sa;password=123456"
End Class
'引用命名空间
Imports System.Data
Imports System.Data.SqlClient
Imports Entity
''' <summary>
''' 建立对数据库表User和Student的连接,查找并返回相应项
''' </summary>
''' <remarks></remarks>
Public Class UserDataDAL
Public conn As New SqlConnection(DBConnectDAL.Connstring) '实例化连接
''' <summary>
''' 函数SelectUser()为一般用户、操作员和管理员登录使用
''' </summary>
''' <param name="UserName">用户姓名</param>
''' <param name="PassWord">用户密码</param>
''' <returns>返回实体</returns>
''' <remarks></remarks>
Public Function SelectUser(ByVal UserName As String, ByVal PassWord As String, ByVal Level As String) As Entity.enUserInfo
Dim reader As SqlDataReader '定义参数reader类型为sqldatareader
Dim strSql As String '定义sql字符串
strSql = "Select UserName,PassWord,Level from Users Where UserName=@UserName And PassWord =@PassWord And Level=@Level "
Dim strCmd As New SqlCommand(strSql, conn) '实例化sqlCommand
Dim enUser As New Entity.enUserInfo '实例化一个新的用来登录的enUserInfo
strCmd.CommandText = strSql '获取要对数据源执行的T-SQL语句或存储过程
strCmd.CommandType = CommandType.Text '获取T-SQL命令的类型,如Select、Update等
'分别给参数@UserName、@PassWord、@Level传递值
strCmd.Parameters.Add(New SqlParameter("@UserName", UserName))
strCmd.Parameters.Add(New SqlParameter("@PassWord", PassWord))
strCmd.Parameters.Add(New SqlParameter("@Level", Level))
conn.Open() '打开连接
reader = strCmd.ExecuteReader() '执行重载,将CommandText发送到Connection并生成一个DataReader
While reader.Read() '读取数据库中的数据,赋值给用户名(UserName)和密码(PassWord)及用户级别(Level)
enUser.UserName = reader.GetString(0)
enUser.PassWord = reader.GetString(1)
enUser.Level = reader.GetString(2)
End While
Return enUser '返回一个enUserInfo的实例enUser
conn.Close() '关闭连接
End Function
B层:
Public Class LoginManagerBLL
Public Function UserLogin(ByVal UserName As String, ByVal PassWord As String, ByVal Level As String) As Entity.enUserInfo
Dim uDAL As New DAL.UserDataDAL '实例化D层的UserDataDAL对象
Dim enUser As Entity.enUserInfo '定义一个类型为实体层enUserInfo的参数
enUser = uDAL.SelectUser(UserName, PassWord, Level) '传值
'判断是否查询到记录,若有,登录成功,并返回实体enUser
If enUser.PassWord Is Nothing Then
Throw New Exception("登录失败,请检查用户名和密码!")
Else
Return enUser
MsgBox("登录成功,马上进入系统.......", MsgBoxStyle.OkOnly, "恭喜")
End If
End Function
End Class
Public Class frmLogin
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Try
'获得用户输入的数据
Dim enUser As New Entity.enUserInfo
Dim enuser1 As Entity.enUserInfo
'分别将U层的用户名、密码和级别传递给实体层
enUser.UserName = txtUserName.Text.Trim()
enUser.PassWord = txtUserName.Text.Trim()
enUser.Level = cmbLevel.Text
'调用B层,登录判断
Dim mgr As New BLL.LoginManagerBLL
enuser1 = mgr.UserLogin(enUser.UserName, enUser.PassWord, enUser.Level)
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
End Try
End Sub
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
End Class
Shared Sub Main()
con = New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI")
Dim cmd As New SqlCommand("SELECT FirstName, LastName FROM Employee WHERE FirstName = @fn", con)
cmd.Parameters.Add(New SqlParameter("@fn", SqlDbType.VarChar, 10)).Value = "Joe"
Try
con.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
Console.WriteLine("{0} - {1}", reader.GetString(0), reader.GetString(1))
End While
reader.Close()
Finally
con.Close()
End Try
End Sub