In VB, the method of connecting Access database is mainly shown in the following three kinds of
- Use ADO objects. Access the database by writing code
COnnection ObjectODBC Data source
- use ado data control to create a database connection at high speed
There are three ways to connect data Link file using ODBC data source names Using connection strings
- To create a database connection by using the Data Environment designer
First, add the environment reference. Then find the data source to connect
Regardless of the connection, there is a path leading to the data source to connect to. Establish a relationship with the data source in the connection. It's like going from Beijing to Shanghai, being able to do a car, being able to do a train, and being able to do airplanes. The car goes fast. The train took the railroad. The flight route is different. But the result is the same. Are arriving at the Shanghai destination.
In the operation of these three connection methods, you will find that there are repeated operations.
In a data control connection, the connection to an ODBC data source and the connection to use a string are different from the direct use of an ODBC data source connection. After you have set up the path directly using an ODBC data source. The connection object is then connected, and the data control does not have a connection object, there are controls, and the path is set. is directly connected to it.
Both the Data Environment Designer and the ADO object use the connection connection. The difference is that, in the Data Environment Designer, the connection connection path is consistent with the connection steps of the string in the database control.
Summary: ADO object connections need to write code, others do not have to write code.
The specific steps for each of these connection methods are described below:
1, using the ADO Connection object
First, to establish an ADO reference, define the class connection, and then instantiate the object.
The code is complete such as the following:
Dim objCN as New Connection, objRS as new Recordset objcn.connectionstring = "provider=microsoft.jet.oledb.3.51;" &am P "Data source=" & App.Path & "\ instance 01.mdb" Objcn.open
2. Working with ODBC data sources
First of all. Open the ODBC data source on your computer. Build relationships.
Procedures such as the following:
1). Open an ODBC data source on your computer
For example, as seen in:
2) Click Join
3) naming
4) Establish relationships with database sources
5) Connect using code
Dim objcn As Connection Set objcn = New Connection objcn.open "dsn= instance 2DSN"
3,ado Data Control to create a connection
1) Right-click the Data control and select the ADODC property
2) Choose one of the three connection resources.
Using the data Link file: Click Browse directly to find the. udl file that includes the connection string to
Using an ODBC data source connection, click New
Select the user database. The next action is the same as the 2 step.
Use connection string: Click Build
Test connection
4. To Create a database connection by using the Data Environment designer
Add Reference
Right-click Create connection. After you right-click the property you want to connect to, the operation is consistent with the operation in the data control using the string.
Detailed explanation VB connection access several methods database