Descarga el ODBC connector
desde la página de descarga de MySQL
.
Busque la connectionstring
correcta sobre aquí
.
En su proyecto VB6, seleccione la referencia a Microsoft ActiveX Data Objects 2.8 Library
. Es posible que también tenga una biblioteca 6.0 si tiene Windows Vista o Windows 7. Si desea que su programa se ejecute también en clientes de Windows XP, es mejor que tenga la biblioteca 2.8. Si tiene Windows 7 con SP 1, su programa nunca se ejecutará en ningún otro sistema con especificaciones más bajas debido a un error de compatibilidad en SP1. Puede leer más sobre este error en KB2517589
.
Este código debería brindarle suficiente información para comenzar con el conector 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