Dynamically create An ADO control in VB6 to access the database

Source: Internet
Author: User
Tags odbc connection

Using the ADO control can easily access the ODBC database in VB6, but it is troublesome to directly place the ADODS control to obtain the ADO data connection. We can create a public data module in the VB project, write the initialization, connection establishment, and connection close operations of the ADO control to the function. In this way, you can call the ADO connection in other modules of the project.
A complete ADO call is divided into the following steps:
Open the connection from ADO to the database and initialize the ado recordset.
You can write an SQL statement to execute the query and return the query result RECORDSET. Alternatively, you can write an SQL statement to execute the corresponding database operations.
Releases the RECORDSET and closes the database connection.
Note that each dynamically created ADO can only be called by one process at a time. To perform multi-table parallel operations, you may need to create multiple dynamic ADO instances in the public data module.
The following is the relevant code:
'-----------------------------------------------------------------
'The following code is saved in the project module named my. bas.
Public CONN As Adodb. Connection 'defines the ado connection object
Public RS As Adodb. Recordset 'defines the ado recordset object
'****************************
'Open the database connection
'****************************
Function ConnOpen ()
Dim ASTR As String
Set CONN = New Adodb. Connection
ASTR = GetDatabasePath 'mdb file database path
CONN. ConnectionString = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" & ASTR & "; Persist Security Info = False"
'The odbc connection in this example is a CONNECTION from JET4.0 directly to the MDB file. If you use an ODBC data source, you can use the following CONNECTION string:
'Provider = MSDASQL.1; Password = ""; Persist Security Info = True; Data Source = Data Source name; Initial Catalog = Data table library name

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.