在vb.net中使用ms访问的登录表单

时间:2010-02-24 13:03:28

标签: vb.net ms-access

我正在使用ms access 2003作为数据库为vb.net创建登录表单。但它只检查用户名并绕过密码。这意味着如果用户名是正确的并且密码没有使用用户名,则用户仍然可以进入系统。这是我的代码:

Try

            Dim NoAcc As String
            Dim NoAccmod2 As String
            Dim NoPas As String

            Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db1.mdb;Jet OLEDB:Database Password=nrew123$%^;")
            Dim cmd As OleDbCommand = New OleDbCommand("Select * from admintable where AdminName= '" & TextBox4.Text & "' ", cn)


            cn.Open()

            rdr = cmd.ExecuteReader
            If rdr.HasRows Then
                rdr.Read()
                NoAcc = rdr("AdminName")
                NoPas = rdr("AdminPass")
                If (TextBox4.Text = NoAcc And TextBox3.Text = NoPas) Then NoAccmod2 = NoAcc

                adminview.Show()



                Me.Hide()
            Else
                MsgBox("Incorrect Username/Password")
                TextBox4.Clear()
                TextBox3.Clear()

            End If
        Catch
            MsgBox("Error logging in, please try again", MsgBoxStyle.Exclamation)
        End Try

如何检查用户名和密码?

6 个答案:

答案 0 :(得分:1)

您正在使用单行IF ..那么:
  If (TextBox4.Text = NoAcc And TextBox3.Text = NoPas) Then NoAccmod2 = NoAcc 所以下一行将永远执行:
  adminview.Show()

你必须重新安排你的IF ..那么条件

答案 1 :(得分:1)

我将使用较少编码的绑定导航器分享vb.net中的登录系统。只需按照下面的链接! http://www.tesear.com/2011/09/login-system-in-vbnet.html

答案 2 :(得分:0)

你可以同时拥有数据库中的uname和pword以及两者中的“WHERE”,如果你没有记录回来,那么你有答案。

答案 3 :(得分:0)

尝试使用System.String.Compare(String str1,String str2, Boolean ) As Integer之类的:

If (System.String.Compare(TextBox4.Text, NoAcc, false) And System.String.Compare(TextBox3.Text, NoPas, false)) Then NoAccmod2 = NoAcc

答案 4 :(得分:0)

这是代码:

Imports System.Data
Imports System.Data.OleDb
Public Class Form5
    Inherits System.Windows.Forms.Form
    Dim mypath = Application.StartupPath & "\login.mdb"
    Dim mypassword = ""
    Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mypath & ";Jet OLEDB:Database Password=" & mypassword)
    Dim cmd As OleDbCommand

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Hide()
        Dim sql = "SELECT UserID ,PassID FROM MYTAB WHERE USERID='" & TextBox1.Text & "' AND PASSID='" & TextBox2.Text & "'"

        cmd = New OleDbCommand(sql, conn)
        conn.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader

        Try
            If dr.Read = False Then
                MessageBox.Show("Authentication failed...")
                Me.Show()
            Else
                MessageBox.Show("Login successfully...")
                Dim frmDialogue As New Form11

                frmDialogue.ShowDialog()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        conn.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Close()
    End Sub


    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Me.Hide()
        Dim frmDialogue As New Form1

        frmDialogue.ShowDialog()
    End Sub


    Private Sub Form5_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Dim frm As New Form1
        frm.Show()
    End Sub
End Class

答案 5 :(得分:0)

尝试

        Dim NoAcc As String

        Dim NoPas As String
        Dim rdr As OleDbDataReader

        Dim cnn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\mobilestore.accdb;Persist Security Info=False;")
        Dim cmd As OleDbCommand = New OleDbCommand("Select * from logindata where Username= '" & TextBox1.Text & "' and password='" & TextBox2.Text & "'", cnn)

        'NoAcc = TextBox1.Text
        'NoPas = TextBox2.Text



        cnn.Open()
        rdr = cmd.ExecuteReader


        If (rdr.Read()) Then

            NoAcc = rdr("Username")
            NoPas = rdr("password")

            If (TextBox1.Text = NoAcc And TextBox2.Text = NoPas) Then
                Adminpage.Show()
                Me.Hide()
            Else
                MsgBox("Incorrect Username/Password")
                TextBox1.Clear()
                TextBox2.Clear()


            End If





        End If
    Catch
        MsgBox("Error logging in, please try again", MsgBoxStyle.Exclamation)
    End Try
End Sub