[原创]3层结构理念制作的会员注册

        在这个程序中我们采用了WEBForm页,BLL组件,自定义类还有存储过程来共同完成。

        数据库很简单只有4个字段user_name,User_pwd,user_mail和user_id,详细看看存储过程

CREATE PROCEDURE sp_useradd

(@username 
nvarchar(50),@pwd nvarchar(50),@email nvarchar(50)
,@id 
int output)

 
AS

insert into tb_user (user_name,user_pwd,user_mail) 

values

 (@username,@pwd,@email)

select

@id
=@@Identity
GO

        在此存储过程中,首先定义了4个变量,其中@id变量为output型,执行很简单,直接给tb_user表插入记录,然后返回其ID

        接下来看看我们的自定义类

Imports System.Data.SqlClient
Namespace gaoy
    
Public Class userdb
        
Public Function useradd(ByVal username As StringByVal pwd As StringByVal email As StringAs String
            
Dim conn As New SqlConnection("data source=localhost;initial catalog=aword;user id=sa;pwd=SC")
            
Dim com As New SqlCommand("sp_useradd", conn) '指定存储过程名sp_add
            com.CommandType = CommandType.StoredProcedure '指定要执行的是存储过程
            Dim uname As SqlParameter = New SqlParameter("@username", SqlDbType.NVarChar, 50)
            uname.Value 
= username
            com.Parameters.Add(uname)
            
Dim upwd As SqlParameter = New SqlParameter("@pwd", SqlDbType.NVarChar, 50)
            upwd.Value 
= pwd
            com.Parameters.Add(upwd)
            
Dim uemail As SqlParameter = New SqlParameter("@email", SqlDbType.NVarChar, 50)
            uemail.Value 
= email
            com.Parameters.Add(uemail)
            
Dim id As SqlParameter = New SqlParameter("@id", SqlDbType.Int4)
            id.Direction 
= ParameterDirection.Output '设置此参数为output型参数
            com.Parameters.Add(id)
            
Try
                conn.Open()
                com.ExecuteNonQuery()
                conn.Close()
                
Return "ADD SUCCEEDED ID is" & id.Value.ToString
            
Catch ex As Exception
                
Return ex.ToString
            
End Try
        
End Function

    
End Class

End Namespace

        这个类使用了gaoy命名空间,类名为userdb,该类包含一个useradd方法,用于执行存储过程,并return 该用户的ID或者出错信息。

        第2个类safe 是为了对密码进行加密加密

Imports System.Security.Cryptography
Imports System.Text
Imports System.Text.RegularExpressions

Namespace gaoy
    
Public Class safe
        
Public Shared Function md5(ByVal cleanString As StringAs String
            
Dim clearBytes As [Byte]()
            clearBytes 
= New UnicodeEncoding().GetBytes(cleanString)
            
Dim hashedBytes As [Byte]() = CType(CryptoConfig.CreateFromName("MD5"), HashAlgorithm).ComputeHash(clearBytes)
            
Dim hashedText As String = BitConverter.ToString(hashedBytes)
            
Return hashedText
        
End Function

    
End Class

End Namespace


        在webform页面中,我们拖入4个textbox控件、一个label控件和一个button控件,并为button控件编写处理程序

    Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click
        
' If Page.IsValid Then
        Dim a As gaoy.userdb = New gaoy.userdb
        
Dim b As gaoy.safe
        Label1.Text 
= a.useradd(TextBox1.Text, b.md5(TextBox2.Text), TextBox4.Text)
        
'  End If
    End Sub


        这样,当我们填写了数据后,通过button_onclick程序,我们开始调用类gaoy的方法,将参数通过sqlcommand传递给存储过程,完成整个注册。
posted on 2005-02-17 16:00  雪无止境  阅读(456)  评论(2编辑  收藏  举报