2012-04-05 12 views
5

Mi piacerebbe trovare un semplice esempio di utilizzo della base MySQL remota. Lo so, ci sono alcuni tutorial su internet, che spiegano come configurare ADODB.Connection e connectionstrings, ma non riesco a farlo funzionare. Grazie per qualsiasi aiuto!Esempio di MySQL per Visual Basic 6.0 - lettura/scrittura

risposta

6

Scarica il ODBC connector dal MySQL download page.

Cerca il codice connectionstring su here.

Nel progetto VB6 selezionare il riferimento a Microsoft ActiveX Data Objects 2.8 Library. È possibile che tu abbia una libreria 6.0 anche se hai Windows Vista o Windows 7. Se vuoi che il tuo programma sia eseguito su client Windows XP, anche il meglio con la libreria 2.8. Se hai Windows 7 con SP 1, il tuo programma non funzionerà mai su nessun altro sistema con specifiche inferiori a causa di un errore di compatibilità in SP1. Puoi leggere ulteriori informazioni su questo bug in KB2517589.

Questo codice dovrebbe fornire informazioni sufficienti per iniziare con il connettore ODBC.

Private Sub RunQuery() 
    Dim DBCon As adodb.connection 
    Dim Cmd As adodb.Command 
    Dim Rs As adodb.recordset 
    Dim strName As String 

    'Create a connection to the database 
    Set DBCon = New adodb.connection 
    DBCon.CursorLocation = adUseClient 
    'This is a connectionstring to a local MySQL server 
    DBCon.Open "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDataBase; User=myUsername;Password=myPassword;Option=3;" 

    'Create a new command that will execute the query 
    Set Cmd = New adodb.Command 
    Cmd.ActiveConnection = DBCon 
    Cmd.CommandType = adCmdText 
    'This is your actual MySQL query 
    Cmd.CommandText = "SELECT Name from Customer WHERE ID = 1" 

    'Executes the query-command and puts the result into Rs (recordset) 
    Set Rs = Cmd.Execute 

    'Loop through the results of your recordset until there are no more records 
    Do While Not Rs.eof 
     'Put the value of field 'Name' into string variable 'Name' 
     strName = Rs("Name") 

     'Move to the next record in your resultset 
     Rs.MoveNext 
    Loop 

    'Close your database connection 
    DBCon.Close 

    'Delete all references 
    Set Rs = Nothing 
    Set Cmd = Nothing 
    Set DBCon = Nothing 
End Sub 
+0

Grazie, ma restituisce me "Non è possibile connettersi al server MySQL su ..." ogni volta che provo a connettermi ... Ho controllato il server, user e pass - tutto è corretto – f1nn

+0

btw, sicuramente ho usato la stringa di connessione per l'accesso REMOTO – f1nn

+0

Qual è il messaggio di errore completo? – Martin

Problemi correlati