How to use the UDL file in Visual C ++ to create an ADO connection

zhaozj2021-02-11  216

VCKBASE ONLINE HELP JOURNAL NO.10

How to use the UDL file in Visual C to establish an ADO connection Wang Wei Li Qin Qin

Creating an ADO connection using a generic data connection file (* .UDL, hereinafter referred to as file), can define the data source to be connected like the ODBC, thereby implementing the transparency of data access.

1. Use the UDL file to create an ADO connection

Create an ADO connection, first set the Connectionstring property of the ADO connection object, which provides a database type, data locator to which you want to connect, the server, the secure authentication information to access the database, and database access. The comparison method is to provide the above information directly in Connectionstring. The following is the standard: CONNECTIONSTRING to access different types of data sources:

Access ODBC data

"Provider = msdasql; dsn = dsnname; uid = username; pwd = userpassword;"

Access Oracle Database

"Provider = msdaora; data source = servername; user ID = username; password = userpassword;"

Access MS SQL Database

"Provider = sqloledb; data source = servername; initial catalog = database; user ID = username; password = userpassword;"

Access Access Database

"Provider = microsoft.jet.OLEDB.4.0; Data Source = DatabaseName; user ID = username; password = userpassword;"

The connection attribute setting standard described above varies with the type of data source, and software users often do not usually have this setting mode, and they want to have visualized data source settings. To this end, Microsoft provides a universal data connection file (.UDL) to establish and test the 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.ud";

m_pdbconn-> open (",", ",", null);

In this way, regardless of how the data source changes, it can be programmed with a unified method in software. When the data source changes, simply double-click the corresponding UDL file to be visualized, and the software is required without changing the software.

Because ADO is a COM interface, an exception processing code can be added when the ADO connection is opened for software reliability.

Try {

m_pdbconn-> open (",", ",", null);

} catch (_COM_ERROR & E) {

// Treat an exception code

.........................

m_pdbconn = NULL;

}

Because _ConnectionPtr M_PDBCONN is a smart pointer, the reference count will be automatically set to 0 when the smart pointer is set to NULL when processing an exception code.

If there is no exception, as long as you are using M_PDBCONN, you can reference the Close method. 2. Create the UDL file you want

In the directory you want to create a UDL file, right-click, select New | Microsoft Data Connection from the menu, and change the newly created UDL file to your desired file name (.udl extension cannot be changed).

Note: If the operating system is Window 2000, create a text file first, change the extension of the text file to "UDL".

Then double-click the UDL file created, which is visually completed the settings of the data source.

Using the UDL file must be installed in the system first install Microsoft MDAC, WIN 98 Second Edition, and Win 2000 automatically contains this component, and you can go to the Microsoft website to download when the latest version is required.

© 1997-2000 vckbase.com All rights reserved.

转载请注明原文地址:https://www.9cbs.com/read-3874.html

New Post(0)