How to use the UDL file to establish an ADO connection

Source: Internet
Author: User
Tags exception handling odbc access database

Use universal data connection files (*. UDL, hereinafter referred to as files) to create an ADO connection that can visually define the data source to be connected as well as ODBC to achieve transparency in data access.

1. Use UDL file to create ADO connection

To create an ADO connection, you first set the ConnectionString property of the ADO Connection object, which provides the type of database to which you want to connect, the server where the data is, and the security authentication information for the database and database access you want to access. A more professional approach is to provide the above information directly in ConnectionString, following is a standard for accessing different types of data source settings connectionstring:

accessing ODBC data

"Provider=msdasql;dsn=dsnname; Uid=username; Pwd=userpassword; "

accessing Oracle Databases

"Provider=msdaora;data Source=servername; User Id=username; Password=userpassword; "

Accessing the MS SQL database

"Provider=sqloledb;data source=servername;initial catalog=databasename; User Id=username; Password=userpassword; "

Accessing the Access database

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=databasename; User Id=username; Password=userpassword; "

The above set standards for connection properties vary with the type of data source, and software users often do not get used to this setting, and they want to have a visual data source Setup method. For this purpose, Microsoft provides a common data connection file (. UDL) to establish and test the ADO connection properties. The ADO Connection object makes it easy to use a UDL file to connect to a data source, and 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);

This allows for a unified approach to programming in software regardless of how the data source changes. When the data source changes, simply double-click the appropriate UDL file to visually set up the data source without changing the software.

Because ADO is a COM interface, in order to software reliability, open ADO connection, you can add exception handling code.

try{
m_pDBConn->Open("","","",NULL);
}catch(_com_error &e){
//处理异常的代码
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
m_pDBConn=NULL;
}

Because _connectionptr M_pdbconn is a smart pointer, setting the smart pointer to null when handling exception code automatically drops the reference count to 0.

If you do not have an exception, simply refer to the Close method as long as you are finished using M_pdbconn.

2. Create the UDL file you need

Right-click in the directory where you want to create the UDL file, and choose New from the menu | Microsoft data connection, and then change the newly created UDL file to the file name (. udl extension cannot be changed) that you want.

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

Then double-click the UDL file you created to visually complete the data source settings.

The UDL file must first be installed in the system with the Microsoft Mdac,win 98 Second Edition, which is automatically included in Win 2000, and can be downloaded to the Microsoft Web site when the latest version of the component is required.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.