Sunday, November 15, 2009

Connect to MySql with VB 6.0




before started make connection from vb 6.0 to mysql we must installed MYSQL-Connector-ODBC in your computer . you can find the mysql-odbc connector on mysql or you can download via http://reliez.net/download. after you installed you will find mysql driver on odbc data source. on this project will used DSN connection, and we must create dsn with the name cdcol. there another way for connection without dsn.

Code for connection with DSN like below;

Public Dbado As ADODB.Connection
Set Dbado = New ADODB.Connection
Dbado.Open "Dsn=cdcol"



for connection without DSN like below

Public Conn As New ADODB.Connection

private sub create_con
Dim ConnString As String

Dim ConnString As String
Dim db_name As String
Dim db_server As String
Dim db_port As String
Dim db_user As String
Dim db_pass As String


db_name = "databaseku" 'db name
db_server = "localhost" 'address your mysql server
db_port = "3306" 'default port is 3306
db_user = "root" 'user name
db_pass = "password" 'password for mysql

ConnString ="DRIVER={MySQL ODBC 3.51 Driver};SERVER=" & db_server & ";DATABASE=" & db_name &

";UID=" & db_user & ";PWD=" & db_pass & ";PORT=" & db_port & ";OPTION=3"

With Conn
.ConnectionString = ConnString
.Open
End With
Exit Sub

download sample project via this link

0 komentar: