HOW TO:Access的身份验证

本文介绍了一种使用ADO.NET进行安全高效的参数化查询的方法,包括直接参数化、定义参数和使用存储过程三种方式,旨在避免SQL注入并提高查询效率。

Author:水如烟  

Public   Class  SimpleWorksDatabase

    
Private   Const  gConnectionString  As   String   =  _
    
" Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|SimpleWorks.mdb;Persist Security Info=True "

    
Private   Function  GetConnection()  As  OleDb.OleDbConnection
        
Return   New  OleDb.OleDbConnection(gConnectionString)
    
End Function

    
Private   Function  GetCommand( ByVal  commandText  As   String As  OleDb.OleDbCommand
        
Return   New  OleDb.OleDbCommand(commandText, GetConnection)
    
End Function

    
' 这种用法不好
     Public   Function  GetLoginIDBySql( ByVal  name  As   String ByVal  pass  As   String As   Integer
        
Dim  mUserID  As  Nullable( Of   Integer )

        
Using  cm  As  OleDb.OleDbCommand  =  GetCommand _
        (
String .Format( " SELECT ID FROM users WHERE userName= '{0}' AND password = '{1}'     " , name, pass))
            
With  cm
                Console.WriteLine(cm.CommandText)
                cm.Connection.Open()
                mUserID 
=  cm.ExecuteScalar
                cm.Connection.Close()
            
End   With
        
End   Using

        
If  mUserID.HasValue  Then
            
Return  mUserID.Value
        
End   If

        
Return   - 1
    
End Function

    
' 可以选取下面三种方法
     Public   Function  GetLoginIDUseParameters( ByVal  name  As   String ByVal  pass  As   String As   Integer
        
Dim  mUserID  As  Nullable( Of   Integer )

        
Using  cm  As  OleDb.OleDbCommand  =  GetCommand( " SELECT ID FROM users WHERE userName= ? AND password = ? " )
            
With  cm
                .Parameters.Add(
" @UserName " , OleDb.OleDbType.VarChar).Value  =  name
                .Parameters.Add(
" @Pass " , OleDb.OleDbType.VarChar).Value  =  pass

                cm.Connection.Open()
                mUserID 
=  cm.ExecuteScalar
                cm.Connection.Close()

            
End   With
        
End   Using

        
If  mUserID.HasValue  Then
            
Return  mUserID.Value
        
End   If

        
Return   - 1
    
End Function

    
Public   Function  GetLoginIDUseParameters2( ByVal  name  As   String ByVal  pass  As   String As   Integer
        
Dim  mUserID  As  Nullable( Of   Integer )

        
Using  cm  As  OleDb.OleDbCommand  =  GetCommand( _
        
" PARAMETERS [@UserName] Text ( 50 ), [@Password] Text ( 50 );  "   &  _
        
" SELECT ID FROM Users WHERE UserName=[@UserName] And Password=[@Password]; " )

            
With  cm
                .Parameters.Add(
" @UserName " , OleDb.OleDbType.VarChar).Value  =  name
                .Parameters.Add(
" @Pass " , OleDb.OleDbType.VarChar).Value  =  pass

                cm.Connection.Open()
                mUserID 
=  cm.ExecuteScalar
                cm.Connection.Close()

            
End   With
        
End   Using

        
If  mUserID.HasValue  Then
            
Return  mUserID.Value
        
End   If

        
Return   - 1
    
End Function

    
' 如果将
     ' PARAMETERS [@UserName] Text ( 50 ), [@Password] Text ( 50 );
     ' SELECT ID FROM Users WHERE UserName=[@UserName] And Password=[@Password];
     ' 存在mdb里作为一个查询,查询命名为sp_LoginID
     ' 那么
     Public   Function  GetLoginIDByProcedure( ByVal  name  As   String ByVal  pass  As   String As   Integer
        
Dim  mUserID  As  Nullable( Of   Integer )

        
Using  cm  As  OleDb.OleDbCommand  =  GetCommand( " Sp_LoginID " )

            
With  cm
                .CommandType 
=  CommandType.StoredProcedure  ' 作为存储过程处理

                .Parameters.Add(
" @UserName " , OleDb.OleDbType.VarChar).Value  =  name
                .Parameters.Add(
" @Pass " , OleDb.OleDbType.VarChar).Value  =  pass

                cm.Connection.Open()
                mUserID 
=  cm.ExecuteScalar
                cm.Connection.Close()

            
End   With
        
End   Using

        
If  mUserID.HasValue  Then
            
Return  mUserID.Value
        
End   If

        
Return   - 1
    
End Function
End Class

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值