如何在VB 2010 Ultimate中创建登录表单

时间:2014-09-14 16:49:00

标签: vb.net visual-studio-2010

我在登录表单中使用Access 2010需要帮助。 这是我的编码实践,它有一个错误!

Option Explicit On
Imports System.Data.Odbc
Public Class Form1
Dim objCon As New OdbcConnection
Dim strSQL As String
Dim strConnection As String = "Provider= Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Samsung\Desktop\HotelReservation\Practise\Login1.accdb"
Dim da As New OleDb.OleDbDataAdapter
Dim ds As New DataSet
Dim attempt As Integer = 0

Private Sub btnLogin_Click(sender As System.Object, e As System.EventArgs) Handles btnLogin.Click


    If (txtUsername.Text = "") And (txtPassword.Text = "") Then
        MsgBox("Please input username and password!")

    ElseIf (txtUsername.Text = "") Then

        MsgBox("Please input your Username!")
    ElseIf (txtPassword.Text = "") Then

        MsgBox("Please input your Password!")
    Else
        txtUsername.Text = strSQL
        txtPassword.Text = strSQL

        If (txtUsername.Text = strSQL) And (txtPassword.Text = strSQL) Then
            Form2.Show()
        End If
    End If


End Sub
End Class

1 个答案:

答案 0 :(得分:-1)

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
    ' Check if username or password is empty
    If txtPassword.Text = "" Or txtUsername.Text = "" Then
        MessageBox.Show("Please complete the required fields..", "Authentication Error",        MessageBoxButtons.OK, MessageBoxIcon.Error)
    Else
        ' Both fields was supply
        ' Check if user exist in database
        ' Connect to DB
        Dim conn As New System.Data.OleDb.OleDbConnection()
'your connection string
        conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data    Source=C:\Users\Samsung\Desktop\HotelReservation\Practise\Login1.accdb"
         Try
            'conn.Open()
            'MsgBox("Susscess")
'your query
            Dim sql As String = "SELECT * FROM tablename WHERE username='" & txtUsername.Text & "' AND password = '" & txtPassword.Text & "'"
            Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql)

            'Open Database Connection
            sqlCom.Connection = conn
            conn.Open()

            Dim sqlRead As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader()

            If sqlRead.Read() Then
                frmMainForm.Show()
                Me.Hide()

            Else
                ' If user enter wrong username and password combination
                ' Throw an error message
                MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                'Clear all fields
                txtPassword.Text = ""
                txtUsername.Text = ""

                'Focus on Username field
                txtUsername.Focus()
            End If

        Catch ex As Exception
            MessageBox.Show("Failed to connect to Database..", "Database Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End If
End Sub