VC ++ ADO Development Practice

zhaozj2021-02-16  52

How to use the UDL file in Visual C to create an ADO connection Using a general data connection file (* .UDL, the following referms) to create the ADO connection, you can define the data source to connect as the ODBC, thereby implementing the transparent data access Sex.

1. Use the UDL file to create an ADO connection to create an ADO connection, first set the Connectionstring property of the ADO connection object, which provides the database type to which you want to connect, the data locates the server, the secure authentication information to access by the database, and database access. The comparative professional method is to provide the above information directly in Connectionstring. The following is the standard for accessing different types of data source settings: 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 = databaseName; User ID = userName; Password = userPassword; "access aCCESS Database "provider = microsoft.jet.oledb.4.0; data source = database; user ID = username; password = userpassword;" The above connection attribute setting criteria varies with the type of data source, software users are often not used to this Setting mode, you want to have visualized data source setting methods. 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); Let's use unified method programming in software, no matter how changes in the data source. 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................. ....................................................... .. If there is no exception, as long as you are using M_PDBCONN, you can reference the Close method. 2. Creating the UDL file you want in the directory you want to create, click Right-click, select New | Microsoft Data Connection from the menu, then change the newly created UDL file to your desired file name (.udl The extension cannot be changed).

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

New Post(0)