詳細介紹ADO參數(shù)相關問題
ADO參數(shù)的取值依次為adInteger、adChar、adChar、adSingle,adChar;Direction參數(shù)的取值依次為adParameterIn、adParameterIn、adParameterIn、adParameterOut、adParameterOut;
對于輸入ADO參數(shù),Size的值可以根據(jù)實際數(shù)值來定,對于輸出參數(shù),最好是根據(jù)定義確定(上例中ReturnInfo參數(shù)的Size值可以取為100)。
關于獲取Output的參數(shù)獲取ourput參數(shù)是大家最關注的問題,同時也是最“難”的問題,因為按照書本上的寫法,經常獲得不了Output參數(shù),ADO參數(shù)其實這個問題很容易解決:在調用_CommandPtr的Execute方法時,ADO參數(shù)寫成cmmd->Execute(NULL, NULL, adCmdStoredProc);而不要寫成RecordsetPtr rec = cmmd->Execute(NULL, NULL, adCmdStoredProc);#t#
也就是說,不取返回值(我不知道這是為什么,但是相信我,事情就是這樣)。ADO參數(shù)這句執(zhí)行完后,使用cmmd->Parameters->GetItem("XXXXXX")->GetValue();輸出參數(shù)的名稱就可以獲得輸出參數(shù)的值了。
以下是一個通過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);//返回參數(shù),返回新建的網絡的ID
- cmmd->Parameters->Append(param);
- cmmd->CommandText=_bstr_t("GSDT_NewNet");//存儲過程的名稱
- cmmd->ActiveConnection = m_pConPtr;//需要使用的ADO連接
- cmmd->CommandType=adCmdStoredProc;
- //注意下面的一行代碼,如果你寫成這樣,就獲得不了返回參數(shù)的值
- //_RecordsetPtr rec = cmmd->Execute(NULL, NULL, adCmdStoredProc);
- //我不知道這是為什么,但事實就是這樣:)
- cmmd->Execute(NULL, NULL, adCmdStoredProc);
- m_nNewNetID=(long)cmmd->Parameters->GetItem("NewNetID")->GetValue();//通過參數(shù)返回值
- 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.
- {