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 comparisonal method is to provide the above information directly in Connectionstring. The following is the standard for accessing different types of data sources to set up CONNECTIONSTRING: Access ODBC Data
"Provider = MSDASQL; DSN = dsnName; UID = userName; PWD = userPassword;" access the ORACLE database "Provider = MSDAORA; Data Source = serverName; User ID = userName; Password = userPassword;" Access MS SQL database "Provider = SQLOLEDB; Data Source = serverName; Initial Catalog = databaseName; 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) {// processing 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.