Problem in ADO
- From: Coder <Coder@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 26 Apr 2007 23:56:01 -0700
Hi ;
I am facing some problem in ADO Related issue.
Recordset is not created for Parametrised query.
following snippet of code is working fine for select query and in case of
table.
but its not functional for the case of parametrised query pls tell me how
can i make changes so that it will become functional for parametrised query.
i searched i din't find Append parameter function for that.
And actually I am using open function and I search that Stored parametrised
query needs to be executed. but i tried hard not able to use execute function.
ADORecordset* MWAdoComUtils::CreateRecordset(CDaoRecordset* pDaoRecordset)
{
if(pDaoRecordset)
{
CDaoDatabase* pDatabase = pDaoRecordset->GetDatabase();
CString strQuery = pDaoRecordset->BuildSQLStringForOpen();// it calls Get
//default sql function it will get either name of query or will find name
of table.
CString strDatabase = pDaoRecordset->GetDefaultDBName();
CString strConnect = pDatabase->GetConnectionString();
int nIndex = strDatabase.ReverseFind('\\');
CString strPath = strDatabase.Left(nIndex);
CString strFileName = strDatabase.Right(strDatabase.GetLength() - nIndex -
1);
strConnect.Format(_T("driver=Microsoft Access Driver
(*.mdb);DBQ=%s;DefaultDir=%s;"), strFileName, strPath);
BSTR bstrQuery = strQuery.AllocSysString();
BSTR bstrConnect = strConnect.AllocSysString();
ADORecordset* pAdoRecordSet = MWAdoComUtils::CreateRecordset(bstrConnect,
bstrQuery);// it will call
//following Create Recordset function with the squery name and connecetion
string
::SysFreeString( bstrQuery );
::SysFreeString( bstrConnect );
return pAdoRecordSet;
}
return NULL;
}
ADORecordset* MWAdoComUtils::CreateRecordset(BSTR bstrSource, BSTR bstrSQL)
{
HRESULT hr;
COleVariant vNull;
ADORecordset* piRecordSet = NULL;
ADOConnection* piConnection=NULL;
hr = CoInitialize(NULL);
if (FAILED(hr))
{
AfxMessageBox(_T("Unable to Initialize COM"));
goto ErrorExit;
}
//Intialize Connection
hr = CoCreateInstance(CLSID_CADOConnection, NULL, CLSCTX_INPROC_SERVER,
IID_IADOConnection, (LPVOID *)&piConnection);
if(FAILED(hr))
{
AfxMessageBox(_T("Unable to create connection object"));
goto ErrorExit;
}
if ( bstrSource == NULL || bstrSQL == NULL )
{
AfxMessageBox(_T("Invalid(NULL) Source name or SQL string"));
goto ErrorExit;
}
//Initialize Recordset
hr = CoCreateInstance(CLSID_CADORecordset, NULL, CLSCTX_INPROC_SERVER,
IID_IADORecordset, (LPVOID *)&piRecordSet);
if(FAILED(hr))
{
AfxMessageBox(_T("Unable to create Recordset"));
if (FAILED(hr)) goto ErrorExit;
}
BOOL bSuccess;
hr = piConnection->Open( bstrSource, NULL, NULL, -1 );
if (FAILED(hr))
{
AfxMessageBox(_T("Unable to make connection to Source:
")+CString(bstrSource));
goto ErrorExit;
}
hr = piRecordSet->putref_ActiveConnection(piConnection);
if (FAILED(hr))
{
AfxMessageBox(_T("Unable to bind Connection/Recordset"));
goto ErrorExit;
}
hr = piRecordSet->put_Source(bstrSQL);
if (FAILED(hr))
{
AfxMessageBox(_T("Unable to put_Source"));
goto ErrorExit;
}
// If your provider does not have cursor capabilities set use
// put_CursorLocation(adUseClient) on the recordset before calling Open
// to make ADO use the cursor library from the service components.
vNull.vt = VT_ERROR;
vNull.scode = DISP_E_PARAMNOTFOUND;
//MW_ADO_CHECK(piRecordSet->Open(vNull, vNull,
//adOpenKeyset, adLockOptimistic, adCmdText),piRecordSet,bSuccess)
MW_ADO_CHECK(piRecordSet->Open(vNull, vNull, adOpenKeyset,
adLockOptimistic, adCmdUnknown),piRecordSet,bSuccess)
//its a macro inside that open function si called
//for parametrised query it should be execute function but i am not able to
make .that
if(!bSuccess) goto ErrorExit;
if(piConnection)
piConnection->Release();
return piRecordSet;
ErrorExit:
TCHAR szBuf[256];
wsprintf(szBuf, _T("Error: %d \n"), hr);
if (hr)
AfxMessageBox(szBuf);
if(piConnection)
piConnection->Release();
if(piRecordSet)
piRecordSet->Release();
return NULL;
}
its a macro
#define MW_ADO_CHECK(exp, recordset, succeded) \
{ \
HRESULT hr; \
succeded = TRUE; \
if(FAILED(hr = exp)) \
{ \
succeded = FALSE; \
ADOConnection *piConnection = 0; \
VARIANT var;\
VariantInit(&var);\
recordset->get_ActiveConnection(&var);\
piConnection = (ADOConnection *) V_DISPATCH(&var);\
ADOErrors *piErrors = 0; \
piConnection->get_Errors(&piErrors); \
long lCount; \
piErrors->get_Count(&lCount); \
ADOError *piError = 0; \
for(int i=0; i<(int)lCount; i++) \
{ \
VARIANT varIndex; \
varIndex.vt = VT_INT; \
varIndex.intVal = i; \
piErrors->get_Item(varIndex, &piError); \
BSTR error; \
piError->get_Description(&error); \
AfxMessageBox(CString(error)); \
} \
piErrors->Clear(); \
if (piErrors) \
piErrors->Release();\
if (piConnection) \
piConnection->Release();\
if (piError) \
piError->Release();\
} \
}
Thanks in advance guya I am really helpless pls help me out
waiting for your replies.. :-(
.
- Prev by Date: Problem in ADO
- Next by Date: Can someone explain why my code is not working
- Previous by thread: Problem in ADO
- Next by thread: Can someone explain why my code is not working
- Index(es):
Relevant Pages
|
|