Document directory
- Data Link file
- Use ODBC Data Source Name
- Use the connection string
In VB, there are three methods to connect to the ACCESS database:
- Use ADO object and write code to access the database
Connection object ODBC Data Source
- Use the ADO Data Control to quickly create a database connection
There are three Connection Methods: Data Link files Use ODBC Data Source names using connection strings
- Use the data environment designer to create a database connection
First, add the environment reference. Then find the data source to connect
No matter which type of connection, there is a path first, leading to the data source to be connected, establishing a relationship with the data source, and connecting. Just like from Beijing to Shanghai, you can make cars, trains, planes, high-speed cars, railway trains, and airplanes. The process is different, but the results are the same. They all reach the destination in Shanghai.
When operating these three connection methods, you will find repeated operations.
When the data control is connected, the ODBC data source is connected and the string is used to connect directly to the ODBC data source. The difference is that you can directly set the path using the ODBC data source and connect it with the connection object; the data control does not need a connection object and has a control. After setting the path, the data is directly connected.
The data environment designer and the ADO object use the connection. The difference is that in the data environment designer, the connection path is consistent with the connection steps of strings in the Data Control.
Different conclusions: You need to write code for the ADO object connection. You do not need to write code for others.
The following describes the detailed steps of each connection method:
1. Use the ADO connection object
First, create an ADO reference, define the connection class, and then instantiate the object.
The code is completed as follows:
Dim objcn as new connection, objrs as new recordset objcn. connectionstring = "provider = Microsoft. jet. oledb.3.51; "&" Data Source = "& App. path & "\ instance 01.mdb" objcn. open
2. Use the ODBC Data Source
First, open the ODBC data source on the computer and establish the relationship.
The procedure is as follows:
1) Open the ODBC data source on the computer
As shown in:
2) Click Add
3) Name
4) establish a relationship with the database Source
5) use code for connection
Dim objcn as connection set objcn = new connection objcn. Open "DSN = instance 2dsn"
3. Create a connection using the ADO Data Control.
1) Right-click the Data Control and select the adodc attribute.
2) select one of the three connection resources.
Use the data link file: Click Browse to find the. udl file containing the connection string.
Connect to the ODBC data source and click Create
Select the user database, and then perform the same operations as step 2.
Use the connection string: Click Generate
Test connection
4. Use the data environment designer to create a database connection
Add reference
Right-click Create connection, right-click the attribute to be connected, and the subsequent operations are the same as those in the Data Control Using strings.