I am trying to write a class that uses ADO to retrieve SQL records. The intent is for the class to maintain one private recordset an other methods move forward, retrieve fields, etc. This is a basic example of my class:
class SqlADO {
private:
ADODB::_RecordsetPtr _recordset;
public:
void Open(); //open the connection
void Execute(const char* sql); // creates or replaces current recordset
void Next(); //moves recordset cursor forward
void Field(const char* fieldName); //retrieves field name from current record of the recordset
};
void SqlConnection::Execute(const char* sql) {
if(_recordset == NULL) { /* is this ok? */
HRESULT hr;
hr = _recordset.CreateInstance(__uuidof(ADODB::Recordset));
if(FAILED(hr)){
throw _com_error(hr);
}
}
if(_recordset->State != ADODB::adStateClosed) {
_recordset->Close();
}
try{
_recordset->Open(sql.c_str(),
_connection.GetInterfacePtr(),
ADODB::adOpenForwardOnly,
ADODB::adLockReadOnly,
ADODB::adCmdText);
} catch(_com_error &e) {
throw;
}
}
My Questions:
- In the Execute method, how can I check to see that the recordset instance has been created (or do I need to) so that I can close it first?
- Do you know of any good ADO COM Interop references?