VB.net编写登录对话框时:怎样在仅输入用户名而密码为空的情况下照样能登录后台数据库 5
问题如下:用vb.net编写了登录对话框的程序,想做到对话框只输入用户名,密码为空。数据库相应密码的字段也为空,但不知道怎么匹配?请高手解决。ImportsSystem....
问题如下:用vb.net编写了登录对话框的程序,想做到对话框只输入用户名,密码为空。数据库相应密码的字段也为空,但不知道怎么匹配?请高手解决。
Imports System.Data.OleDb
Public Class frmlogin
Dim i As Integer
Dim Conn As OleDb.OleDbConnection
Dim Cmd As OleDb.OleDbCommand
Dim Rd As OleDb.OleDbDataReader
Dim SQL As String
Dim Provider = "Provider=Microsoft.Jet.OLEDB.4.0"
'Dim Database = "Data Source=" & Application.StartupPath & "\Database\data.mdb"
Dim Database = "Data Source=D:\dbase.mdb"
Private Sub OK_Click(sender As Object, e As EventArgs) Handles OK.Click
Conn = New OleDb.OleDbConnection(Provider & ";" & Database)
Conn.Open()
SQL = "Select * From 登录管理 Where 用户名='" & txtUserName.Text & "' And 密码='" & txtPassword.Text & "'"
Cmd = New OleDb.OleDbCommand(SQL, Conn)
Rd = Cmd.ExecuteReader()
If Rd.Read() Then
MsgBox("登录成功!感谢您使用本软件!")
MyBase.Hide()
FrmMain.Show()
Else
If i = 1 Then
MsgBox("对不起!您是非授权用户!", vbOKOnly + vbCritical, "操作员登录")
Rd.Close()
Conn.Close()
Me.Dispose()
Application.Exit()
End If
MsgBox("用户名或密码输入错误,你还有" + CStr(i - 1) + "次机会了, 请重新输入!", vbOKOnly + vbCritical, "测量员登录")
txtUserName.Text = ""
txtPassword.Text = ""
txtUserName.Focus()
i = i - 1
End If
Rd.Close()
Conn.Close()
'MsgBox(ds.Tables("登录管理").Rows(j)(1))
End Sub
Private Sub cmdclose_Click(sender As Object, e As EventArgs) Handles cmdclose.Click
Application.Exit()
End Sub
Private Sub cmdMin_Click(sender As Object, e As EventArgs) Handles cmdMin.Click
WindowState = FormWindowState.Minimized
End Sub
Private Sub cmdCancel_Click(sender As Object, e As EventArgs) Handles cmdCancel.Click
Application.Exit()
End Sub
Private Sub frmlogin_Load(sender As Object, e As EventArgs) Handles Me.Load
i = 3
txtUserName.Text = ""
txtPassword.Text = ""
txtPassword.PasswordChar = "*"
txtUserName.Focus()
End Sub
End Class 展开
Imports System.Data.OleDb
Public Class frmlogin
Dim i As Integer
Dim Conn As OleDb.OleDbConnection
Dim Cmd As OleDb.OleDbCommand
Dim Rd As OleDb.OleDbDataReader
Dim SQL As String
Dim Provider = "Provider=Microsoft.Jet.OLEDB.4.0"
'Dim Database = "Data Source=" & Application.StartupPath & "\Database\data.mdb"
Dim Database = "Data Source=D:\dbase.mdb"
Private Sub OK_Click(sender As Object, e As EventArgs) Handles OK.Click
Conn = New OleDb.OleDbConnection(Provider & ";" & Database)
Conn.Open()
SQL = "Select * From 登录管理 Where 用户名='" & txtUserName.Text & "' And 密码='" & txtPassword.Text & "'"
Cmd = New OleDb.OleDbCommand(SQL, Conn)
Rd = Cmd.ExecuteReader()
If Rd.Read() Then
MsgBox("登录成功!感谢您使用本软件!")
MyBase.Hide()
FrmMain.Show()
Else
If i = 1 Then
MsgBox("对不起!您是非授权用户!", vbOKOnly + vbCritical, "操作员登录")
Rd.Close()
Conn.Close()
Me.Dispose()
Application.Exit()
End If
MsgBox("用户名或密码输入错误,你还有" + CStr(i - 1) + "次机会了, 请重新输入!", vbOKOnly + vbCritical, "测量员登录")
txtUserName.Text = ""
txtPassword.Text = ""
txtUserName.Focus()
i = i - 1
End If
Rd.Close()
Conn.Close()
'MsgBox(ds.Tables("登录管理").Rows(j)(1))
End Sub
Private Sub cmdclose_Click(sender As Object, e As EventArgs) Handles cmdclose.Click
Application.Exit()
End Sub
Private Sub cmdMin_Click(sender As Object, e As EventArgs) Handles cmdMin.Click
WindowState = FormWindowState.Minimized
End Sub
Private Sub cmdCancel_Click(sender As Object, e As EventArgs) Handles cmdCancel.Click
Application.Exit()
End Sub
Private Sub frmlogin_Load(sender As Object, e As EventArgs) Handles Me.Load
i = 3
txtUserName.Text = ""
txtPassword.Text = ""
txtPassword.PasswordChar = "*"
txtUserName.Focus()
End Sub
End Class 展开
展开全部
你这意图没说明白,如果密码为空都能登录那么密码没有任何意义.
我理解为你是想让Admin用户可以不用密码登录.
那么在登录时候判断用户名是不是等于Admin就行.
或者在数据里加上一个权限字段,判断用户是不是有不用密码登录的权限.
我理解为你是想让Admin用户可以不用密码登录.
那么在登录时候判断用户名是不是等于Admin就行.
或者在数据里加上一个权限字段,判断用户是不是有不用密码登录的权限.
追问
可能我表述不太清楚。数据库相应字段的密码为空,我在窗体的文本框上什么也不输入。但我上述程序的编写存在问题,它不认为这样是相等的关系。我也知道出现这个问题的原因是文本框的返回值是空字符串,而数据库空字段的返回值是NULL,所以它们之间肯定是不相等的。在程序中做点什么样的修改,才能解决这个问题。
追答
方案一改sql语句
SQL = "Select * From 登录管理 Where 用户名='" & txtUserName.Text & "' And 密码" & iif(isNothing(txtPassword.Text, " is null" , "='" & txtPassword.Text & "'")
方案二,修改access数据库,密码字段默认值为""空字段,必填字段
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询