ADO多次调用存储过程问题
最近写连数据库相关的问题,碰到连续调用SP的时候,老是出问题,请教各位大牛帮我看看,谢谢了!
代码如下:
// Create Connect
BOOL OnInitADOConn()
{
::CoInitialize(NULL);
try
{
m_pConnection.CreateInstance("ADODB.Connection");
CStringstrConnect;
strConnect = _T("Provider=SQLOLEDB.1");
strConnect += _T(";Password=")+m_DbInfo.csPassward;
strConnect += _T(";Persist Security Info=True");
strConnect += _T(";User ID=")+m_DbInfo.csUSerName;
strConnect += _T(";Initial Catalog=")+m_DbInfo.csDBName;
strConnect += _T(";Data Source=")+m_DbInfo.csDataSource;
strConnect += _T("\n;");
m_pConnection->Open(strConnect.GetBuffer(strConnect.GetLength()+1),"","",adModeUnknown);
return TRUE;
}
catch (_com_error e)
{
CString str = e.Description();
errorMsg = &str;
AfxMessageBox(e.Description());
return FALSE;
}
// Create Command
// 循环调用时,会重新配置commText
try
{
if (!m_pADOComm)
{
CreateADOComm();
// set command type to stored process
m_pADOComm->CommandType = adCmdStoredProc;
m_pADOComm->ActiveConnection = m_pConnection;
m_pConnection->CursorLocation = adUseClient;
}
m_pADOComm->CommandText = commText;// SP name from Database
}
catch (_com_error e)
{
AfxMessageBox(e.Description());
return FALSE;
}
return TRUE;
}
// Add parameter to command
// 删除上次Command的Parameter的变量,循环调用时要重新配置参数...
InitAdoCommForSP()
{
long nParcount = 0;
m_AdoConn->m_pADOComm->Parameters->get_Count(&nParcount);
if (nParcount > 0)
{
m_AdoConn->ReleaseADOCommParamer();
}
_ParameterPtr pParm = NULL;
int lenth = 10;
_bstr_t strParmVal;
strParmVal = (_bstr_t)csSrcLang;
// Set source language to the command of the ADO
pParm = m_AdoConn->m_pADOComm->CreateParameter("@source_language",adVarChar,adParamInput,lenth,strParmVal);
m_AdoConn->AddParamToCom(pParm);
strParmVal = (_bstr_t)m_desLangArray[nDesLIndex].csLangCode;
pParm = m_AdoConn->m_pADOComm->CreateParameter("@desc_language",adVarChar,adParamInput,lenth,strParmVal);
m_AdoConn->AddParamToCom(pParm);
remap_flag_1 = 1;
remap_flag_2 = 4;
// Set remap_flag range to the command of the ADO
pParm = m_AdoConn->m_pADOComm->CreateParameter("@remap_flag_1",adInteger,adParamInput,sizeof(int),remap_flag_1);
m_AdoConn->AddParamToCom(pParm);
pParm = m_AdoConn->m_pADOComm->CreateParameter("@remap_flag_2",adInteger,adParamInput,sizeof(int),remap_flag_2);
m_AdoConn->AddParamToCom(pParm);
strMarker = "BANZ";
pParm = m_AdoConn->m_pADOComm->CreateParameter("@maker",adVarChar,adParamInput,max_vehicle_str_len,strMarker);
m_AdoConn->AddParamToCom(pParm);
}
// m_AdoConn的ComExcute
_RecordsetPtr& ADOConn::ComExcute()
{
if (!m_pRecordset)
{
m_pRecordset.CreateInstance(__uuidof(Recordset));
}
try{
m_pRecordset = m_pADOComm->Execute(NULL,NULL,adCmdStoredProc);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
return m_pRecordset;
}
// 多次调用的代码
// 用户每次点击动作会调用下面的代码:
InitAdoCommForSP();
// Get the result assemble from the db
_pRecordset = m_AdoConn->ComExcute();
int nCountRet = _pRecordset->GetRecordCount();