''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' '
' 登錄驗證組件 '
' '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Imports System.Security.Cryptography
Imports System.Text
Imports System.Data
Imports System.Data.SqlClIEnt
Public Class ValidatorClass Validator
Inherits System.ComponentModel.Component
Private username As String
Private userpwd As String
Public Property vUsername()Property vUsername() As String
Get
Return username
End Get
Set(ByVal Value As String)
username = Value
End Set
End Property
Public Property vUserpwd()Property vUserpwd() As String
Get
Return userpwd
End Get
Set(ByVal Value As String)
userpwd = Value
End Set
End Property
'轉換為MD5
Private Function convertMD5()Function convertMD5(ByVal pwd As String) As String
Dim md5 As New MD5CryptoServiceProvider
Dim passWord As Byte() = (New ASCIIEncoding).GetBytes(pwd)
'轉換為哈希值Byte數組
Dim mdByte As Byte() = md5.ComputeHash(passWord)
'Dim mdString As String = System.BitConverter.ToString(mdByte)
Dim mdString As String = (New ASCIIEncoding).GetString(mdByte)
Return mdString
End Function
Public Function validate()Function validate() As Boolean
'連接到Users表
Dim myConnection As New SqlConnection("server=localhost;database=TEST;Trusted_Connection=yes;user id=sa;passWord=;")
Dim selectAdapter As New SqlDataAdapter("select * from Users where UserName='" + username + "'" + "and PassWord='" + convertMD5(userpwd) + "'", myConnection)
Dim ds As New DataSet
Try
selectAdapter.Fill(ds, "Users")
If (ds.Tables(0).Rows.Count > 0) Then
Return True
Else
Return False
End If
Catch ep As SqlException
MsgBox("連接數據庫出錯")
Catch pp As Exception
MsgBox("Oh,發生了不可預料的事情在你身邊,你死定了。退出吧。")
End Try
End Function
#Region " 組件設計器生成的代碼 "
Public Sub New()Sub New(ByVal Container As System.ComponentModel.IContainer)
MyClass.New()
'Windows.Forms 類撰寫設計器支持所必需的
Container.Add(Me)
End Sub
Public Sub New()Sub New()
MyBase.New()
'該調用是組件設計器所必需的。
InitializeComponent()
'在 InitializeComponent() 調用之後添加任何初始化
End Sub
'組件重寫 dispose 以清理組件列表。
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'組件設計器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下過程是組件設計器所必需的
'可以使用組件設計器修改此過程。
'不要使用代碼編輯器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub
#End Region
End Class
簡介:組件其實是一段可以重用的代碼,通過遵循IComponent接口的標准來實現一個組件,所以有組件都是派生於Component類,由Component類來實現IComponent接口。在組件中應正確使用函數的訪問級別來控制外部對其的訪問限制。
只要有足夠的權限就可以將組件放到自己的程序中而不用擔心組件會產生多大的錯誤,因為組件已經經過測試的。比如說可以把一段登錄的程序做成一個組件,或者把經常使用到的一些功能也做成組件,這樣就可以減少開發中的錯誤,也可以縮短開發時間。組件之間也可以互相套用,如一個組件引用另一個組件,都是沒問題,但要先在Add Reference中添加對組件的引用,在.Net中是通過把組件放在程序集中來實現的,程序集中存放著這些組件所依賴的文件信息和所在路徑,因此CLR就可以通過這些信息來確定組件所需要的其他程序集的位置。
( 另外在組件設計過程中應好好利用接口來設計組件)
在VS中創建組件:選建一個Project,再從模板中選Class Library,OK。接著再從Project菜單中Add Component,到些為止,組件的一個框架就呈現在眼前,平台自動繼承了Component類和構造函數。可以刪除原先創建類庫時自動生成的Class1,看應用的需要。接著就可以在組件類裡寫要實現的功能,最後從Build(生成)菜單中選擇Build Solution(生成解決方案)來生成組件。如果生成成功的話,到應用程序的BIN目錄下會看到一個DLL文件。
引用組件:只要在Solution Explorer窗口中,添加對DLL的Reference就可以了。
Imports loginValidator
Imports System.Data
Imports System.Data.SqlClIEnt
Public Class loginFormClass loginForm
Inherits System.Windows.Forms.Form
#Region " Windows 窗體設計器生成的代碼 "
Public Sub New()Sub New()
MyBase.New()
'該調用是 Windows 窗體設計器所必需的。
InitializeComponent()
'在 InitializeComponent() 調用之後添加任何初始化
End Sub
'窗體重寫 dispose 以清理組件列表。
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗體設計器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下過程是 Windows 窗體設計器所必需的
'可以使用 Windows 窗體設計器修改此過程。
'不要使用代碼編輯器修改它。
FrIEnd WithEvents lblUserPwd As System.Windows.Forms.Label
FrIEnd WithEvents lblUserName As System.Windows.Forms.Label
FrIEnd WithEvents txtUserName As System.Windows.Forms.TextBox
FrIEnd WithEvents txtUserPwd As System.Windows.Forms.TextBox
FrIEnd WithEvents btnSubmit As System.Windows.Forms.Button
FrIEnd WithEvents btnExit As System.Windows.Forms.Button
FrIEnd WithEvents Label1 As System.Windows.Forms.Label
FrIEnd WithEvents Label2 As System.Windows.Forms.Label
FrIEnd WithEvents btnCancel As System.Windows.Forms.Button
FrIEnd WithEvents Label3 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(loginForm))
Me.lblUserPwd = New System.Windows.Forms.Label
Me.lblUserName = New System.Windows.Forms.Label
Me.txtUserName = New System.Windows.Forms.TextBox
Me.txtUserPwd = New System.Windows.Forms.TextBox
Me.btnSubmit = New System.Windows.Forms.Button
Me.btnExit = New System.Windows.Forms.Button