Connecting to the MySQL database using VB

Source: Internet
Author: User

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:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.