?? adocore.cpp
字號:
// ADOCORE: Implementation functions for demonstrating ADO via
// Visual C++ and MFC OLE.
//
// 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 )
{
_Connection Conn1;
_Command Cmd1;
Parameters Params1;
_Parameter Param1;
_Recordset Rs1;
Fields Flds1;
CString strTmp = "";
COleException e;
// 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.CreateDispatch( "ADODB.Connection.1.5", &e );
Conn1.SetConnectionString( strAccessConnect );
Conn1.Open( strEmpty, strEmpty, strEmpty, -1 );
//-----------------------------------
// Open Parameterized Command Object
//-----------------------------------
List1.AddString( "\t...Parameterized Command Object" );
// Create Command Object (1.5 Version )
Cmd1.CreateDispatch ( "ADODB.Command.1.5", &e );
Cmd1.SetRefActiveConnection( (LPDISPATCH) Conn1 );
Cmd1.SetCommandText( strOpenAccessWithParam );
// Create Parameter Object (1.5 Version )
Param1.CreateDispatch( "ADODB.Parameter.1.5" );
Param1 = Cmd1.CreateParameter( "",
adInteger,
adParamInput,
-1,
COleVariant( (long) 5) );
Param1.SetValue( COleVariant( (long) 5 ) );
Params1 = Cmd1.GetParameters();
Params1.Append( Param1 );
// Open Recordset Object
Rs1 = Cmd1.Execute( &vtEmpty, &vtEmpty2, adCmdText );
//----------------------------------------
// Manipulate Recordset/Fields Collection
//----------------------------------------
List1.AddString( "\t...Forward-Only Recordset" );
List1.AddString( "Dumping contents of each record..." );
// Reset recordset (1.5 Version)
Rs1.Requery(-1);
// While not on EOF...
while ( Rs1.GetEof() == FALSE )
{
Field Fld1;
Field Fld2;
// Get Fields 1 & 2
Flds1 = Rs1.GetFields();
Fld1 = Flds1.GetItem( COleVariant((long) 0 ));
Fld2 = Flds1.GetItem( COleVariant((long) 1 ));
// Display value in each field
strTmp.Format( "\t %s | %s ", CrackStrVariant( Fld1.GetValue() ), CrackStrVariant( Fld2.GetValue() ) );
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 );
Flds1 = Rs1.GetFields();
// Iterate through columns
for ( int i=0; i < Flds1.GetCount(); i++ )
{
Field Fld1;
Flds1 = Rs1.GetFields();
Fld1 = Flds1.GetItem( COleVariant( (long) i ) );
// Display Field #
strTmp.Format("\tField #%d", i + 1 );
List1.AddString( strTmp );
// Display Actual Size
strTmp.Format( "\t\t...Actual Size = %ld", Fld1.GetActualSize );
List1.AddString( strTmp );
// Display Attributes
strTmp.Format( "\t\t...Attributes = %ld", Fld1.GetAttributes );
List1.AddString( strTmp );
// Display Defined Size
strTmp.Format( "\t\t...Defined Size = %ld", Fld1.GetDefinedSize );
List1.AddString( strTmp );
// Display Name
strTmp.Format( "\t\t...Name = %s", Fld1.GetName() );
List1.AddString( strTmp );
// Display Type
strTmp.Format( "\t\t...Type = %s", GetType( (int) Fld1.GetType() ) );
List1.AddString( strTmp );
// Display Value
if( Fld1.GetType() == adGUID )
{
List1.AddString( "\t\t...Value = <GUID>" );
}
else
{
strTmp.Format ( "\t\t...Value = %s", CrackStrVariant( Fld1.GetValue() ));
List1.AddString( strTmp );
}
}
// Successful Shutdown
List1.AddString( "*** Success! ***" );
}
// Catch Blocks
catch( CException *e )
{
MfcErrorEx( 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.m_lpDispatch != NULL ) LOGQ( Rs1.Close(); )
if( Conn1.m_lpDispatch != NULL ) LOGQ( Conn1.Close(); )
// Force Release of objects;
if( Flds1.m_lpDispatch != NULL ) LOGQ( Flds1.ReleaseDispatch(); )
if( Rs1.m_lpDispatch != NULL ) LOGQ( Rs1.ReleaseDispatch(); )
if( Param1.m_lpDispatch != NULL ) LOGQ( Param1.ReleaseDispatch(); )
if( Params1.m_lpDispatch != NULL ) LOGQ( Params1.ReleaseDispatch(); )
if( Cmd1.m_lpDispatch != NULL ) LOGQ( Cmd1.ReleaseDispatch(); )
if( Conn1.m_lpDispatch != NULL ) LOGQ( Conn1.ReleaseDispatch(); )
}
void ProviderProperties( CHorzListBox &List1 )
{
_Connection Conn1;
_Command Cmd1;
Parameters Params1;
_Parameter Param1;
_Recordset Rs1;
Fields Flds1;
Field Fld1;
CString strTmp = "";;
int i = 0;
COleVariant varTmp;
COleException e;
// Trap any error/exception
try
{
//-------------------
// Connection Object
//-------------------
// Warm & Fuzzy for user
List1.ResetContent();
// Create Connection Object (1.5 Version )
Conn1.CreateDispatch( "ADODB.Connection.1.5", &e );
Conn1.SetConnectionString( strAccessConnect );
Conn1.Open( strEmpty, strEmpty, strEmpty, -1 );
// Dump connection properties
List1.AddString( "*** *** *** *** *** Dumping contents of the Properties Collection for the Connection object *** *** *** *** ***" );
DumpProperty( List1, Conn1, (Properties) Conn1.GetProperties() );
//----------------
// Command Object
//----------------
// Create Command Object (1.5 Version )
Cmd1.CreateDispatch ( "ADODB.Command.1.5", &e );
Cmd1.SetRefActiveConnection( (LPDISPATCH) Conn1 );
// Dump Command Properties
List1.AddString( "*** *** *** *** *** Dumping contents of the Properties Collection for the Command object *** *** *** *** ***" );
DumpProperty( List1, Conn1, (Properties) Cmd1.GetProperties() );
//------------------
// Recordset Object
//------------------
// Create Recordset Object
Cmd1.SetCommandText( strOpenAccessWithParam );
// Create Parameter Object (1.5 Version )
Param1.CreateDispatch( "ADODB.Parameter.1.5" );
Param1 = Cmd1.CreateParameter( "",
adInteger,
adParamInput,
-1,
COleVariant( (long) 5) );
Param1.SetValue( COleVariant( (long) 5 ) );
Params1 = Cmd1.GetParameters();
Params1.Append( Param1 );
Rs1 = Cmd1.Execute( &vtEmpty, &vtEmpty2, adCmdUnknown );
// Dump Recordset Properties
List1.AddString( "*** *** *** *** *** Dumping contents of the Properties Collection for the Recordset object *** *** *** *** ***" );
DumpProperty( List1, Conn1, (Properties) Rs1.GetProperties() );
//--------------
// Field Object
//--------------
// Create Field Object (if necessary)
Flds1 = Rs1.GetFields();
Fld1 = Flds1.GetItem( COleVariant( (long) 0 ) );
// Dump Field Properties
List1.AddString( "*** *** *** *** *** Dumping contents of the Properties Collection for the Field object *** *** *** *** ***" );
DumpProperty( List1, Conn1, (Properties) Fld1.GetProperties() );
// Successful Shutdown
List1.AddString( "*** Success! ***" );
}
// Catch Blocks
catch( CException *e )
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -