VB6 connects to the MySQL database through ODBC

Source: Internet
Author: User
Tags odbc connection

Before trying to connect to the database, you need to briefly describe the two most common ways to connect to the database: one is to establish a DSN connection, which is troublesome and has poor program portability; I usually use ADODB to connect to the database. This method is flexible and convenient, and requires no additional work to facilitate program transplantation.

The following example shows how to connect to a MySQL database through ADODB:

First, run VB and create a standard EXE project. In the menu project-> reference dialog box that appears, find Microsoft ActiveX Data Objects X. X library, where X. X is the version number, and there may be many. Here I chose 2.5 ()

 

Click OK to close the dialog box, so that ADO (ActiveX Data Objects) is introduced in the project. Double-click form design form to open the code window, enter the following code in the form_load process (in the code comment ):
'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

'Define SQL statement Variables
Dim SQL as string

'Initialize database connection Variables
Db_host = "localhost"
Db_user = "yourusername"
Db_pass = "yourpassword"
Db_data = "yourdatabase"

'Mysql ODBC connection Parameters
'+ ------------ + --------------------- + ---------------------------------- +
'| Parameter | default value | description |
'+ ------------ + ------------------------------------------------------- +
'| User | ODBC (on Windows) | MySQL user name |
'| Server | localhost | MySQL server address |
'| Database | default connection to the database |
'| Option | 0 | the parameter is used to specify the connection mode. |
'| Port | 3306 | connection port |
'| Stmt | a declaration that can be run after the database is connected |
'| Password | MySQL user password |
'| Socket | (Omitted) |
'+ ------------ + --------------------- + ---------------------------------- +

'View official instructions in detail
'Http://dev.mysql.com/doc/refman/5.0/en/myodbc-configuration-connection-parameters.html

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 Table1
SQL = "select * From Table1"
Rs. Open SQL, CN
Msgbox Rs. recordcount

The rest is just like operating other databases. The biggest difference lies in some SQL statements.

Related Article

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.