Introduction.
Here, you'll learn the use of a VB.NET class library project through
this User Authentication example. Instead of the conventional ADO recordset,
I've used the new Dataset(check out my previous article for more information on
Datasets) to traverse. Once the class is ready and compiled, you can reference
this class in any VB.NET project by selecting the Project -> Add Reference menu
option.
But first, you need to create a database with a table called REGUSERS and the
field's username and password. After the database creation, map it to a DSN
called authtest.
Hope you find this code helpful enough in your .NET endeavor. Long then.
Source Code
Imports
System.ComponentModel
Imports
System.Data
Imports
System.Data
Imports
System.Data.SQL
Imports
System.Data.ADO
Imports
Microsoft.VisualBasic.Compatibility
Public
Class CWS
Inherits
System.ComponentModel.Component
Const
cnStr As
String = "DSN=authTest"
Public
Function authenticate(ByVal
username As
String, ByVal password
As String)
As Boolean
'Create an
ADOConnection instance
Dim
objConn As ADOConnection
objconn = New ADOConnection()
'Set the ADOCOnnection
object's connection string property
objconn.ConnectionString = cnStr
objconn.Open()
'Create a SQL string
Dim
strSQL As
String = "select username from regusers " & _
"where username='" & userName & "' " & _"and password='" & passWord & "'"
'Create an instance of the
ADODatasetcommand and Dataset objects
Dim
objDSCommand As ADODataSetCommand
Dim
objDataset As DataSet
objdataset = New
DataSet()
objdscommand = New
ADODataSetCommand(strsql, objconn)
objdscommand.FillDataSet(objdataset, "auth")
Try
If
objDataset.Tables(0).Rows(0).Item(0).ToString <> ""
Then
Return
True
End
If
Catch
Return
False
End
Try
objconn.Close()
objconn = Nothing
End
Function
Public Sub
New()
MyBase.New()
'This call is required by
the Component Designer.
InitializeComponent()
' TODO: Add any
initialization after the InitializeComponent() call
End
Sub
#Region " Component Designer
generated code "
'Required by the Component
Designer
Private
components As Container
'NOTE: The following
procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
Private
Sub InitializeComponent()
components = New
System.ComponentModel.Container()
End
Sub
#End
Region
End Class