Type: Translate Source: http://www.codeguru.com Time: 2004-09-27
Translator: Mrwang2000) Email: Yuanzhaowang@sohu.com or Yuanzhaoang@163.com
I recently made an online banking project in Niit.Bangalore. This project is almost written in VB, only a small part involves ATL components, just teach us to write distributed applications. One intermediate component I have written for ATL and ADO to query the backend (SQL Server), some code shows here. I assume that the reader understands (at least understand) ATL's COM programming and VB's ADO programming
What is ADO? ADO is an abbreviation for ActiveX Data Object. ADO uses OLEDB data support to provide an interface for object-oriented access data sources, which is a collection of DAO and RDO object model, which is a collection of DAO and RDO.
C writes the OLEDB program is easy. However, those languages that do not provide pointers and other C features (such as Visual Basic) are hard to implement OLDB.
This is the reason for the ADO true appearance. ADO is an advanced interface based on COM interface technology, so any application that supports COM can implement ADO.
ADO's characteristics
. Allow access to all data types. Provide free thread. Provide asynchronous queries. Provide client and server pointer. Provide separate recording set
ADO's structure In the ADO model, we will use three main object types. CONNECTION. COMMAND. Recordset
The Connection object is used to establish a connection with the data source, first, data source name, location, user ID, password, etc. are stored in the Connectionstring object, used to pass to the Connection object to connect the data source.
Command objects are used to perform SQL commands, query, and stored procedures
When a query is executed, it returns a set of result sets stored by the RecordSet object, and the data in the Recordset can be modified and updated into the database.
Use ADO
First, we will create an ATL DLL component. This component is made by a method, the method has an input parameter (the user ID of the project) and returns the corresponding Recordset result reference to the client client, and then the client displays data in a form.
To create a DLL, we use the ATL COM AppWizard to generate an application framework, name FindCust to the project, select the server type to Dynamic Link Library, and select Support MFC Library. Option
Insert a new ATL object (New ATL Object), select Simple Object Enter Search in the SIMT NAME text box of the ATL Object Wizard Properties window and click OK, add an object
In the class view (ClassView), right-click the interface name and add a method, which gives this method as SearchCust and input the following text into the Parameters text box:
[in] BSTR BSTRCUSTID, [OUT, RETVAL] _Recordset ** Ptr
Click the OK button to add this method.
Since the SearchCust method returns a reference to a RecordSet object, we need to import (import) ADO library: Open the stdafx.h file and add the following code: #import "c: / program files / compon files / system / ado / msado15.dll" Rename_namespace ("Adocust") Rename ("EOF", "Endoffile" Using Namespace Adocust;
This step is to let the VC compiler understand the ADO object defined in object type library msado15.dll
The rename_namespace function changes the name of a name space, the DLL file is imported into the name space. The rename option change the EOF keyword to endoffile because EOF has been defined to the standard header file.
The .idl file also contains the SearchCust method, change the reference to the Recordset object, in order to let the MIDL compiler also understand the ADO object, import type library in the ivportlib statement in the .IDL file (in IMPORTLIB "stdole2.tlb" Afterwards)
Importlib ("C: / Program Files / Common Files / System / ADO / Msado15.dll");
Move the definition of the interface to the back of the .IDL file, the back of the IMPORLIB statement, is also to let the MIDL compiler understand the ADO object
After completing, my interface block is defined as follows.
[Object, uuid (EB78D558-E071-4D25-80DD-41FD3519934E), dual, helpstring ( "ISearch Interface"), pointer_default (unique)] interface ISearch: IDispatch {[id (1), helpstring ( "method SearchCust")] HRESULT SearchCust ([In] BSTR RCUSTID, [OUT, RETVAL] _Recordset ** PTR);
Build an ATL component
Now we are ready to write the code of the SearchCust method to get the corresponding message, we need to do these things:. Initialize the COM library. Connect to the data source. Execute the SQL command. Returns the Recordset object. Anti-initialization COM library
Initialize the COM library Coinitialize (NULL);
In order to connect the data source, you must first declare an ID_CONNECTIONPTR Conptr (__ uuidof (connection)) of a CONNECTION object pointer.
Call the Open function to the connection to the data source
Conptr-> Open (_t ("provider = sqloledb.1; data source = sqlser; initial catalog = customer"), _t ("user1"), _t (""), adopenunSpecified;
The Open function is from four parameters. The first parameter is a connection string, containing the name of the provider and the name of the SQL Server to connect. The second parameter and the third parameter are used to establish a username and password. The fourth parameter is the type of pointer used _t macro ensures the compatibility of the Unicode of the string
In order to execute the SQL command, create a Command object pointer by passing a CLSID of a Command object.
_Commandptr CMD (__ uuidof (command)); Set the activeConnection property of the Command object to the pointer to the open Connection object
CMD-> ActiveConnection = Conptr;
Store the SQL statement to be executed to the CommandText property of the Command object
CMD-> commandtext = "
Create a RecordSet object and specify the Command object as a record source
_RecordSetPtr RST (__ uuidof (recordset)); RST-> Putrefsource (CMD);
Open RecordSet now, use the Open method, as follows: _VARIANT_V VNULL;
RST-> Open (Vnull, Vnull, AdoPENDYNAMIC, AdLockOptimistic, AdcmdText);
The OPEN method is made by the first and second parameters of five parameters and the name of the data source and their respective activity connections Since the data source has been specified by the ActiveConnection property in the Connection object, and is also specified in the Command object, One and the second parameter use NULL variant values. The third parameter indicates the type of pointer, the fourth parameter indicates the lock parameters, and the fifth parameter indicates how the database should evaluate the command when the command is sent.
The Recordset object pointer now created will reference the record set returned by the SQL statement. We should return this RECORDSET to the client, use the following code: RST-> queryinterface (__ uuidof (_Recordset), (void **) PTR);
The QueryInterface function uses the IID of the Recordset object and returns a reference to the recordset returned by the SQL statement. When the client calls the SearchCust method, this pointer returns to the client.
Anti-initialization COM library :: Couninitialize ();
Now build (build) components, this will register DLL to the registry
Create a client
Open VB and create a new standard EXE project is simple, here is not described in detail.