VC下用ADO调用存储过程例程

_ConnectionPtr m_pConnection; _CommandPtr m_pCommand; .cpp中在函数中执行 //建立ado连接 HRESULT hr; hr=m_pConnection.CreateInstance(__uuidof(Connection)); try { if(SUCCEEDED(hr)) { hr=m_pConnection->Open(_bstr_t(L”Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Viper;Data Source=Viper”),_bstr_t (L”sa”),_bstr_t (L””),adModeUnknown); } } catch(_com_error & err) { AfxMessageBox(err.Description(),MB_OK,0); AfxMessageBox(err.ErrorMessage(),MB_OK,0); AfxMessageBox(“无法连接SQL SERVER 服务器,程序将退出。请检查网络设备”,MB_OK,0); exit(0); }

//执行储存过程 CString cvar1,cvar2; int cvar3; cvar1=”ddd”; cvar2=””; cvar3=0; try { m_pCommand.CreateInstance(__uuidof(Command)); m_pCommand->ActiveConnection=app->m_pConnection; m_pCommand->CommandType=adCmdStoredProc; m_pCommand->CommandText=_bstr_t(“pr_zs_dzdy”);

_variant_t vvar1,vvar2,vvar3; vvar1=_variant_t(_bstr_t(cvar1)); vvar2=_variant_t(_bstr_t(cvar2)); vvar3=_variant_t(cvar3); _ParameterPtr mp_var1,mp_var2,mp_var3; mp_var1.CreateInstance(uuidof(Parameter)); mp_var2.CreateInstance(uuidof(Parameter)); mp_var3.CreateInstance(__uuidof(Parameter));

mp_var1=m_pCommand->CreateParameter ( _bstr_t(“var1”), adVarChar, adParamInput, 3, vvar1 ); m_pCommand->Parameters->Append(mp_var1);

mp_var2=m_pCommand->CreateParameter ( _bstr_t(“var2”), adVarChar, adParamOutput, 3, vvar2 ); m_pCommand->Parameters->Append(mp_var2);

mp_var3=m_pCommand->CreateParameter ( _bstr_t(“var3”), adIntger, adParamOutput, 9, vvar3 ); m_pCommand->Parameters->Append(mp_var3);

_variant_t vNull; vNull.vt=VT_ERROR; vNull.scode=DISP_E_PARAMNOTFOUND; m_pCommand->Execute(&vNull;,&vNull;,adCmdStoredProc); cvar2=mp_var2->Value.bstrVal; cvar3=mp_var3->Value; } catch(_com_error &error;) { MessageBox(error.ErrorMessage(),”ADO错误!”); MessageBox(error.Description(),”ADO错误!”); }

支持原创技术分享,据说打赏我的人,都找到了女朋友!