Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2016. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: Login Demo (This is not a secure app. Demo only!)
- '
- ' Author: Joseph L. Bolen
- ' Date Created: 01 JUN 2016
- '
- ' Description: This Login Form is from the standard Visual Studio templates.
- ' The demo is to show how to use the ExecuteScalar method in
- ' a separate class to return a boolean value of a success or
- ' failure in finding a record in a table. The ConnectionString
- ' is retrieved from the App.config file.
- '
- ' NOTE: This is not a secure app. Demo only! Salted Password Hashing
- ' should be implemented.
- Imports System.Data.SqlClient
- Public Class LoginForm
- Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
- Handles OK.Click
- ' Validate input.
- If String.IsNullOrWhiteSpace(UsernameTextBox.Text) OrElse
- String.IsNullOrWhiteSpace(PasswordTextBox.Text) Then
- MessageBox.Show("Both User Name and Password values are required.",
- "Logon Error",
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- Exit Sub
- End If
- Try
- If UsersDB.IsUserFound(UsernameTextBox.Text, PasswordTextBox.Text) Then
- 'Note: Application Shutdown Mode has been set to "When last form closes".
- Dim appForm As New MainForm
- appForm.Show()
- Me.Close()
- Else
- MessageBox.Show("Incorrect User Name and/or Password values were entered.",
- "Logon Error",
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- UsernameTextBox.Select(0, 0)
- UsernameTextBox.Focus()
- Exit Sub
- End If
- Catch ex As DataException ' General ADO.NET Component error.
- MessageBox.Show(ex.Message,
- ex.GetType.ToString,
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- Catch ex As SqlException ' SQLException error.
- MessageBox.Show("Database error # 0x" & ex.ErrorCode.ToString("X") & " - " & ex.Message,
- ex.GetType.ToString,
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- Catch ex As Exception ' General catch all error.
- MessageBox.Show(ex.Message,
- ex.GetType.ToString,
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- End Try
- End Sub
- Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
- Handles Cancel.Click
- Me.Close()
- End Sub
- End Class
- '-------------------------------------------------------------------------------------------
- ' Class Name: UsersDB
- '
- ' Author: Joseph L. Bolen
- ' Date Created: 01 JUN 2016
- '
- ' Description: For this demonstration, the Users table from the MS SQL Express
- ' database MyTestDB is being used. The database's file location
- ' is retrieved from the App.config file.
- '
- Imports System.Data.SqlClient
- Public Class UsersDB
- Public Shared Function IsUserFound(ByVal user As String, ByVal psw As String) As Boolean
- Dim found As Boolean = False
- Dim query As String = "SELECT TOP 1 1" &
- "FROM Users " &
- "WHERE UserName = @UserName AND UserPassword = @UserPassword;"
- Try
- Using con As SqlConnection = MyTestDB.GetConnection()
- Using cmd As New SqlCommand(query, con)
- cmd.Parameters.AddWithValue("@UserName", user)
- cmd.Parameters.AddWithValue("@UserPassword", psw)
- con.Open()
- found = Convert.ToBoolean(cmd.ExecuteScalar())
- End Using
- End Using
- Catch ex As Exception
- Throw ex
- End Try
- Return found
- End Function
- End Class
- '-------------------------------------------------------------------------------------------
- ' Class Name: MyTestDB
- '
- ' Author: Joseph L. Bolen
- ' Date Created: 01 JUN 2016
- '
- ' Description: For this demonstration, the Users table from the MS SQL Express
- ' database MyTestDB is being used. The database's file location
- ' is retrieved from the App.config file.
- '-------------------------------------------------------------------------------------------
- 'Note: Add Reference to System.Configuration to the Project.
- Imports System.Configuration
- Imports System.Data.SqlClient
- Public Class MyTestDB
- Public Shared Function GetConnection() As SqlConnection
- Dim conn As String = ConfigurationManager.ConnectionStrings("MyTestDB").ConnectionString
- Return New SqlConnection(conn)
- End Function
- End Class
- The App.Config file...
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <startup>
- <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
- </startup>
- <connectionStrings>
- <add name="MyTestDB" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MyTestDB;Integrated Security=True;"
- providerName="System.Data.SQLClient"/>
- </connectionStrings>
- </configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement