In VB, the method of connecting Access database mainly has the following three kinds
- Use ADO objects to write code to access the database
COnnection ObjectODBC Data source
- To create a database connection at high speed using the ADO Data Control
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
No matter what kind of connection, there is a path to the data source to connect to, and the data source to establish the relationship between the connection. Just like from Beijing to Shanghai, can do the car, can do the train, but also can do the plane, the car go fast, the train to take the railway, the plane route, the process is not the same, but the result is the same, is to reach the Shanghai destination.
In the operation of these three connection methods, you will find that there are repeated operations.
In the data control connection, the connection of the ODBC data source and the connection to the use of the string are directly connected with the ODBC data source, but when the path is set up directly using an ODBC data source, the connection object is connected, and the data control is not connection object. There is a control, set the path, 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, using an ODBC data source
First, open the ODBC data source on your computer to establish a relationship.
Procedures such as the following:
1), open the 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, followed by the same procedure as in 2.
Use connection string: Click Build
Test connection
4. Create a database connection using the Data Environment designer
Add Reference
Right-click the Create connection, right-click the property you want to connect to, and then follow the action in the data control using the string.
Several methods of connecting Access database in VB