|
Vckbase online help Journal No. 10 |
|
How to Use udl files in Visual C ++ to establish an ADO connection |
Wang weilei Qin Yi |
|
Use a universal data connection file (*. udl, hereinafter referred to as a file) to create an ADO connection. You can visually define the data source to be connected like ODBC to achieve data access transparency. |
1. Use the udl file to create an ADO connection |
To create an ADO connection, you must first set the connectionstring attribute of the ADO connection object. This attribute provides the security authentication information for the database type, the server where the data is located, the database to be accessed, and the database to be accessed. The more professional method is to directly provide the above information in connectionstring. The following is the standard for setting connectionstring for accessing different data sources: |
"Provider = msdasql; DSN = dsnname; uid = username; Pwd = userpassword ;" |
Accessing Oracle databases |
"Provider = msdaora; Data Source = servername; user id = username; Password = userpassword ;" |
"Provider = sqloledb; Data Source = servername; initial catalog = databasename; user id = username; Password = userpassword ;" |
Access the ACCESS database |
"Provider = Microsoft. Jet. oledb.4.0; Data Source = databasename; user id = username; Password = userpassword ;" |
The above connection Property setting standards change with the data source type. software users often do not like this setting method and want to have a visual data source setting method. For this reason, Microsoft provides a universal data connection file (. udl) to establish and test ADO connection properties. The ADO connection object can easily connect to the data source using the udl file. The following example uses my_data1.udl to create an ADO connection. |
_ Connectionptr m_pdbconn; |
M_pdbconn.createinstance (_ uuidof (connection )); |
M_pdbconn-> connectionstring = "file name = C:/mydir/my_data1.udl "; |
M_pdbconn-> open ("", null ); |
In this way, you can use a unified method for programming in the software regardless of how the data source changes. When the data source changes, you only need to double-click the corresponding udl file to visually set the data source without changing the software. |
Because ADO is a COM interface, you can add Exception Handling Code when opening an ADO connection for software reliability. |
M_pdbconn-> open ("", null ); |
} Catch (_ com_error & E ){ |
// Code for handling exceptions |
............................. |
Because _ connectionptr m_pdbconn is a smart pointer, the reference count should be automatically reduced to 0 after the smart pointer is set to null when handling abnormal code. |
If no exception occurs, you only need to reference the close method after using m_pdbconn. |
2. Create the udl file you need |
Right-click the directory where you want to create the udl file, choose create from menu | MICROSOFT data connection, and change the newly created udl file to your desired file name (. the udl extension cannot be changed ). |
Note: If the operating system is window 2000, first create a text file, and then change the extension of the text file to "udl ". |
Double-click the created udl file to visually set the data source. |
To use a udl file, you must first install Microsoft mdac in the system. Windows 98 version 2 and Windows 2000 automatically contain this component. You can download the latest version of this component from the Microsoft website. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1997-2000 vckbase. com All Rights Reserved.