📄 adocore.cpp
字号:
catch(...)
{
UnknownErrorEx( List1, Conn1 );
}
// Better check that HRESULT!
if( FAILED( hr ) )
{
List1.AddString("*** Failed HRESULT ***" );
strTmp = LogCrackHR( hr );
List1.AddString( strTmp );
// Dump Errors Collection, since if no exception was raised
// This is only way we can get at it!
if( Conn1 != NULL )
{
AdoErrorEx( 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(); )
// Force Release of objects;
if( Fld1 != NULL ) LOGQ( Fld1->Release(); )
if( Flds1 != NULL ) LOGQ( Flds1->Release(); )
if( Rs1 != NULL ) LOGQ( Rs1->Release(); )
if( Props1 != NULL ) LOGQ( Props1->Release(); )
if( Cmd1 != NULL ) LOGQ( Cmd1->Release(); )
if( Conn1 != NULL ) LOGQ( Conn1->Release(); )
}
void CodeTemplate( CHorzListBox &List1 )
{
// Using the helper routines, this shows what a typical
// code fragment using ADO would need in order to provide
// accurate error handling.
ADOConnection* Conn1 = NULL;
HRESULT hr = S_OK;
CString strTmp;
// Trap any error/exception
try
{
//----------------------------------
// (Fail To) Open Connection Object
//----------------------------------
// Warm & Fuzzy for user
List1.ResetContent();
List1.AddString( "ADO Code Template..." );
List1.AddString( "\t...Deliberately opening conneciton object with no connection information to generate an error" );
// Create Connection Object(1.5 Version)
hr = CoCreateInstance( CLSID_CADOConnection,
NULL,
CLSCTX_INPROC_SERVER,
IID_IADOConnection,
(LPVOID *) &Conn1 );
if( SUCCEEDED( hr ) ) hr = Conn1->Open( bstrEmpty, bstrEmpty, bstrEmpty, -1 );
//----------------------
// YOUR CODE GOES HERE!
//----------------------
// Successful Shutdown
}
// Catch Blocks
catch( CException *e )
{
MfcErrorEx( e, List1, Conn1 );
}
catch( SEH_Exception &e )
{
Win32ErrorEx( e, List1, Conn1 );
}
catch(...)
{
UnknownErrorEx( List1, Conn1 );
}
// Better check that HRESULT!
if( FAILED( hr ) )
{
List1.AddString("*** Failed HRESULT ***" );
strTmp = LogCrackHR( hr );
List1.AddString( strTmp );
// Dump Errors Collection, since if no exception was raised
// This is only way we can get at it!
if( Conn1 != NULL )
{
AdoErrorEx( List1, Conn1 );
}
}
//----------------------------------
// Miscellaneous (graceful) Cleanup
// with quiet error trapping
//----------------------------------
// Close any open objects
if( Conn1 != NULL ) LOGQ( Conn1->Close(); )
// Force Release of objects;
if( Conn1 != NULL ) LOGQ( Conn1->Release(); )
}
void InputOutputReturnParams( CHorzListBox &List1 )
{
ADOConnection* Conn1 = NULL;
ADOCommand* Cmd1 = NULL;
ADOParameters* Params1 = NULL;
ADOParameter* Param1 = NULL;
ADORecordset* Rs1Tmp = NULL;
ADOFields* Flds1 = NULL;
ADOField* Fld1 = NULL;
CString strTmp;
CString SQLServerName;
long nCount;
BSTR bstrSQLServerConnect = NULL;
VARIANT_BOOL bEOF = VARIANT_FALSE;
HRESULT hr = S_OK;
COleVariant v1;
// Trap any error/exception
try
{
//-------------------------------------
// Determine SQL Server to Connect too
//-------------------------------------
CInputBox dlg;
if( dlg.DoModal() == IDOK )
{
if( dlg.m_Path.GetLength() == 0 )
return;
CString strTmp;
strTmp.Format( "driver={sql server};"
"server=%s;"
"Database=pubs;UID=sa;PWD=;",
dlg.m_Path );
SQLServerName = dlg.m_Path;
bstrSQLServerConnect = strTmp.AllocSysString();
}
//------------------------
// Open Connection Object
//------------------------
// Warm & Fuzzy for user
List1.ResetContent();
List1.AddString( "Demonstrating Return, Input and Output parameters..." );
strTmp.Format( "\t...Assumes SQL Server named %s", SQLServerName );
List1.AddString( strTmp );
List1.AddString( "\t...With Error Handling Using Connection Object" );
List1.AddString( "\t...uses stored procedure sp_AdoTest" );
// Create Connection Object (1.5 Version )
hr = CoCreateInstance( CLSID_CADOConnection,
NULL,
CLSCTX_INPROC_SERVER,
IID_IADOConnection,
(LPVOID *) &Conn1 );
if( SUCCEEDED( hr ) ) hr = Conn1->put_ConnectionString( bstrSQLServerConnect );
if( SUCCEEDED( hr ) ) hr = Conn1->Open( bstrEmpty, bstrEmpty, bstrEmpty, -1 );
//--------------------------
// Create Stored Procedures
//--------------------------
if( SUCCEEDED( hr ) ) List1.AddString( "Creating Stored Procedure..." );
// Drop Procedure
if( SUCCEEDED( hr ) ) hr = Conn1->Execute( bstrSQLDrop, &vtEmpty, adCmdText, &Rs1Tmp );
if( Rs1Tmp != NULL ) // Paranoia!
{
Rs1Tmp->Release();
Rs1Tmp = NULL;
}
// Create Procedure
if( SUCCEEDED( hr ) ) hr = Conn1->Execute( bstrSQLCreate, &vtEmpty, adCmdText, &Rs1Tmp );
if( Rs1Tmp != NULL ) // Paranoia!
{
Rs1Tmp->Release();
Rs1Tmp = NULL;
}
//-----------------------------------
// Open Parameterized Command Object
//-----------------------------------
if( SUCCEEDED( hr ) ) List1.AddString( "Opening a ForwardOnly Recordset from a Parameterized Command Object..." );
if( SUCCEEDED( hr ) ) List1.AddString( "...creating command object" );
// Create Command Object
if( SUCCEEDED( hr ) ) hr = CoCreateInstance( CLSID_CADOCommand,
NULL,
CLSCTX_INPROC_SERVER,
IID_IADOCommand,
(LPVOID *) &Cmd1 );
if( SUCCEEDED( hr ) ) hr = Cmd1->putref_ActiveConnection( Conn1 );
if( SUCCEEDED( hr ) ) hr = Cmd1->put_CommandText( bstrStoredProc );
if( SUCCEEDED( hr ) ) hr = Cmd1->put_CommandType( adCmdStoredProc );
// Fill Parameters Collection
List1.AddString( "...refreshing parameters collection off stored procedure" );
if( SUCCEEDED( hr ) ) hr = Cmd1->get_Parameters( &Params1 );
if( SUCCEEDED( hr ) ) hr = Params1->Refresh();
if( SUCCEEDED( hr ) ) hr = Params1->get_Item( COleVariant((long) 1 ), &Param1 );
if( SUCCEEDED( hr ) ) hr = Param1->put_Value( COleVariant((long) 10 ) );
// Limit scope of Recordset object (so later we can easily fetch ret/out params)
List1.AddString( "...opening Recordset" );
{
ADORecordset* Rs1 = NULL;
// Open Recordset
if( SUCCEEDED( hr ) ) hr = Cmd1->Execute( &vtEmpty, &vtEmpty2, adCmdStoredProc, &Rs1 );
// Dump the recordset
if( SUCCEEDED( hr ) ) List1.AddString( "...Dumping Recordset" );
if( SUCCEEDED ( hr ) ) hr = Rs1->get_Fields( & Flds1 );
if( SUCCEEDED ( hr ) ) hr = Flds1->get_Count( &nCount );
if( SUCCEEDED( hr ) ) hr = Rs1->get_EOF( &bEOF );
while ( ( SUCCEEDED( hr ) ) && ( bEOF == VARIANT_FALSE ) )
{
CString s;
strTmp = "\t";
// Refresh Fields Collection
if( SUCCEEDED( hr ) ) hr = Flds1->Refresh();
// Dump Fields Collection
for( long l = 0; ( SUCCEEDED( hr ) && (l < nCount)); l++ )
{
if( SUCCEEDED ( hr ) ) hr = Flds1->get_Item( COleVariant( (long) l ), &Fld1 );
if( SUCCEEDED ( hr ) ) hr = Fld1->get_Value( &v1 );
if( SUCCEEDED ( hr ) )
{
Fld1->Release();
Fld1 = NULL;
}
if( l != ( nCount - 1 ) )
s.Format( "%s | ", CrackStrVariant( v1 ) );
else
s.Format( "%s", CrackStrVariant( v1 ) );
strTmp += s;
}
// Add record
if( SUCCEEDED( hr ) ) List1.AddString( strTmp );
// MoveNext
if( SUCCEEDED( hr ) ) hr = Rs1->MoveNext();
if( SUCCEEDED( hr ) ) hr = Rs1->get_EOF( &bEOF );
}
if( SUCCEEDED( hr ) ) Rs1->Close();
// Not necessary for return parametrs
// if( SUCCEEDED( hr ) ) Rs1->Release();
// if( SUCCEEDED( hr ) ) Rs1 = NULL;
}
// Display Parameters Collection (with caveat for user)
if( SUCCEEDED( hr ) ) List1.AddString( "...It is strictly Driver/Provider dependent whether you have to close" );
if( SUCCEEDED( hr ) ) List1.AddString( "the recordset to retrieve output/return parameters. With the release of" );
if( SUCCEEDED( hr ) ) List1.AddString( "of the SQL Server ODBC Driver with ODBC 3.X, you have to close the recordset." );
if( SUCCEEDED( hr ) ) List1.AddString( "Previous versions of this driver did not have this requirement, which itself" );
if( SUCCEEDED( hr ) ) List1.AddString( "came about as part of a bug fix in previous versions of the driver." );
if( SUCCEEDED( hr ) ) hr = Cmd1->get_Parameters( &Params1);
// Get first parameter
if( SUCCEEDED( hr ) ) hr = Params1->get_Item( COleVariant( (long) 0 ), &Param1 );
if( SUCCEEDED( hr ) ) hr = Param1->get_Value( &v1 );
if( SUCCEEDED( hr ) ) strTmp.Format( "\tRetVal Param = %s", CrackStrVariant( v1 ) );
if( SUCCEEDED( hr ) ) List1.AddString( strTmp );
// Get second parameter
if( SUCCEEDED( hr ) ) hr = Params1->get_Item( COleVariant( (long) 1 ), &Param1 );
if( SUCCEEDED( hr ) ) hr = Param1->get_Value( &v1 );
if( SUCCEEDED( hr ) ) strTmp.Format( "\tRetVal Param = %s", CrackStrVariant( v1 ) );
if( SUCCEEDED( hr ) ) List1.AddString( strTmp );
// Get third parameter
if( SUCCEEDED( hr ) ) hr = Params1->get_Item( COleVariant( (long) 2 ), &Param1 );
if( SUCCEEDED( hr ) ) hr = Param1->get_Value( &v1 );
if( SUCCEEDED( hr ) ) strTmp.Format( "\tRetVal Param = %s", CrackStrVariant( v1 ) );
if( SUCCEEDED( hr ) ) List1.AddString( strTmp );
}
// Catch Blocks
catch( CException *e )
{
MfcErrorEx( e, List1, Conn1 );
}
catch( SEH_Exception &e )
{
Win32ErrorEx( e, List1, Conn1 );
}
catch(...)
{
UnknownErrorEx( List1, Conn1 );
}
// Better check that HRESULT!
if( SUCCEEDED( hr ) )
{
List1.AddString( "*** Success! ***" );
}
else
{
List1.AddString("*** Failed HRESULT ***" );
strTmp = LogCrackHR( hr );
List1.AddString( strTmp );
// Dump Errors Collection, since if no exception was raised
// This is only way we can get at it!
if( Conn1 != NULL )
{
AdoErrorEx( List1, Conn1 );
}
}
//----------------------------------
// Miscellaneous (graceful) Cleanup
// with quiet error trapping
//----------------------------------
// Close any open objects
if( Conn1 != NULL ) LOGQ( Conn1->Close(); )
// Force Release of objects;
if( Fld1 != NULL ) LOGQ( Fld1->Release(); )
if( Flds1 != NULL ) LOGQ( Flds1->Release(); )
if( Rs1Tmp != NULL ) LOGQ( Rs1Tmp->Release(); )
if( Param1 != NULL ) LOGQ( Param1->Release(); )
if( Params1 != NULL ) LOGQ( Params1->Release(); )
if( Cmd1 != NULL ) LOGQ( Cmd1->Release(); )
if( Conn1 != NULL ) LOGQ( Conn1->Release(); )
// Release BSTR
if( bstrSQLServerConnect != NULL ) LOGQ( SysFreeString( bstrSQLServerConnect ); )
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -