VC存储过程

如果用ado的话用_CommandPtr执行sql语句"callyourproc"或是"execyourproc(?,?,?)"参数用m_commandptr->Parameters->Append(m_commandptr->CreateParameter("tableitemid",adInteger,adParamInput,4,COleVariant((short)desttableitemid)));来建立.
---------------------------------------------------------------

#include"stdafx.h"
#import"C:/PROGRA~1/COMMON~1/System/ado/msado15.dll"rename("EOF","adoEOF")
structInitOle
{
InitOle(){::CoInitialize(NULL);}
~InitOle(){::CoUninitialize();}
}_init_InitOle_;
voidPrintProviderError(ADODB::_ConnectionPtrpConnection);

intmain(intargc,char*argv[])
{
printf("CreateaadParamReturnValueforCommandobjecttogetreturnvalueofstoredprocedure/n");
printf("Reference:MSDN---AppendandCreateParameterMethodsExample(VC++)/n");
ADODB::_ConnectionPtrConn1;
ADODB::_CommandPtrCmd1;
ADODB::_RecordsetPtrRs1;
ADODB::_ParameterPtrretParam=NULL;
ADODB::_ParameterPtrinParam=NULL;
ADODB::_ParameterPtroutParam=NULL;
_variant_tvtEmpty(DISP_E_PARAMNOTFOUND,VT_ERROR);
_variant_tvtEmpty2(DISP_E_PARAMNOTFOUND,VT_ERROR);
_bstr_tbstrConnect(L"driver={sqlserver};server=Cell;Database=zhg;UID=sa;PWD=;");
//thefollowingstoredprocedurereturn12345intvalue
_bstr_tbstrCreate(L"createprocsp_AdoTest(@OutParamintOUTPUT,@InParamint)"
L"as"
L"select@OutParam=@InParam+10"
L"select*fromTable1"
L"return12345");
_bstr_tbstrSP(L"sp_Adotest");
try
{
_bstr_tbstrEmpty;
Conn1.CreateInstance(__uuidof(ADODB::Connection));
Cmd1.CreateInstance(__uuidof(ADODB::Command));
Rs1.CreateInstance(__uuidof(ADODB::Recordset));
//Establishconnection.
Conn1->ConnectionString=bstrConnect;
Conn1->Open(bstrConnect,bstrEmpty,bstrEmpty,-1);
//Openrecordset.
Cmd1->ActiveConnection=Conn1;
Cmd1->CommandText=bstrSP;
Cmd1->CommandType=ADODB::adCmdStoredProc;
retParam=Cmd1->CreateParameter(_bstr_t("Return"),ADODB::adInteger,ADODB::adParamReturnValue,sizeof(int));
Cmd1->Parameters->Append(retParam);
outParam=Cmd1->CreateParameter(_bstr_t("OutParam"),ADODB::adInteger,ADODB::adParamOutput,sizeof(int));
Cmd1->Parameters->Append(outParam);
inParam=Cmd1->CreateParameter(_bstr_t("InParam"),ADODB::adInteger,ADODB::adParamInput,sizeof(int),_variant_t((long)10));
inParam->Value=_variant_t((long)10);
Cmd1->Parameters->Append(inParam);
Cmd1->Parameters->Refresh();
Cmd1->Parameters->Item[_variant_t(_bstr_t("@InParam"))]->Value=_variant_t((long)11);
Rs1->put_CursorLocation(ADODB::adUseClient);
Rs1->Open((_variant_t((IDispatch*)Cmd1)),vtEmpty,ADODB::adOpenStatic,
ADODB::adLockReadOnly,-1);
//Getreturnvalueofthestoredprocedure.adCmdUnknown
TCHARtcbuf[1024];
longretvalue=Cmd1->Parameters->Item[(short)0]->Value;
longp2=Cmd1->Parameters->Item[(short)1]->Value;
longp3=Cmd1->Parameters->Item[(short)2]->Value;
intrecordcount=0;
if(Rs1->State==ADODB::adStateClosed)
MessageBox(NULL,"norecordsetisreturnedfromthestoredprocedure","Information",MB_OK);
else
recordcount=Rs1->GetRecordCount();
wsprintf(tcbuf,

你可能感兴趣的:(存储过程)