|
Trouver une ressource
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
CLASS CONNEXION ADODB SECURISE
Information sur la source
Description
C'est une classe facilitant la mise en oeuvre d'une connexion ADODB par du code. De plus, via les fonctions de formatage SQL, elle permet de sécuriser vos applications contre le SQL Injection (testé seulement pour MySQL, a adapté sur autres bases tel que SQL Serveur). De plus vous trouverez le debugage facilité pour vos applications (requete sql plus visible, centralisation du code) . Vous pouvez facilement logguer les requestes sql
Source
- 'Développé par ZAPPY zappy@gosney42.org
- 'Pour toute utilisation commercial, contactez l'auteur
- 'En cas de redistribution, vous êtes tenu de livrer tel quel, avec le copyright
-
- Option Explicit
- Public oConn As ADODB.Connection
-
- Public bSilent As Boolean
- Public strConnexionString As String
- Public tCursorLocation As ADODB.CursorLocationEnum
- Private bPermanant As Boolean
-
- Private Function ExecString(strSQL As String) As ADODB.Recordset
- On Error GoTo erreur
-
- 'Connection & execution
- If Len(strConnexionString) > 0 Then
- If oConn Is Nothing Then
- If Not Connect Then
- Set ExecString = Nothing
- Exit Function
- End If
- End If
- Debug.Print "SQL: " & strSQL
- Set ExecString = oConn.Execute(strSQL)
- End If
-
- 'Deconnection
- If Not bPermanant Then
- Disconnect
- End If
-
- Exit Function
- erreur:
- If gError(Err, "ExecString") Then
- Resume Next
- End If
- End Function
-
- Public Function Connect() As Boolean
- On Error GoTo erreur
- Set oConn = New ADODB.Connection
-
- oConn.CursorLocation = tCursorLocation
- oConn.Open (strConnexionString)
- Connect = True
-
- Exit Function
- erreur:
- gError Err, "Connect()"
- End Function
-
- Private Function Disconnect() As Boolean
- On Error GoTo erreur
- If Not oConn Is Nothing Then
- oConn.Close
- Set oConn = Nothing
- End If
- Exit Function
- erreur:
- gError Err, "Disconnect()"
- End Function
-
- Public Property Let Permanant(value As Boolean)
- bPermanant = value
- If value Then
- tCursorLocation = adUseServer
- End If
- End Property
-
- Public Property Get Permanant() As Boolean
- Permanant = bPermanant
- End Property
-
- Public Function ExecSQL(strSQL As String) As ADODB.Recordset
- On Error GoTo erreur
-
- Set ExecSQL = ExecString(strSQL)
-
- Exit Function
- erreur:
- If gError(Err, "ExecSQL") Then
- Resume Next
- End If
- End Function
-
- Public Function fExecSQL(strSQL As String, ParamArray strArgument() As Variant) As ADODB.Recordset
- On Error GoTo erreur
-
- Const MagicString = "42!42!42!42!42!42!" ' "42!" * (4+2) = leet code !!!
-
- Dim i As Long
- Dim strTmp As Variant
-
- 'Creation de la chaine de commande
- For Each strTmp In strArgument
- i = i + 1
- If IsNull(strTmp) Then strTmp = "NULL"
- strSQL = Replace(strSQL, "$" & CStr(i) & "$", AddSlashes(Replace(CStr(strTmp), "$", MagicString, , , vbTextCompare)), , , vbTextCompare)
- Next strTmp
- strSQL = Replace(strSQL, MagicString, "$", , , vbTextCompare)
-
- 'Execution de la chaine SQL
- Set fExecSQL = ExecString(strSQL)
-
- Exit Function
- erreur:
- If gError(Err, "fExecSQL") Then
- Resume Next
- End If
- End Function
-
- Private Function gError(oErr As ErrObject, strFunction As String) As Boolean
- gError = False
-
- Select Case strFunction
- Case Else
- If Not bSilent Then
- If vbYes = MsgBox("Une erreur est survenue !" & vbCrLf & "Voulez vous continuer ?" & String(2, vbCrLf) & CStr(Err.Number) & " : " & Err.Description, vbYesNo + vbExclamation, "Erreur contrôlé") Then
- gError = True
- End If
- End If
- End Select
- End Function
-
- Private Sub Class_Initialize()
- 'Initialisation de la class
- tCursorLocation = adUseClient
- bSilent = False
- bPermanant = False
- End Sub
-
- Private Sub Class_Terminate()
- Call Disconnect
- End Sub
-
'Développé par ZAPPY zappy@gosney42.org
'Pour toute utilisation commercial, contactez l'auteur
'En cas de redistribution, vous êtes tenu de livrer tel quel, avec le copyright
Option Explicit
Public oConn As ADODB.Connection
Public bSilent As Boolean
Public strConnexionString As String
Public tCursorLocation As ADODB.CursorLocationEnum
Private bPermanant As Boolean
Private Function ExecString(strSQL As String) As ADODB.Recordset
On Error GoTo erreur
'Connection & execution
If Len(strConnexionString) > 0 Then
If oConn Is Nothing Then
If Not Connect Then
Set ExecString = Nothing
Exit Function
End If
End If
Debug.Print "SQL: " & strSQL
Set ExecString = oConn.Execute(strSQL)
End If
'Deconnection
If Not bPermanant Then
Disconnect
End If
Exit Function
erreur:
If gError(Err, "ExecString") Then
Resume Next
End If
End Function
Public Function Connect() As Boolean
On Error GoTo erreur
Set oConn = New ADODB.Connection
oConn.CursorLocation = tCursorLocation
oConn.Open (strConnexionString)
Connect = True
Exit Function
erreur:
gError Err, "Connect()"
End Function
Private Function Disconnect() As Boolean
On Error GoTo erreur
If Not oConn Is Nothing Then
oConn.Close
Set oConn = Nothing
End If
Exit Function
erreur:
gError Err, "Disconnect()"
End Function
Public Property Let Permanant(value As Boolean)
bPermanant = value
If value Then
tCursorLocation = adUseServer
End If
End Property
Public Property Get Permanant() As Boolean
Permanant = bPermanant
End Property
Public Function ExecSQL(strSQL As String) As ADODB.Recordset
On Error GoTo erreur
Set ExecSQL = ExecString(strSQL)
Exit Function
erreur:
If gError(Err, "ExecSQL") Then
Resume Next
End If
End Function
Public Function fExecSQL(strSQL As String, ParamArray strArgument() As Variant) As ADODB.Recordset
On Error GoTo erreur
Const MagicString = "42!42!42!42!42!42!" ' "42!" * (4+2) = leet code !!!
Dim i As Long
Dim strTmp As Variant
'Creation de la chaine de commande
For Each strTmp In strArgument
i = i + 1
If IsNull(strTmp) Then strTmp = "NULL"
strSQL = Replace(strSQL, "$" & CStr(i) & "$", AddSlashes(Replace(CStr(strTmp), "$", MagicString, , , vbTextCompare)), , , vbTextCompare)
Next strTmp
strSQL = Replace(strSQL, MagicString, "$", , , vbTextCompare)
'Execution de la chaine SQL
Set fExecSQL = ExecString(strSQL)
Exit Function
erreur:
If gError(Err, "fExecSQL") Then
Resume Next
End If
End Function
Private Function gError(oErr As ErrObject, strFunction As String) As Boolean
gError = False
Select Case strFunction
Case Else
If Not bSilent Then
If vbYes = MsgBox("Une erreur est survenue !" & vbCrLf & "Voulez vous continuer ?" & String(2, vbCrLf) & CStr(Err.Number) & " : " & Err.Description, vbYesNo + vbExclamation, "Erreur contrôlé") Then
gError = True
End If
End If
End Select
End Function
Private Sub Class_Initialize()
'Initialisation de la class
tCursorLocation = adUseClient
bSilent = False
bPermanant = False
End Sub
Private Sub Class_Terminate()
Call Disconnect
End Sub
Conclusion
Exemple d'utilisation : Dim rec As Recordset, myConn as clsConnexion Set myConn = new clsConnexion myConn Set rec = myConn.ExecSQL(GetSQLCommand) myConn.Permanant = True 'Ne doit pas deco apres la premiere requete myConn.strConnexionString = GetConnexionString 'Parametrage de la connexion myConn.Connect set rec = myConn.fExecSQL("select * from db_clients where s_name='$1$' and i_age > $2$", txtName.text, txtAge.text) 'Et voila, y'a pas plus simple. Juste un exemple de GetConnexion() : Private Function GetConnexionString() As String Dim ConnexionString As String ConnexionString = "DRIVER={MySQL ODBC 3.51 Driver};" & _ "SERVER=myhost;" & _ "DATABASE=mydb;" & _ "USER=myuser;" & _ "OPTION=96;" & _ "PASSWORD=mypassword;" GetConnexionString = ConnexionString End Function
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Comparez les prix Nouvelle version
|