ADO参数的取值依次为adInteger、adChar、adChar、adSingle,adChar;Direction参数的取值依次为adParameterIn、adParameterIn、adParameterIn、adParameterOut、adParameterOut;
对于输入ADO参数,Size的值可以根据实际数值来定,对于输出参数,最好是根据定义确定(上例中ReturnInfo参数的Size值可以取为100)。
关于获取Output的参数获取ourput参数是大家最关注的问题,同时也是最“难”的问题,因为按照书本上的写法,经常获得不了Output参数,ADO参数其实这个问题很容易解决:在调用_CommandPtr的Execute方法时,ADO参数写成cmmd->Execute(NULL, NULL, adCmdStoredProc);而不要写成RecordsetPtr rec = cmmd->Execute(NULL, NULL, adCmdStoredProc);#t#
也就是说,不取返回值(我不知道这是为什么,但是相信我,事情就是这样)。ADO参数这句执行完后,使用cmmd->Parameters->GetItem("XXXXXX")->GetValue();输出参数的名称就可以获得输出参数的值了。
以下是一个通过ADO调用存储过程的部分代码:
- _ParameterPtr param;
- param = cmmd->CreateParameter(""/*NetType*/,adTinyInt, adParamInput,
- sizeof(BYTE),(BYTE)(m_nNetType+1));
- cmmd->Parameters->Append(param);
- param = cmmd->CreateParameter(""/*Name*/,adVarChar, adParamInput,
- m_strName.GetLength()+1, _variant_t(m_strName));
- cmmd->Parameters->Append(param);
- param = cmmd->CreateParameter(""/*Desp*/,adVarChar, adParamInput,
- m_strDesp.GetLength()+1, _variant_t(m_strDesp));
- cmmd->Parameters->Append(param);
- param = cmmd->CreateParameter("NewNetID"/*NetID*/,adInteger, adParamOutput,
- sizeof(long), (long)m_nNewNetID);//返回参数,返回新建的网络的ID
- cmmd->Parameters->Append(param);
- cmmd->CommandText=_bstr_t("GSDT_NewNet");//存储过程的名称
- cmmd->ActiveConnection = m_pConPtr;//需要使用的ADO连接
- cmmd->CommandType=adCmdStoredProc;
- //注意下面的一行代码,如果你写成这样,就获得不了返回参数的值
- //_RecordsetPtr rec = cmmd->Execute(NULL, NULL, adCmdStoredProc);
- //我不知道这是为什么,但事实就是这样:)
- cmmd->Execute(NULL, NULL, adCmdStoredProc);
- m_nNewNetID=(long)cmmd->Parameters->GetItem("NewNetID")->GetValue();//通过参数返回值
- cmmd.Detach();
- ActiveX Data Objects (ADO) enables you to write a client application to access and manipulate data in a database server through a provider.
- ADO's primary benefits are ease of use, high speed, low memory overhead, and a small disk footprint.
- This sample project is for ADODB, an implementation of ADO optimized for use with Microsoft OLE DB providers, including the Microsoft ODBC provider for OLE DB.
- Using this we can execute stored procedure, pass arguments and retrieve value. To use this sample you will have to create the two stored procedures mentioned below.
- For using this project you need MFC 5.0 OR above + ADO in your machine.
- {