Vous êtes sur la page 1sur 7

VB 2008/2010 User Authentication

By Patrick Allan WabwireJr

Step One: Add Service Based Database to your Project 1. Click Project, Choose Add New Item 2. Select Service-based Database 3. Give your Databse a name and Click Add (Database Name in Example is ApplicaionDB)

4. A Data Source Configuration will appear. Click Cancel, Since we have no Tables in the new Database

Patrick Allan WabwireJr wabwireallan@cit.mak.ac.ug www.facebook.com/wabwirejr

Page 1

5. Database you created will appear on your Server Explorer Tab, Under Data Connections. 6. Double Click on your Database (ApplicationDB.mdf)

Patrick Allan WabwireJr wabwireallan@cit.mak.ac.ug www.facebook.com/wabwirejr

Page 2

7. Right Click on Tables and Choose Add Table

8. A Table Definition Tab will appear, Create as Defined below

9. To Save the Table, Right Click on the Table Definition Tab and Save Table1

10. An Input Box will appear, prompting table name. Save table as Users

11. Right click on the Table Definition Tab and Close it(Click Close)

Patrick Allan WabwireJr wabwireallan@cit.mak.ac.ug www.facebook.com/wabwirejr

Page 3

12. Your Table should be visible in the Sever Explorer tab

Step Two: Get Database Connection String 1. Click Data, Choose Add New Data Source 2. A Data Source Configuration will appear. Click Database, then Next

3. Click Next again

Patrick Allan WabwireJr wabwireallan@cit.mak.ac.ug www.facebook.com/wabwirejr

Page 4

4. The Next Dialog box will show you a list on databases you have on your system, Choose the one your created (ApplicationDB.mdf) 5. On the same Dialog Box, The last section should be labeled Connection string 6. Click on the + sign to expand it, and your connection string will be displayed

7. Copy and save your Connection String. It should be something like this; Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ApplicationDB.mdf;Integrated Security=True;User Instance=True 8. Click Cancel, Since we didnt want to create any Datasets, We just wanted to get a Connection String

Patrick Allan WabwireJr wabwireallan@cit.mak.ac.ug www.facebook.com/wabwirejr

Page 5

Step Two: Create Login and Main Form

Object Form

Property Name: Text: Start Possition:


Name: Name: PasswordChar: Name: Text:

Settings Form1 Login CenterScreen


UsernameTextBox PasswordTextBox * btnOK OK

Function User Authentication Form

TextBox TextBox

User Login ID User Login Password

Button

Performs Authentication(tests if Username and Password are Valid) Quits Application.

Button

Name: Text: Name: Text: Start Possition:

btnCancel Cancel Main Main Form CenterScreen

Form

Main Form to be Displayed if Login is Successful

Patrick Allan WabwireJr wabwireallan@cit.mak.ac.ug www.facebook.com/wabwirejr

Page 6

Code
Public Class LoginForm ' TODO: Perform custom authentication using the provided username and password Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click Try Dim MyDbConnection As New SqlClient.SqlConnection Dim MySqlCommand As New SqlClient.SqlCommand Dim MyAdapter As New SqlClient.SqlDataAdapter Dim MyDataset As New DataSet 'Add Connection String (See Pages 4 and5 on how to get your connection string) MyDbConnection.ConnectionString = ("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ApplicationDB.mdf;Integrated Security=True;User Instance=True") MySqlCommand.CommandText = "SELECT * From [Users] WHERE UserName= '" & UsernameTextBox.Text & "' AND Password='" & PasswordTextBox.Text & "' " 'Open Connection and Query the MyDbConnection.Open() MySqlCommand.Connection = MyAdapter.SelectCommand = MyAdapter.Fill(MyDataset, Database MyDbConnection MySqlCommand "0")

'There Can only be one user with the Entered Username and Password Dim Count = MyDataset.Tables(0).Rows.Count If Count = 1 Then Main.Show() 'Form Show When Username and Password are Valid Me.Hide() Me.Visible = False PasswordTextBox.Clear() UsernameTextBox.Clear() Else MsgBox("Please Check Your Username and/or Password and Try Again", MsgBoxStyle.Critical, "Login Failed") PasswordTextBox.Clear() UsernameTextBox.Focus() End If MyDbConnection.Close() Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click Application.Exit() End Sub End Class

Patrick Allan WabwireJr wabwireallan@cit.mak.ac.ug www.facebook.com/wabwirejr

Page 7

Vous aimerez peut-être aussi