- 爱易网页
-
ASP.NET教程
- VB.NET中的组件开发
日期:2012-11-11 浏览次数:20576 次
先看段组件的代码:(临时写的,写得比较乱)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' '
' 登录验证组件 '
' '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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接口。在组件中应正确使用函数的访问级别来控制外部对其的访问限制。