
Using ADO for SQL Server data access
STDMETHODIMP CSWSConfTool::IsCategoryValid( long nDoc_category,
VARIANT_BOOL * varB )
// *********************
// Inputs: nDoc_category is the page ID or document category to check for
validity.
// Outputs: varB is set to false if the nDoc_category document category
// is not present in a record of the table top_page_table.
// *********************
{
HRESULT hr = S_OK;
ado15::_ConnectionPtr adoConnection;
ado15::_CommandPtr adoCommand;
ado15::_ParameterPtr adoParameter;
ado15::ParametersPtr adoParameters;
ado15::_RecordsetPtr adoRecordset;
adoConnection = NULL;
adoCommand = NULL;
adoRecordset = NULL;
LPBYTE lpB = new BYTE[BUFFER_SIZE];
try
{
short nRet;
// Instantiate connection and recordset objects.
ATLTRACE(_T("Datasource: %s User id: %s password: %s\n"),
(LPTSTR)m_strDatasource, (LPTSTR)m_strDBUserID, (LPTSTR)m_strDBPassword);
if (
FAILED(hr = adoConnection.CreateInstance( __uuidof(ado15::Connection) ))
|| FAILED(hr = adoCommand.CreateInstance( __uuidof(ado15::Command) ))
|| FAILED(hr = adoRecordset.CreateInstance( __uuidof(ado15::Recordset)
))
|| FAILED(hr = adoConnection->Open( m_strDatasource, m_strDBUserID,
m_strDBPassword, -1 ))
) {
goto ErrorExit;
}
// Open a recordset on our new table.
_variant_t varCategory( (long)nDoc_category, VT_I4 );
nRet = QueryRegistryKey( _T("FIND_D_C"), lpB, BUFFER_SIZE );
if ( _IVSSWS_SUCCESS != nRet )
goto ErrorExit;
adoCommand->PutCommandText( (LPTSTR)lpB );
delete [] lpB;
lpB = NULL;
adoCommand->PutRefActiveConnection( adoConnection );
adoParameter = adoCommand->CreateParameter(
_bstr_t(_T("Param2"))
, ado15::adInteger
, ado15::adParamInput
, -1
, varCategory
);
if ( FAILED(adoCommand->get_Parameters(&adoParameters)))
goto ErrorExit;
// add the new CParameters into the ADO->Command->Parameters collection
if ( FAILED( adoParameters->Append( adoParameter)) )
goto ErrorExit;
VARIANT varValue = vtMissing;
adoRecordset = adoCommand->Execute( &varValue, &varValue, -1 );
if ( ! short(adoRecordset->Fields->Item[0L]->Value) )
goto ErrorExit;
} catch (_com_error & err) {
if ( lpB ) {
delete [] lpB;
lpB = NULL;
}
ATLTRACE(_T("%ld: %s\n"), err.Error(), err.ErrorMessage());
goto ErrorExit;
} catch (...) {
if ( lpB ) {
delete [] lpB;
lpB = NULL;
}
goto ErrorExit;
}
*varB = VARIANT_TRUE;
goto final_exit;
ErrorExit:
*varB = VARIANT_FALSE;
final_exit:
if ( adoRecordset ) {
adoRecordset->Close();
adoRecordset = NULL;
}
adoCommand = NULL;
if ( adoConnection ) {
adoConnection->Close();
adoConnection = NULL;
}
return hr;
Quote:
}