#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename ("EOF", "adoEOF")
#include <comdef.h>
#include <atlbase.h>
_ConnectionPtr m_pConnection;
_CommandPtr m_pCommand;
_RecordsetPtr m_pRecordset;
try
{
// AfxMessageBox("Befor open");
m_pConnection->ConnectionTimeout = 180;
// m_pConnection->ConnectionString = "DSN=ankur;UID=sbpfinder;PWD=projectfind;";
hr = m_pConnection->Open(L"ankur",L"sbpfinder",L"projectfind",NULL) ;
//hr = m_pConnection->Open(strCnn,"","",adConnectUnspecified);
if(!SUCCEEDED(hr))
return FALSE;
m_pCommand->ActiveConnection = m_pConnection;
hr = m_pConnection->Errors->Clear();
if(!SUCCEEDED(hr))
return FALSE;
m_pRecordset->Open ("permission",_variant_t((IDispatch *)m_pConnection,
true),adOpenStatic,adLockOptimistic, adCmdTable);
m_pConnection->Execute(strDeterminePermission,NULL,adExecuteNoRe cords);
m_pRecordset->Requery(adCmdUnknown);
while(!m_pRecordset->adoEOF)
{
_variant_t TheValue6;
_variant_t TheValue7;
_variant_t TheValue8;
_variant_t TheValue9;
_variant_t TheValue10;
TheValue6 = m_pRecordset->GetCollect("book_id");
TheValue7 = m_pRecordset->GetCollect("st_lesson_id");
TheValue8 = m_pRecordset->GetCollect("end_lesson_id");
TheValue9 = m_pRecordset->GetCollect("author_id");
TheValue10 = m_pRecordset->GetCollect("permission_level");
TheValue6.ChangeType(VT_BSTR);
TheValue7.ChangeType(VT_BSTR);
TheValue8.ChangeType(VT_BSTR);
TheValue9.ChangeType(VT_INT);
TheValue10.ChangeType(VT_INT);
if((TheValue9.intVal == AuthorID)
&& strcmp(OLE2T(TheValue6.bstrVal),((CAdminModuleApp *)AfxGetApp())->cmdInfo.m_strBookArg) == 0)
{
permissioninfo * newnode = new permissioninfo;
newnode->BookID = TheValue6.bstrVal;
newnode->stLessonID = TheValue7.bstrVal;
newnode->endLessonID = TheValue8.bstrVal;
newnode->Permission = TheValue10.intVal;
if(m_pTocInfo.GetCount() == 0)
m_pPermissionInfo.AddHead(newnode);
else
m_pPermissionInfo.AddTail(newnode);
TRACE("UNDER WHILE\n");
}
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
/*********************/
m_pRecordset->Open ("book",_variant_t((IDispatch *)m_pConnection,
true),adOpenStatic,adLockOptimistic, adCmdTable);
m_pConnection->Execute(strFindBookName,NULL,adExecuteNoRecords );
m_pRecordset->Requery(adCmdUnknown);
while(!m_pRecordset->adoEOF)
{
_variant_t BName;
_variant_t BDecs;
_variant_t BID;
BName = m_pRecordset->GetCollect("book_name");
BDecs = m_pRecordset->GetCollect("book_desc");
BID = m_pRecordset->GetCollect("book_id");
BID.ChangeType(VT_BSTR);
if(((CAdminModuleApp *)AfxGetApp())->cmdInfo.m_strBookArg == OLE2T(BID.bstrVal))
{
m_strBookName.Format("%s",(char*)(_bstr_t(BName))) ;
m_strBookDesc.Format("%s",(char*)(_bstr_t(BDecs))) ;
//---used by dev---//
_variant_t BId;
BId = m_pRecordset->GetCollect("book_id");
g_BookId.Format("%s",(char*)(_bstr_t(BId)));
g_BookDesc = m_strBookName;
//-----------------//
}
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
/************************************************** *************/
m_pRecordset->Open ("toc",_variant_t((IDispatch *)m_pConnection,
true),adOpenStatic,adLockOptimistic, adCmdTable);
m_pConnection->Execute(strSQLChange,NULL,adExecuteNoRecords);
m_pRecordset->Requery(adCmdUnknown);
int noofrec = 0;
while(!m_pRecordset->adoEOF)
{
++noofrec;
m_pRecordset->MoveNext();
}
m_pRecordset->Requery(adCmdUnknown);
noofrec = -1;
dlg.m_progress.StepIt();
while(!m_pRecordset->adoEOF)
{
_variant_t TheValue;
_variant_t TheValue1;
_variant_t TheValue2;
_variant_t TheValue3;
_variant_t TheValue4;
_variant_t TheValue5;
TheValue = m_pRecordset->GetCollect("book_id");
TheValue1 = m_pRecordset->GetCollect("lesson_id");
TheValue2 = m_pRecordset->GetCollect("sublesson_id");
TheValue3 = m_pRecordset->GetCollect("toc_id");
TheValue4 = m_pRecordset->GetCollect("toc_title");
TheValue5 = m_pRecordset->GetCollect("modified_date");
if(TheValue.vt!=VT_NULL || TheValue1.vt!=VT_NULL || TheValue2.vt!=VT_NULL)
{
CString ss;ss.Format("%04s%02s",OLE2T(TheValue.bstrVal),OL E2T(TheValue1.bstrVal));
int a = SearchForThePermissionOfALesson(ss);
if(a == -1)
{
if(!m_pRecordset->adoEOF)
m_pRecordset->MoveNext();
continue;
}
wsprintf(ssss,"%s%s%s%s",
(char*)_bstr_t(TheValue),
(char*)_bstr_t(TheValue1),
(char*)_bstr_t(TheValue2),
(char*)_bstr_t(TheValue3));
++noofrec;
nodeinfo * newnode = new nodeinfo;
TheValue.ChangeType(VT_INT);
newnode->BookID = TheValue.intVal;
TheValue1.ChangeType(VT_INT);
newnode->LessonID = TheValue1.intVal;
TheValue2.ChangeType(VT_INT);
newnode->SubLessonID = TheValue2.intVal;
TheValue3.ChangeType(VT_INT);
newnode->SubSubLessonID = TheValue3.intVal;
newnode->Text.Format("%s",(char*)(_bstr_t(TheValue4)));
newnode->Permission = a;
newnode->ID.Format("%s",ssss);
if(m_pTocInfo.GetCount() == 0)
m_pTocInfo.AddHead(newnode);
else
m_pTocInfo.AddTail(newnode);
}
if(!m_pRecordset->adoEOF)
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
wsprintf(ssss,"%d",noofrec);
m_iTocRecords = noofrec+1;
}
catch(_com_error &e)
{
char ss[256];
ErrorPtr pErr = NULL;
if( (m_pConnection->Errors->Count) > 0)
{
long nCount = m_pConnection->Errors->Count;
// Collection ranges from 0 to nCount -1.
for(long i = 0; i < nCount; i++)
{
pErr = m_pConnection->Errors->GetItem(i);
wsprintf(ss,"Error number: %x\t%s\n", pErr->Number,
(LPCSTR)pErr->Description);
AfxMessageBox(ss);
}
}
m_pRecordset->Close();
return TRUE;
}
catch(...)
{
MessageBox("A problem faced while interacting with server","Administrator Module Error",MB_ICONSTOP|MB_OK);
m_pRecordset->Close();
return TRUE;
}
This code is not complete nor is it simple but it will provide you all the elements that make up database intaraction through ADO in VC++
Regards
Ankur Verma
|