Using ADO.Net to Build a Login System Dr. Ron Eaglin
Lecture This lecture will take you step by step through using SQL Server 2005 to create a Database Driven login system. –Requires SQL Server 2005 –Requires Visual Studio 2005
Requirements Login and password information will be stored in a database. Users will be presented a login screen that will allow them to; –Create a user account –Authenticate a login
Steps 1. Design Overall System Architecture 2. Design Database to Meet Requirements 3. Design Software System to Meet Needs 4. Build 5. Test 6. Deploy
Design (Visio)
Enterprise Application (in Visio)
Create Diagram and Document Design
System Architecture Design
Database Design Must be able to –(1) Keep names for users –(2) Keep login names for users –(3) Store passwords for users –(4) Check validity of password –(5) Create new users –(6) Verify new login names do not conflict
Database Design
Create New Table
Table Design Set as Primary Key So must be unique Null passwords are Not allowed
Table Design
Stored Procedures Check if login is valid Add new user to database
New Stored Procedure
Parameter Template (Ctrl-Shift M)
Add NewUser Procedure CREATE PROCEDURE nvarchar(200) AS BEGIN AS smallint = COUNT(*) FROM dbo.UserLogin WHERE LoginName
AddNewUser Procedure >= 1 BEGIN PRINT 'Login Name Already Exists in Database' RETURN END INSERT INTO dbo.UserLogin (LoginName, UserName, END GO
Stored Procedure Click Execute
Test Stored Procedure
isLoginValid Function
IsUserValid CREATE FUNCTION isUserValid ( -- Add the parameters for the function nvarchar(30) ) RETURNS SMALLINT AS BEGIN SMALLINT = COUNT(*) FROM dbo.UserLogin WHERE LoginName ANDUserPassword END GO
Test Function
UserValidResult Stored Procedure ALTER PROCEDURE int output AS BEGIN select END
Visual Studio 2005 Create Project
Windows Application We will create a Windows Application
Designer Page Form under construction Solution Explorer Properties of currently Selected object
VS Toolbox VS Toolbox contains all the objects That can be placed on the user form. In this example we will have a text box For the login name, the password, and A submit button.
Designing Form Place 2 labels (drag and drop From the toolbox, a text box And a masked text box on The form. Also drop a button. Line each of the items up.
Setting Properties Select Object Set Properties
Connecting to a Database
Connect to Database
Set Database Server Set Database within Server
Database Connection Connection info Will be in the Server explorer.
Code Window View Double click on Any object to Open a code Window.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim enteredUsername As String = Trim(TextBox1.Text) Dim enteredUserPassword As String = Trim(MaskedTextBox1.Text) Dim NumUsersFound As Integer Dim myConnection As New Data.SqlClient.SqlConnection myConnection.ConnectionString = "Data Source=WHITEWATER;Initial Catalog=CET4429;Integrated Security=True" Try myConnection.Open() Catch ex As Exception MsgBox("Could not open Connection", MsgBoxStyle.Information) Return End Try
Dim MyDataAdapter As New Data.SqlClient.SqlDataAdapter("UserValidResult", myConnection) MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure MyDataAdapter.SelectCommand.Parameters.Add(New _ SqlDbType.VarChar, 50)) MyDataAdapter.SelectCommand.Parameters.Add(New _ SqlDbType.VarChar, 30)) MyDataAdapter.SelectCommand.Parameters.Add(New _ SqlDbType.Int, 4))
= enteredUsername = enteredUserPassword = ParameterDirection.Output
Dim DS As New DataSet() 'Create a new DataSet to hold the records. MyDataAdapter.Fill(DS, "UserValidResult") 'Fill the DataSet with the rows returned NumUsersFound = If NumUsersFound = 0 Then MsgBox("No users found") Else MsgBox("Users found") End If myConnection.Close()
Results
Skill Summary User SQL Server 2005 Design and Create Tables Design and Create Application Create Stored Procedures and Functions Use Visual Studio Create a Windows Application Design a Form Use Database Object
Skill Summary Database Objects –Connection Object –Data Adapter –Parameter Objects –Data Set