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

Catégorie :Base de Donnees Niveau : Débutant Date de création : 16/09/2003 Date de mise à jour : 04/11/2003 19:09:05 Vu : 7 021

Note :
4 / 10 - par 1 personne
4,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (4)
Ajouter un commentaire et/ou une note


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
 

Commentaires et avis

signaler à un administrateur
Commentaire de nass932 le 21/09/2003 02:43:45

ne manque t'il pas quelque chose ?

myConn as clsConnexion

quel est le type de 'clsConnexion'

signaler à un administrateur
Commentaire de skyghis le 20/08/2004 13:09:45

'clsConnexion' C sa classe.

signaler à un administrateur
Commentaire de JoePatent le 06/10/2004 21:01:09

Ya quelqu'un pour qui ca fonctionne ?

J'ai besoin d'un exemple fonctionnel svp.

signaler à un administrateur
Commentaire de ptoussaint le 02/11/2004 23:21:09

Je trouve que c'est un peu compliqué pour remplacer les instructions standards. On fait la même chose en 5 lignes sans se poser de question. Mais ma foi, quand on aime on ne compte pas !

Ajouter un commentaire



Nos sponsors

Sondage...

CalendriCode

Juillet 2009
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
2728293031  

Consulter la suite du CalendriCode

Comparez les prix Nouvelle version

Photothèque Nouveau !



Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés
Temps d'éxécution de la page : 0,250 sec

Google Coop CodeS-SourceS Google Coop CodeS-SourceS


Certaines images présentes sur le site (notament certains avatars) sont issues des collections IconShock, donc si vous souhaitez utiliser ces icons vous devez les acheter, ne les copiez pas et ne utilisez pas dans vos sites et applications sans les avoir commandé.