[ Pobierz całość w formacie PDF ]
.The Command object enables more complex queriesand commands to be run against the data source.For instance, you could use a Command object to calla stored procedure with parameters in SQL Server.You will learn more about stored procedures onDay 6.A Command can create its own connection to the database or use a reference to an existingConnection object for greater efficiency.Techniques for Using ADO in C++ ApplicationsThere are a couple of ways to use ADO in your C++ code.You can use the ADO header files andimport library from the OLE DB SDK.You include the ADO header files (adoid.h and adoint.h) inyour source and add the ADO import library adoid.lib to your linker input.This enables you to createinstances of the ADO objects and access their member functions.Using this method, the code toconnect to a data source and create a Command object could look something like Listing 4.1.The codein Listing 4.1 doesn't check return values for the sake of code brevity.This is a code snippet only andwill not compile as shown.Listing 4.1.Using ADO via the OLE DB SDK1: ADOConnection* piConnection;2: ADOCommand* piCommand;3:4: CoCreateInstance(CLSID_CADOConnection, NULL,CLSCTX_INPROC_SERVER,IID_IADOConnection, (LPVOID *)&piConnection);5:6: CoCreateInstance(CLSID_CADOCommand, NULL,CLSCTX_INPROC_SERVER,IID_IADOCommand, (LPVOID*)&piCommand);7:8: piConnection->Open(L"MyDSN", L"sa", L"bodacious");9:10: piCommand->putref_ActiveConnection(piConnection);Lines 1 and 2 declare pointers to two ADO COM interfaces.Lines 4 and 6 callCoCreateInstance to create instances of the ADO interfaces and assign the pointers to them.(You will learn more about CoCreateInstance and COM interfaces on Day 9, "UnderstandingCOM.") Line 8 uses the Open function of the ADO Connection object to open a connection with anODBC data source called MyDSN.It uses a username of sa (system administrator) and a password ofbodacious.Line 10 calls the ADO Command object's putref_ActiveConnection functionto tell it to use the connection that was opened in line 8.Later in your program you will need to callthe Release function on piConnection and piCommand to free those objects.The other way to use ADO in a C++ application is to use the Visual C++ #import directive.Usingthe #import directive with the ADO library enables you to write less verbose code to accomplish thesame tasks with ADO.For instance, with #import, the preceding code can be abbreviated to thecode in Listing 4.2.This code doesn't check return values for the sake of code brevity.This is a codesnippet only and will not compile as shown.Listing 4.2.Using ADO via #import1: _ConnectionPtr pConnection;2: _CommandPtr pCommand;3:4: pConnection.CreateInstance(__uuidof( Connection ));5: pCommand.CreateInstance(__uuidof( Command ));6:7: pConnection->Open(L"MyDSN", L"sa", L"bodacious");8:9: pCommand->ActiveConnection = pConnection;In Listing 4.2, lines 1 and 2 define instances of two ADO smart pointer classes.Lines 4 and 5 call theCreateInstance function of those smart pointer classes to create instances of the ADO classes.Line 7 uses the Open function of the ADO Connection object to open a connection with an ODBCdata source called MyDSN.It uses a username of sa and a password of bodacious.Line 9 sets theADO Command object's ActiveConnection data member so that it uses the connection opened inline 7.Later in your program, you should not call the Release function on pConnection andpCommand to free those objects.pConnection and pCommand are smart pointers, so when theygo out of scope, Release will be automatically called.Also, using the #import directive meansthat you don't need to include the ADO header files (adoid.h and adoint.h) in your source, nor do youneed to link with the ADO import library adoid.lib.Article Q169496 in the Microsoft KnowledgeBase (KB) provides additional information on using the #import directive with ADO.You canobtain KB articles from the MSDN subscription CDs.Also, you can send email to the KB email serverat mshelp@microsoft.com.As you can see from the code listings, using the #import directive enables you to write code that isless verbose than OLE DB SDK code.Article Q174565 in the Microsoft Knowledge Base comparesthe processes of using ADO via the OLE DB SDK, via the #import directive, and via the OLE DBSDK, using the MFC OLE classes.That Knowledge Base article recommends using ADO via the#import directive.Based on my personal experience in writing ADO applications, I have found thatthe #import directive hides some of the complexity of using ADO.Therefore, the ADO examples inthis book use ADO via the #import directive.Article Q174565 in the Microsoft Knowledge Base compares the process of using ADO via the OLEDB SDK, via the #import directive, and via the OLE DB SDK using the MFC OLE classes.ThatKnowledge Base article also recommends using ADO via the #import directive.If you are interested in exploring the process of using ADO via the OLE DB SDK, there is a sampleapplication, ADOSDK, on the CD-ROM.It's an MFC application that gives you some idea of whatcode you need to write in order to use ADO via the OLE DB SDK.NOTEADO is a COM DLL.To call ADO functions from your C++ programs, theADO DLL must be registered, which means that the location of the ADODLL must be recorded in your machine's registry [ Pobierz całość w formacie PDF ]