begin process at 2012 02 11 23:19:57
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Base de Donnees

 > CLASS CONNEXION ADODB SECURISE

CLASS CONNEXION ADODB SECURISE


 Information sur la source

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

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
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 856

Auteur : zappy

Ecrire un message privé
Site perso
Ce membre participe au partage de revenus publicitaires
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


 Sources du même auteur

Source avec Zip EASY ARRAY, LES TABLEAUX DYNAMIQUES FACILE !
Source avec Zip CRACK DE "PROTECTION INVIOLABLE PAR MOT DE PASSE"
Source .NET (Dotnet) CLASS TCP/IP LISTENER TRES SIMPLE ET MULTITHREAD DOTNET
Source avec Zip MAGIQUE CAPTURE (CAPTURE D'ÉCRAN ET DE SOUS FENÊTRES)
TRAITER UNE LIGNE CSV

 Sources de la même categorie

Source avec Zip Source avec une capture BIEN ADMINISTRER LES ETUDIANTS ET LEURS CÔTES par okosa
Source avec Zip VBA EXEL GESTION DE PERSONEL NOUVEAU CONTRAT DE TRAVAI par oudlarbi
Source avec Zip Source avec une capture CREATION D'UN OBJET D'ACCÈS AUX DONNÉES par okosa
Source avec Zip Source .NET (Dotnet) MISAHORAIRE par MdelM
Source avec Zip Source avec une capture BASEDEDONNEES,GESTIONDEMALADES,DATABASSE par shadkitenge

Commentaires et avis

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'

Commentaire de skyghis le 20/08/2004 13:09:45

'clsConnexion' C sa classe.

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.

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

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
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

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,577 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales