Download presentation
Presentation is loading. Please wait.
Published byRonald Clark Modified over 9 years ago
1
Using ADO.Net to Build a Login System Dr. Ron Eaglin
2
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
3
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
4
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
5
Design (Visio)
6
Enterprise Application (in Visio)
7
Create Diagram and Document Design
8
System Architecture Design
9
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
10
Database Design
11
Create New Table
12
Table Design Set as Primary Key So must be unique Null passwords are Not allowed
13
Table Design
14
Stored Procedures Check if login is valid Add new user to database
15
New Stored Procedure
17
Parameter Template (Ctrl-Shift M)
18
Add NewUser Procedure CREATE PROCEDURE AddNewUser @LoginName nvarchar(50), @UserPassword nvarchar(30), @UserName nvarchar(200) AS BEGIN DECLARE @loginNameCount AS smallint SELECT @loginNameCount = COUNT(*) FROM dbo.UserLogin WHERE LoginName = @LoginName
19
AddNewUser Procedure IF @loginNameCount >= 1 BEGIN PRINT 'Login Name Already Exists in Database' RETURN END INSERT INTO dbo.UserLogin (LoginName, UserName, UserPassword) VALUES (@LoginName, @USerName, @UserPassword) END GO
20
Stored Procedure Click Execute
21
Test Stored Procedure
23
isLoginValid Function
24
IsUserValid CREATE FUNCTION isUserValid ( -- Add the parameters for the function here @LoginName nvarchar(50), @UserPassword nvarchar(30) ) RETURNS SMALLINT AS BEGIN DECLARE @Result SMALLINT SELECT @Result = COUNT(*) FROM dbo.UserLogin WHERE LoginName = @LoginName ANDUserPassword = @UserPassword RETURN @Result END GO
25
Test Function
26
UserValidResult Stored Procedure ALTER PROCEDURE [dbo].[UserValidResult] @LoginName nvarchar(50), @UserPassword nvarchar(30), @Result int output AS BEGIN select @Result=dbo.isUserValid(@LoginName,@UserPassword) END
27
Visual Studio 2005 Create Project
28
Windows Application We will create a Windows Application
29
Designer Page Form under construction Solution Explorer Properties of currently Selected object
30
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.
31
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.
32
Setting Properties Select Object Set Properties
33
Connecting to a Database
34
Connect to Database
35
Set Database Server Set Database within Server
36
Database Connection Connection info Will be in the Server explorer.
37
Code Window View Double click on Any object to Open a code Window.
38
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
39
Dim MyDataAdapter As New Data.SqlClient.SqlDataAdapter("UserValidResult", myConnection) MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure MyDataAdapter.SelectCommand.Parameters.Add(New Data.SqlClient.SqlParameter("@LoginName", _ SqlDbType.VarChar, 50)) MyDataAdapter.SelectCommand.Parameters.Add(New Data.SqlClient.SqlParameter("@UserPassword", _ SqlDbType.VarChar, 30)) MyDataAdapter.SelectCommand.Parameters.Add(New Data.SqlClient.SqlParameter("@Result", _ SqlDbType.Int, 4))
40
MyDataAdapter.SelectCommand.Parameters("@LoginName").Value = enteredUsername MyDataAdapter.SelectCommand.Parameters("@UserPassword").Value = enteredUserPassword MyDataAdapter.SelectCommand.Parameters("@Result").Direction = ParameterDirection.Output
41
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 = CInt(MyDataAdapter.SelectCommand.Parameters("@Result").Value) If NumUsersFound = 0 Then MsgBox("No users found") Else MsgBox("Users found") End If myConnection.Close()
42
Results
44
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
45
Skill Summary Database Objects –Connection Object –Data Adapter –Parameter Objects –Data Set
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.