Me connecter à une BD mysql distante avec mysql_Connecter (IP fixe)

beegeezzz Messages postés 152 Date d'inscription mardi 4 novembre 2008 Statut Membre Dernière intervention 10 avril 2017 - 10 juin 2009 à 13:37
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 - 10 juin 2009 à 15:26
Bonjour tout le monde,


J'utilise MySQL_Connecter pour me connecter à une base de données mysql avec VB6.


En local, cela fonctionne parfaitement :

Set BD = MySQL_Connecter("localhost", "root", "mdp")

Mais ma base de données se trouve sur un serveur distant avec une adresse IP fixe du genre 123.456.789.123


Sauriez-vous me dire quel code je dois mettre pour pouvoir me connecter à ma base de données ?


Merci d'avance.


beegees

3 réponses

PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
10 juin 2009 à 14:00
salut,
sans nous dire ce qu'est ton objet DB ni la méthode MySQL_Connecter, on ne peut rien faire non

voir ce snippet :



<hr />
'    CONNEXION À UNE BASE DE DONNÉES MYSQL
'    http://www.codyx.org/snippet_connexion-base-donnees-mysql_108.aspx#1899
'    Posté par [ PCPT ] le 23/06/2008
<hr />




'  
COPIEZ LE CODE CI-DESSOUS DANS UN MODULE DE CLASS, VOUS AVEZ ACCES AUX OBJETS DB
ET RS


Option Explicit


' msado25.tlb (Microsoft ActiveX Data Objects 2.5
Library)


' msadox.dll (Microsoft ADO Ext. 2.7 for
DLL and Security)


Public 
DB 
As 
New ADODB.Connection


Public 
RS 
As 
New Recordset

    
'  
CONNEXION
Public Function DBConnect(ByVal sDBName As String, Optional ByVal sHost As String = "127.0.0.1", Optional ByVal sUser As String = "root", Optional ByVal sPassword As String = vbNullString) As
Boolean
    Me.DBClose
    With DB
        .ConnectionString = "DRIVER={MySQL ODBC
3.51 Driver};SERVER=" & sHost & ";DATABASE=" & sDBName & ";UID=" & sUser & ";PWD=" & sPassword & ";OPTION=" & 1 + 2 + 8 + 32 + 2048 + 16384 & ";"
        .CursorLocation = adUseClient
        .ConnectionTimeout = 1
        .CommandTimeout = 1
        On Error GoTo Err_Handler
        .Open
        DBConnect = True
        Exit Function
    End With
Err_Handler:
    Debug.Print "[DBConnect] " & Err.Number & " :
" & Err.Description
End Function
'   FERMETURE
DB
Public Sub DBClose()
    Me.DB.Cancel
    If Me.DBConnected Then Me.DB.Close
End Sub
'   BASE CONNECTéE ?
Public Function DBConnected() As Boolean
    DBConnected = Not (Me.DB.State = adStateClosed)
End Function
'  
REQUÊTE
Public Function RSExecute(ByVal sSql As String) As Boolean
    If Me.DBConnected Then
        Call RSClose
        Me.RS.CursorLocation = adUseClient
        On Local Error GoTo Err_Handler
        Me.RS.Open sSql, Me.DB, adOpenDynamic, adLockOptimistic,
-1
        RSExecute = True
    End If
    Exit Function
Err_Handler:
    Debug.Print "[RSExecute] " & Err.Number & " :
" & Err.Description
End Function
'   FERMETURE
RS
Private Sub RSClose()
    Me.RS.Cancel
    If Not (Me.RS.State = adStateClosed) Then
Me.RS.Close
End Sub
'   DESTRUCTION
CLASS
Private Sub Class_Terminate()
    Call RSClose:   Set Me.RS = Nothing
    Me.DBClose:     Set Me.DB = Nothing
End Sub
0
beegeezzz Messages postés 152 Date d'inscription mardi 4 novembre 2008 Statut Membre Dernière intervention 10 avril 2017 1
10 juin 2009 à 14:50
Salut,

Merci pour ta réponse.

En fait, j'avais mis :82 en trop.

J'ai maintenant un autre message d'erreur qui est le suivant :

Can't connect to MySQL server on IP ADRESSE (10060)

Il trouve donc le serveur mais ne peut pas s'y connecter.

Pour répondre à tes questions précédentes :

BD est une nouvelle connection ADODB : Private BD As ADODB.Connection

Voici le code de Mysql_Connecter :

Public Function MySQL_Connecter(ByVal Serveur As String, ByVal Utilisateur As String, ByVal MotDePasse As String) As ADODB.Connection
    Dim Connexion As New ADODB.Connection
   
    Set MySQL_Connecter = Nothing
    'On Error GoTo ErreurDeConnexion
    Connexion.Open "DRIVER={MySQL ODBC 5.1 Driver};" _
               & "SERVER=" & Serveur & ";" _
               & "UID=" & Utilisateur & ";" _
               & "PWD=" & MotDePasse
    'On Error GoTo 0
    Set MySQL_Connecter = Connexion
ErreurDeConnexion:
End Function

Merci d'avance pour l'aide.

beegees
0
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
10 juin 2009 à 15:26
ta chaîne OPEN n'est pas bonne
jette un oeil au snippet indiqué au dessus
0
Rejoignez-nous