📄 adocore.cpp
字号:
// ADOCORE: Implementation functions for demonstrating ADO via
// Visual C++ and #import.
//
// Non-ADO Native and Native ADO Error Handling is embedded
// within each exception handling function. AdoErrorEx handles
// processing errors raised to the Ado Errors Collection
#include "stdafx.h"
#include "hlb.h"
#include "log.h"
#include "AdoUtils.h"
#include "VCADO.h"
#include "inputbox.h"
//-----------------------
// Dialog Event Handlers
//-----------------------
void OpenAccessDatabase( CHorzListBox &List1 )
{
ADODB::_ConnectionPtr Conn1;
ADODB::_CommandPtr Cmd1;
ADODB::ParametersPtr *Params1 = NULL; // Not an instance of a smart pointer.
ADODB::_ParameterPtr Param1;
ADODB::_RecordsetPtr Rs1;
CString strTmp = "";
// Trap any error/exception
try
{
//------------------------
// Open Connection Object
//------------------------
// Warm & Fuzzy for user
List1.ResetContent();
List1.AddString( "Opening Access Database ADODEMO.MDB..." );
List1.AddString( "\t...Assumes ADODEMO.MDB is in the same directory" );
List1.AddString( "\t...With Error Handling Using Connection Object" );
// Create Connection Object (1.5 Version)
Conn1.CreateInstance( __uuidof( ADODB::Connection ) );
Conn1->ConnectionString = bstrAccessConnect;
Conn1->Open( bstrEmpty, bstrEmpty, bstrEmpty, -1 );
//-----------------------------------
// Open Parameterized Command Object
//-----------------------------------
List1.AddString( "\t...Parameterized Command Object" );
// Create Command Object
Cmd1.CreateInstance( __uuidof( ADODB::Command ) );
Cmd1->ActiveConnection = Conn1;
Cmd1->CommandText = bstrOpenAccessWithParam;
// Create Parameter Object
Param1 = Cmd1->CreateParameter( _bstr_t(bstrEmpty),
ADODB::adInteger,
ADODB::adParamInput,
-1,
_variant_t( (long) 5) );
Param1->Value = _variant_t( (long) 5 );
Cmd1->Parameters->Append( Param1 );
// Open Recordset Object
Rs1 = Cmd1->Execute( &vtEmpty, &vtEmpty2, ADODB::adCmdText );
//----------------------------------------
// Manipulate Recordset/Fields Collection
//----------------------------------------
List1.AddString( "\t...Forward-Only Recordset" );
List1.AddString( "Dumping contents of each record..." );
// While not on EOF...
while ( Rs1->adoEOF == VARIANT_FALSE )
{
// Display value in each field
strTmp.Format( "\t %s | %s ",
CrackStrVariant( (tagVARIANT) Rs1->Fields->GetItem( _variant_t( 0L ) )->Value ),
CrackStrVariant( (tagVARIANT) Rs1->Fields->GetItem( _variant_t( 1L ) )->Value) );
List1.AddString( strTmp );
// Move to next record
Rs1->MoveNext();
}
// Warm & Fuzzy for user
List1.AddString( "Dumping properties of each column in the recordset..." );
// Reset recordset (1.5 Version)
Rs1->Requery( -1 );
// Iterate through columns
for ( int i=0; i < Rs1->Fields->Count; i++ )
{
_variant_t varTmp = (_variant_t)((long)i);
// Display Field #
strTmp.Format("\tField #%d", i + 1 );
List1.AddString( strTmp );
// Display Actual Size
strTmp.Format( "\t\t...Actual Size = %ld", Rs1->Fields->Item[varTmp]->ActualSize );
List1.AddString( strTmp );
// Display Attributes
strTmp.Format( "\t\t...Attributes = %ld", Rs1->Fields->Item[varTmp]->Attributes );
List1.AddString( strTmp );
// Display Defined Size
strTmp.Format( "\t\t...Defined Size = %ld", Rs1->Fields->Item[varTmp]->DefinedSize );
List1.AddString( strTmp );
// Display Name
strTmp.Format( "\t\t...Name = %s", (char *) Rs1->Fields->Item[varTmp]->Name );
List1.AddString( strTmp );
// Display Type
strTmp.Format( "\t\t...Type = %s", GetType( (int) Rs1->Fields->Item[varTmp]->Type ) );
List1.AddString( strTmp );
// Display Value
if( Rs1->Fields->Item[varTmp]->Type == ADODB::adGUID )
{
List1.AddString( "\t\t...Value = <GUID>" );
}
else
{
strTmp.Format ( "\t\t...Value = %s", (char *) (_bstr_t) Rs1->Fields->Item[varTmp]->Value );
List1.AddString( strTmp );
}
}
// Successful Shutdown
List1.AddString( "*** Success! ***" );
}
// Catch Blocks
catch( CException *e )
{
MfcErrorEx( e, List1, Conn1 );
}
catch( _com_error &e )
{
ImportErrorEx( e, List1, Conn1 );
}
catch( SEH_Exception &e )
{
Win32ErrorEx( e, List1, Conn1 );
}
catch(...)
{
UnknownErrorEx( List1, Conn1 );
}
//----------------------------------
// Miscellaneous (graceful) Cleanup
// with quiet error trapping
//----------------------------------
// Close any open objects
if( Rs1 != NULL ) LOGQ( Rs1->Close(); )
if( Conn1 != NULL ) LOGQ( Conn1->Close(); )
}
void ProviderProperties( CHorzListBox &List1 )
{
ADODB::_ConnectionPtr Conn1;
ADODB::_CommandPtr Cmd1;
ADODB::_RecordsetPtr Rs1;
CString strTmp = "";;
int i = 0;
_variant_t varTmp;
// Trap any error/exception
try
{
//-------------------
// Connection Object
//-------------------
// Warm & Fuzzy for user
List1.ResetContent();
// Create Connection Object (1.5 Version)
Conn1.CreateInstance( __uuidof( ADODB::Connection ) );
Conn1->ConnectionString = bstrAccessConnect;
Conn1->Open( bstrEmpty, bstrEmpty, bstrEmpty, -1 );
// Dump connection properties
List1.AddString( "*** *** *** *** *** Dumping contents of the Properties Collection for the Connection object *** *** *** *** ***" );
DumpProperty( List1, Conn1, Conn1->Properties );
//----------------
// Command Object
//----------------
// Create Command Object
Cmd1.CreateInstance( __uuidof( ADODB::Command ) );
Cmd1->ActiveConnection = Conn1;
// Dump Command Properties
List1.AddString( "*** *** *** *** *** Dumping contents of the Properties Collection for the Command object *** *** *** *** ***" );
DumpProperty( List1, Conn1, Cmd1->Properties );
//------------------
// Recordset Object
//------------------
// Create Recordset Object
Cmd1->CommandText = _bstr_t( L"SELECT * FROM Authors" );
Rs1 = Cmd1->Execute( &vtEmpty, &vtEmpty2, ADODB::adCmdText );
// Dump Recordset Properties
List1.AddString( "*** *** *** *** *** Dumping contents of the Properties Collection for the Recordset object *** *** *** *** ***" );
DumpProperty( List1, Conn1, Rs1->Properties );
//--------------
// Field Object
//--------------
// Create Field Object (if necessary)
// Dump Field Properties
List1.AddString( "*** *** *** *** *** Dumping contents of the Properties Collection for the Field object *** *** *** *** ***" );
DumpProperty( List1, Conn1, Rs1->Fields->Item[0L]->Properties );
// Successful Shutdown
List1.AddString( "*** Success! ***" );
}
// Catch Blocks
catch( CException *e )
{
MfcErrorEx( e, List1, Conn1 );
}
catch( _com_error &e )
{
ImportErrorEx( e, List1, Conn1 );
}
catch( SEH_Exception &e )
{
Win32ErrorEx( e, List1, Conn1 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -