Table Of ContentLearn OLE DB
Development with
Visual C++ 6.0
Nathan Wallace
Wordware Publishing, Inc.
LibraryofCongressCataloging-in-PublicationData
Wallace,Nathan
LearnOLEDBDevelopmentwithVisualC++6.0/byNathanWallace.
p. cm.
Includesindex.
ISBN 1-55622-634-9(pbk.)
1. MicrosoftVisualC++. 2. Databasemanagement. 3. OLE
(Computerfile) I. Title.
QA76.73.C153W314 1999
005.7—dc21 99-34012
CIP
©2000,WordwarePublishing,Inc.
AllRightsReserved
2320LosRiosBoulevard
Plano,Texas75074
Nopartofthisbookmaybereproducedinanyformorbyanymeans
withoutpermissioninwritingfromWordwarePublishing,Inc.
PrintedintheUnitedStatesofAmerica
ISBN 1-55622-634-9
10 9 8 7 6 5 4 3 2 1
0001
Productnamesmentionedareusedforidentificationpurposesonlyandmaybetrademarksoftheir
respectivecompanies.
AllinquiriesforvolumepurchasesofthisbookshouldbeaddressedtoWordwarePublishing,Inc.,
attheaboveaddress.Telephoneinquiriesmaybemadebycalling:
(972)423-0090
Dedication
To Beth Kohler. Thanks for all the help and patience.
Acknowledgments
Like so many complex undertakings, this book would not have existed with-
out support from many people beside the guy who tickled the keyboard. I’d
like to take a moment to thank Beth Kohler, who tried to make vague sense of
my frantic scribblings. Any leftover typos or illogic is entirely mine.
A big thank you to Russell Stultz, owner of Wordware Publishing, for his care
and thoughtfulness in a business all too often filled with the reverse.
And a final thanks to my wife, Laura, for her kindness, support, and patience
during the creative process.
Above all, I need to thank the Totoro, because without him, I wouldn’t be
here at all!
Contents
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiii
ChapterOne A COM Primer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
WhatisCOM? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
HowDidCOMHappen?. . . . . . . . . . . . . . . . . . . . . . . . . . . 2
WhyDoWeNeedCOM?. . . . . . . . . . . . . . . . . . . . . . . . . . . 5
HowCOMWorks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
TheCOMServer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
TheCOMClassFactory . . . . . . . . . . . . . . . . . . . . . . . . . . 8
TheCOMInterface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
TheCOMReferenceCount . . . . . . . . . . . . . . . . . . . . . . . . 9
TheCOMSysteminWindows . . . . . . . . . . . . . . . . . . . . . . 10
TheCOMClient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
COMisStatic,AutomationisDynamic. . . . . . . . . . . . . . . . . . . 10
AutomationOpensCOMtotheWorld . . . . . . . . . . . . . . . . . . . 12
FromAutomationtoOCXtoActiveX. . . . . . . . . . . . . . . . . . . . 13
ActiveXControlsareAutomationServerswithaUserInterface. . . . . . 16
StockPropertiesandPropertyPagesStandardizeActiveXControl
Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
ConnectionPointsAllowEventstobeSentfromActiveXControls
toContainers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
PersistenceAllowsActiveXControlstoSaveTheirStateOverTime. . . . 21
WhereWeGoFromHere. . . . . . . . . . . . . . . . . . . . . . . . . . 21
ChapterTwo An ATL Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
ATLinVisualC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
GettingATLforOlderVisualC++Versions . . . . . . . . . . . . . . . 24
ATL’sOnlineDocumentation . . . . . . . . . . . . . . . . . . . . . . . 24
CreatingATLProjectswiththeATLAppWizard . . . . . . . . . . . . . . 25
TheNewDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
TheATLAppWizard . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
TheConfirmationDialog. . . . . . . . . . . . . . . . . . . . . . . . . 26
AddingCOMInterfaceswiththeATLObjectWizard . . . . . . . . . . . 27
TheObjectWizardObjects. . . . . . . . . . . . . . . . . . . . . . . . 27
TheObjectWizardControls . . . . . . . . . . . . . . . . . . . . . . . 27
TheObjectWizardMiscellaneousElements . . . . . . . . . . . . . . . 28
TheObjectWizardDataAccessElements . . . . . . . . . . . . . . . . 28
TheObjectWizardNamesTab. . . . . . . . . . . . . . . . . . . . . . 28
TheObjectWizardAttributesTab . . . . . . . . . . . . . . . . . . . . 29
vi (cid:1) Contents
SpecializedObjectWizardTabs . . . . . . . . . . . . . . . . . . . . . 29
CreatingFunctionsandPropertieswiththeATLInterfaceWizards . . . . 30
TheClassViewShortcutMenu. . . . . . . . . . . . . . . . . . . . . . 30
TheAddMethodtoInterfaceDialog. . . . . . . . . . . . . . . . . . . 30
TheAddPropertytoInterfaceDialog . . . . . . . . . . . . . . . . . . 31
TheEditAttributesDialog . . . . . . . . . . . . . . . . . . . . . . . . 32
SomeAdvancedTopicsforATLProjects . . . . . . . . . . . . . . . . . . 32
TheProxyGenerator. . . . . . . . . . . . . . . . . . . . . . . . . . . 32
AdvancedServers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
ATLCode(TemplatesandMacros,OhMy!) . . . . . . . . . . . . . . . . 33
TemplatesCreateCustomClassesfromStandardC++Code . . . . . . 33
MacrosExpandintoCustomizedCode. . . . . . . . . . . . . . . . . . 33
What’sNewwithATLVersion3.0?. . . . . . . . . . . . . . . . . . . . . 34
Version3.0ChangestotheAppWizard . . . . . . . . . . . . . . . . . 34
Version3.0ChangestotheATLObjectWizard . . . . . . . . . . . . . 35
Version3.0ChangestotheClassViewContextMenus. . . . . . . . . . 36
TheAddWindowsMessageHandlerOption. . . . . . . . . . . . 36
TheImplementConnectionPointOption . . . . . . . . . . . . . 38
TheImplementInterfaceOption. . . . . . . . . . . . . . . . . . 39
WhereWeGoFromHere. . . . . . . . . . . . . . . . . . . . . . . . . . 40
ChapterThree An MFC Primer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
MFCinVisualC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
MFC’sOnlineDocumentation . . . . . . . . . . . . . . . . . . . . . . 41
CreatingMFCProjectswiththeMFCActiveXControlWizard . . . . . . . 42
TheNewDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
TheMFCActiveXControlWizard . . . . . . . . . . . . . . . . . . . . 43
TheConfirmationDialog. . . . . . . . . . . . . . . . . . . . . . . . . 44
WorkingMFCMagicwiththeClassWizard. . . . . . . . . . . . . . . . . 44
TheClassWizardMessageMapsTab. . . . . . . . . . . . . . . . . . . 45
TheClassWizardMemberVariablesTab . . . . . . . . . . . . . . . . . 45
TheClassWizardAutomationTab . . . . . . . . . . . . . . . . . . . . 46
TheClassWizardActiveXEventsTab. . . . . . . . . . . . . . . . . . . 46
TheClassWizardClassInfoTab . . . . . . . . . . . . . . . . . . . . . 47
AugmentingActiveXControlFeatureswithClassWizardDialogs . . . . . 47
TheClassWizardNewClassDialog . . . . . . . . . . . . . . . . . . . 47
TheClassWizardAddMethodDialog . . . . . . . . . . . . . . . . . . 48
TheClassWizardAddPropertyDialog. . . . . . . . . . . . . . . . . . 48
TheClassWizardAddEventDialog . . . . . . . . . . . . . . . . . . . 49
SomeAdvancedTopicsforMFCActiveXControlProjects . . . . . . . . . 49
UsingWindowsEventswithClassWizard . . . . . . . . . . . . . . . . 49
EditingPropertyPages. . . . . . . . . . . . . . . . . . . . . . . . . . 50
ConnectingPropertyPageControlstoActiveXControlProperties
withClassWizard . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
MFCCode(ClassesandMacros,OhMy!) . . . . . . . . . . . . . . . . . 52
MFCClassesEncapsulateMuchWindowsFunctionalityinC++
Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
MacrosExpandintoCustomizedCode. . . . . . . . . . . . . . . . . . 52
Contents (cid:1) vii
WhereWeGoFromHere. . . . . . . . . . . . . . . . . . . . . . . . . . 52
ChapterFour An OLE DB Primer. . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
IAccessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
IAccessor::AddRefAccessor. . . . . . . . . . . . . . . . . . . . . . . . 54
IAccessor::CreateAccessor . . . . . . . . . . . . . . . . . . . . . . . . 55
IAccessor::GetBindings. . . . . . . . . . . . . . . . . . . . . . . . . . 63
IAccessor::ReleaseAccessor . . . . . . . . . . . . . . . . . . . . . . . 65
IColumnsInfo. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
IColumnsInfo::GetColumnInfo. . . . . . . . . . . . . . . . . . . . . . 67
IColumnsInfo::MapColumnIDs. . . . . . . . . . . . . . . . . . . . . . 74
ISourcesRowset. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
ISourcesRowset::GetSourcesRowset . . . . . . . . . . . . . . . . . . . 75
IDBCreateSession. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
IDBCreateSession::CreateSession . . . . . . . . . . . . . . . . . . . . 79
IDBInfo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
IDBInfo::GetKeywords. . . . . . . . . . . . . . . . . . . . . . . . . . 80
IDBInfo::GetLiteralInfo . . . . . . . . . . . . . . . . . . . . . . . . . 84
IGetDataSource. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
IGetDataSource::GetDataSource. . . . . . . . . . . . . . . . . . . . . 90
IOpenRowset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
IOpenRowset::OpenRowset . . . . . . . . . . . . . . . . . . . . . . . 91
ISessionProperties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
ISessionProperties::GetProperties . . . . . . . . . . . . . . . . . . . . 95
ISessionProperties::SetProperties . . . . . . . . . . . . . . . . . . . . 97
IDBCreateCommand . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
IDBCreateCommand::CreateCommand . . . . . . . . . . . . . . . . . 99
ITableDefinition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
ITableDefinition::AddColumn . . . . . . . . . . . . . . . . . . . . . 100
ITableDefinition::CreateTable . . . . . . . . . . . . . . . . . . . . . 102
ITableDefinition::DropColumn . . . . . . . . . . . . . . . . . . . . . 107
ITableDefinition::DropTable . . . . . . . . . . . . . . . . . . . . . . 108
ICommand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
ICommand::Cancel. . . . . . . . . . . . . . . . . . . . . . . . . . . 109
ICommand::Execute . . . . . . . . . . . . . . . . . . . . . . . . . . 110
ICommand::GetDBSession . . . . . . . . . . . . . . . . . . . . . . . 116
ICommandProperties . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
ICommandProperties::GetProperties . . . . . . . . . . . . . . . . . . 118
ICommandProperties::SetProperties . . . . . . . . . . . . . . . . . . 121
ICommandText . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
ICommandText::GetCommandText. . . . . . . . . . . . . . . . . . . 124
ICommandText::SetCommandText . . . . . . . . . . . . . . . . . . . 125
IColumnsRowset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
IColumnsRowset::GetAvailableColumns . . . . . . . . . . . . . . . . 127
IColumnsRowset::GetColumnsRowset . . . . . . . . . . . . . . . . . 128
RequiredMetadataColumns . . . . . . . . . . . . . . . . . . . . . . 132
OptionalMetadataColumns . . . . . . . . . . . . . . . . . . . . . . 134
ICommandPrepare . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
viii (cid:1) Contents
ICommandPrepare::Prepare . . . . . . . . . . . . . . . . . . . . . . 138
ICommandPrepare::Unprepare. . . . . . . . . . . . . . . . . . . . . 140
ICommandWithParameters . . . . . . . . . . . . . . . . . . . . . . . . 140
ICommandWithParameters::GetParameterInfo. . . . . . . . . . . . . 141
ICommandWithParameters::MapParameterNames. . . . . . . . . . . 144
ICommandWithParameters::SetParameterInfo . . . . . . . . . . . . . 145
IRowset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
IRowset::AddRefRows . . . . . . . . . . . . . . . . . . . . . . . . . 153
IRowset::GetData. . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
IRowset::GetNextRows . . . . . . . . . . . . . . . . . . . . . . . . . 157
IRowset::ReleaseRows . . . . . . . . . . . . . . . . . . . . . . . . . 162
IRowset::RestartPosition . . . . . . . . . . . . . . . . . . . . . . . . 165
IRowsetInfo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
IRowsetInfo::GetProperties. . . . . . . . . . . . . . . . . . . . . . . 168
IRowsetInfo::GetReferencedRowset . . . . . . . . . . . . . . . . . . 171
IRowsetInfo::GetSpecification . . . . . . . . . . . . . . . . . . . . . 172
IRowsetChange . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
IRowsetChange::DeleteRows . . . . . . . . . . . . . . . . . . . . . . 174
IRowsetChange::InsertRow. . . . . . . . . . . . . . . . . . . . . . . 177
IRowsetChange::SetData . . . . . . . . . . . . . . . . . . . . . . . . 182
IRowsetUpdate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
IRowsetUpdate::GetOriginalData. . . . . . . . . . . . . . . . . . . . 188
IRowsetUpdate::GetPendingRows . . . . . . . . . . . . . . . . . . . 191
IRowsetUpdate::GetRowStatus. . . . . . . . . . . . . . . . . . . . . 193
IRowsetUpdate::Undo . . . . . . . . . . . . . . . . . . . . . . . . . 195
IRowsetUpdate::Update . . . . . . . . . . . . . . . . . . . . . . . . 199
ITransaction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
ITransaction::Abort. . . . . . . . . . . . . . . . . . . . . . . . . . . 204
ITransaction::Commit . . . . . . . . . . . . . . . . . . . . . . . . . 205
ITransaction::GetTransactionInfo. . . . . . . . . . . . . . . . . . . . 207
ITransactionOptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
ITransactionOptions::GetOptions. . . . . . . . . . . . . . . . . . . . 209
ITransactionOptions::SetOptions. . . . . . . . . . . . . . . . . . . . 209
ITransactionObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
ITransactionObject::GetTransactionObject . . . . . . . . . . . . . . . 211
ITransactionJoin. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
ITransactionJoin::GetOptionsObject . . . . . . . . . . . . . . . . . . 212
ITransactionJoin::JoinTransaction . . . . . . . . . . . . . . . . . . . 212
IRowsetIndex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
IRowsetIndex::GetIndexInfo . . . . . . . . . . . . . . . . . . . . . . 215
IRowsetIndex::Seek. . . . . . . . . . . . . . . . . . . . . . . . . . . 216
IRowsetIndex::SetRange . . . . . . . . . . . . . . . . . . . . . . . . 219
IErrorRecords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
IErrorRecords::AddErrorRecord . . . . . . . . . . . . . . . . . . . . 222
IErrorRecords::GetBasicErrorInfo. . . . . . . . . . . . . . . . . . . . 224
IErrorRecords::GetCustomErrorObject . . . . . . . . . . . . . . . . . 225
IErrorRecords::GetErrorInfo . . . . . . . . . . . . . . . . . . . . . . 225
IErrorRecords::GetErrorParameters. . . . . . . . . . . . . . . . . . . 226
Contents (cid:1) ix
IErrorRecords::GetRecordCount . . . . . . . . . . . . . . . . . . . . 227
IErrorLookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
IErrorLookup::GetErrorDescription. . . . . . . . . . . . . . . . . . . 229
IErrorLookup::GetHelpInfo. . . . . . . . . . . . . . . . . . . . . . . 230
IErrorLookup::ReleaseErrors . . . . . . . . . . . . . . . . . . . . . . 231
ISQLErrorInfo. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
ISQLErrorInfo::GetSQLInfo. . . . . . . . . . . . . . . . . . . . . . . 232
WhereWeGoFromHere . . . . . . . . . . . . . . . . . . . . . . . . . 233
ChapterFive An ADO Primer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
ADOCollections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
TheErrorsCollection. . . . . . . . . . . . . . . . . . . . . . . . . . 236
TheFieldsCollection . . . . . . . . . . . . . . . . . . . . . . . . . . 237
TheParametersCollection . . . . . . . . . . . . . . . . . . . . . . . 239
ThePropertiesCollection. . . . . . . . . . . . . . . . . . . . . . . . 241
TheADOConnectionObject . . . . . . . . . . . . . . . . . . . . . . 242
TheADOCommandObject. . . . . . . . . . . . . . . . . . . . . . . 259
TheADORecordsetObject . . . . . . . . . . . . . . . . . . . . . . . 264
TheADOFieldObject . . . . . . . . . . . . . . . . . . . . . . . . . 293
TheADOErrorObject . . . . . . . . . . . . . . . . . . . . . . . . . 299
TheADOPropertyObject. . . . . . . . . . . . . . . . . . . . . . . . 301
TheADOParameterObject. . . . . . . . . . . . . . . . . . . . . . . 304
ADOEvents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
BeginTransCompleteEvent. . . . . . . . . . . . . . . . . . . . . . . 308
CommitTransCompleteEvent. . . . . . . . . . . . . . . . . . . . . . 309
ConnectCompleteEvent . . . . . . . . . . . . . . . . . . . . . . . . 309
DisconnectEvent . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
EndOfRecordsetEvent . . . . . . . . . . . . . . . . . . . . . . . . . 310
ExecuteCompleteEvent . . . . . . . . . . . . . . . . . . . . . . . . 311
FetchCompleteEvent. . . . . . . . . . . . . . . . . . . . . . . . . . 312
FetchProgressEvent . . . . . . . . . . . . . . . . . . . . . . . . . . 313
FieldChangeCompleteEvent . . . . . . . . . . . . . . . . . . . . . . 313
InfoMessageEvent . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
MoveCompleteEvent. . . . . . . . . . . . . . . . . . . . . . . . . . 314
OnErrorEvent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
OnReadyStateChangeEvent . . . . . . . . . . . . . . . . . . . . . . 316
RecordChangeCompleteEvent . . . . . . . . . . . . . . . . . . . . . 316
RecordsetChangeCompleteEvent. . . . . . . . . . . . . . . . . . . . 317
RollbackTransCompleteEvent . . . . . . . . . . . . . . . . . . . . . 317
WillChangeFieldEvent . . . . . . . . . . . . . . . . . . . . . . . . . 318
WillChangeRecordEvent . . . . . . . . . . . . . . . . . . . . . . . . 319
WillChangeRecordsetEvent . . . . . . . . . . . . . . . . . . . . . . 319
WillConnectEvent . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
WillExecuteEvent . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
WillMoveEvent. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
WhereWeGoFromHere . . . . . . . . . . . . . . . . . . . . . . . . . 323
Description:Learn OLE DB Development with Visual C++ 6.0 presents the basics of the Component Object Model and OLE DB, and assists Visual C++ programmers in creating OLE DB providers and consumers. OLE DB is the ODBC-compliant standard from Microsoft that lets non-record-based data interact with its users via r