Microsoft KB Archive/200122

= SAMPLE: ATL2ADO.exe Returns Disconnected ADO Recordset =

Article ID: 200122

Article Last Modified on 3/14/2005

-

APPLIES TO


 * Microsoft ActiveX Data Objects 2.0
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



This article was previously published under Q200122



SUMMARY
ATL2ADO.exe is a sample that demonstrates a minimal three-tiered Database Application using ADO 2.0. It also demonstrates the following:
 * How to create a simple ATL COM object that returns a disconnected ADO recordset.
 * How to build the proxy/stub DLL that is used to marshal the recordset.



MORE INFORMATION
The following file is available for download from the Microsoft Download Center:

Atl2ado.exe

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

FileName                  Size - adostudent.mdb            72 readme.txt                491 helper.idl                21 R1ADOdrsExe.aps           4332 R1ADOdrsExe.cpp           4447 R1ADOdrsExe.dsp           12760 R1ADOdrsExe.dsw           545 R1ADOdrsExe.h             84 R1ADOdrsExe.idl           756 R1ADOdrsExe.rc            2941 R1ADOdrsExe.rgs           185 R1ADOdrsExeps.def         253 R1ADOdrsExeps.mk          502 r1DisRS.cpp               473 r1DisRS.h                 891 r1DisRS.rgs               645 resource.h                549 StdAfx.cpp                315 StdAfx.h                  1091

ATL2ADO.exe is a sample that demonstrates a minimal three-tiered Database Application using ADO 2.0. An ATL server in the middle tier returns a read/write disconnected recordset to a Visual Basic client for presentation. The Visual Basic client can change the data and send it to the server to be updated. The business logic is located in the COM server. The ATL COM server updates the Database tier.

In addition, ATL2ADO.exe demonstrates how to create a simple ATL COM object that returns a disconnected ADO recordset. The sample includes a Visual Basic client that uses the ADO _Recordset returned from the ATL COM Server. The Visual Basic client can change the record data and use the UpdateBat method provided by the server to save changes to the database. After extracting the files, build the Visual C++ project first to register the COM Objects and then use Visual Basic 6.0 to build the client program. The client and server can be distributed to different computers on the network.

The sample also shows how to build the proxy/stub DLL that is used to marshal the recordset. The sample includes an Access .mdb file that you can use with Access and SQL to build the student table that you can use with SQL Server. Use the following query with the GradYear parameter supplied by the Visual Basic client: "SELECT * FROM Student WHERE GradYear > ?" Build the Visual C++ project with Visual C++ 6.0 to create and register the server and the proxy/stub DLL. Build the Visual Basic project with Visual Basic version 6.0. You can build the server on one computer and the client on another computer on the same network. Copy the included Access database, ADOStudent.mdb, to C:\test (or change the connection string in the source file).

The sample server is created with the ATL COM AppWizard as an .exe server. A simple object ( r1DisRS ) is inserted. You need to make the following additions to the .idl file:  Import helper.idl. The file name "helper.idl" is arbitrary. "helper.idl" consists of one line: import msado15.idl. You must import ADO indirectly to successfully build the proxy/stub DLL. Otherwise, MIDL gives you compiler errors.  Add the following three methods to the interface: getRS([in,optional,defaultvalue(88)] short GY, [out, retval]_Recordset **ppRS); ConnString([in] BSTR newVal); UpdateBat( _Recordset *ppRS); 

You must also create a file helper.h with the following contents:

struct _Recordset; typedef struct _Recordset _Recordset; ADO is imported through the file stdAfx.h with the following two lines: From the Project Settings menu, click Post Build tab, add the "build & Reg Proxy/Stub DLL" as the description, and add the following two commands: nmake /f R2ADOmodps.mk regsvr32 R1ADOdrsExeps.dll This allows Visual C++ to automatically build and register the proxy/stub DLL. You must also specify Win32 Debug on the build bar.
 * 1) if !defined(__cplusplus) || defined(CINTERFACE)
 * 1) endif
 * 1) undef EOF                         // Necessary for EOF collision.
 * 2) import "msado15.dll" no_namespace // Correct place to import ADO.

The Visual Basic client sets the recordset property MarshalOptions to adMarshalModifiedOnly so that only the modified records are marshaled back to the server. Note that once you click commandSave, the recordset is trimmed to just the modified records. You must click CommandGetRS to retrieve the entire recordset back to the Visual Basic client.

You can build the Visual C++ project on Computer1 and build the Visual Basic client on Computer2. To specify the remote computer (Computer1 in the following example) use the following Visual Basic syntax: Set idrs = CreateObject("R1ADOdrsExe.r1DisRS", "Computer1")

Create Student Table on SQL Server
// Use the following SQL to create the student table on SQL Server. CREATE TABLE student (studentID int IDENTITY (1000, 1) NOT NULL,     name text NULL ,      gradYear int NULL ) GO  INSERT INTO student (name, gradYear) VALUES ('Billy Reubin', 99) go  INSERT INTO student (name, gradYear) VALUES ('Joe Mana', 98) go  INSERT INTO student (name, gradYear) VALUES ('Willy Klifton', 69) go

INSERT INTO student (name, gradYear) VALUES ('Lerry ElvisSon', 73) go

