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