1. Use the MySQL database that comes with xamp
2. Download The mysql-connector-odbc-3.51.28-win32.msi and add the data source conn configuration as follows
3. Open the xamp control panel and run the Apache and MySQL services.
4. Type localhost to go to the Management page, add a sohard database, add a user table under the sohard database, and write a record in the table with the username Wangyang
5. Create a VB project and enter the following code:
Private sub commandementclick ()
'Define and create database connections and access objects
Dim cn as new ADODB. Connection
Dim RS as new ADODB. recordset
'Define the database connection string variable
Dim strcn as string
'Define database connection parameter variables
Dim db_host as string
Dim db_user as string
Dim db_pass as string
Dim db_data as string
Dim db_result as string
'Define SQL statement Variables
Dim SQL as string
'Initialize database connection Variables
Db_host = "127.0.0.1"
Db_user = "root"
Db_pass = ""
Db_data = "sohard"
Strcn = "driver = {MySQL ODBC 3.51 driver };"&_
"Server =" & db_host &";"&_
"Database =" & db_data &";"&_
"Uid =" & db_user & "; Pwd =" & db_pass &";"&_
"Option = 3; stmt = set names gb2312"
'Stmt = set names gb2312
'Specifies the encoding method of the database.
'Set the Chinese operating system to gb2312
'Then there will be no problem with Chinese.
'The version requires MySQL 4.1 +
'Connect to the database
CN. Open strcn
'Set this attribute to make the recordcount and absolutepage attributes available
CN. cursorlocation = aduseclient
'Access table users
SQL = "select uname from dh_user"
Rs. Open SQL, CN
Msgbox Rs. Fields (0)
End sub
Private sub command2_click ()
Unload me
End sub
Private sub form_load ()
Text1.text = "127.0.0.1"
Text2.text = "3306"
Text3.text = "root"
Text4.text = ""
Text5.text = "sohard"
End sub
6. The running result is as follows: