VC下如何调用存储过程的返回值啊??送分。。。!

wpk 2004-12-02 10:13:16
我用的是MFC,步知道CDatabas有没有此功能?或者还有其它控件?最好能给个例子看看了!wangpk8@163.com
多谢!
...全文
189 点赞 收藏 10
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
bigmm 2004-12-02
mask
回复
Trashy 2004-12-02
路过,看看!
回复
wpk 2004-12-02
HRESULT hr=m_pConnection.CreateInstance(_uuidof(Connection));
sprintf(strConn, "provider = ORAOLEDB.ORACLE;DATA SOURCE = %s", DsnName);
hr = m_pConnection->Open(strConn,(_bstr_t)sUserID,(_bstr_t)sPassword,adOpenUnspecified);
//执行上面这句话就报错了,怎么回事??
DsnName是Oracale的ODBC配置的连接名吗?


回复
oyljerry 2004-12-02
ADO的pCommand指针,可以
回复
hjunxu 2004-12-02
m_pConnection->Open((_bstr_t)"provider = ORAOLEDB.ORACLE;DATA SOURCE = TNSNAME"
,(_bstr_t)sUserID,(_bstr_t)sPassword,adOpenUnspecified);
回复
hjunxu 2004-12-02
This example uses the Append and CreateParameter methods to execute a stored procedure with an input parameter.
回复
wpk 2004-12-02
我用的是ORACALE数据库,pConnection的链接串怎么写呢?
m_pConnection.CreateInstance("ADODB.Connection");//创建连接对象
m_pConnection->Open((_bstr_t)DsnName,(_bstr_t)sUserID,(_bstr_t)sPassword,adOpenUnspecified);
这样写怎么步成功啊?
回复
DentistryDoctor 2004-12-02
SQLFetch
回复
hjunxu 2004-12-02
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")

#include <ole2.h>
#include <stdio.h>
#include "conio.h"
#include "AppendX.h"

//Function declaration
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void AppendX(VOID);
void PrintProviderError(_ConnectionPtr pConnection);

///////////////////////////////////////////////////////////
// //
// Main Function //
// //
///////////////////////////////////////////////////////////
void main()
{
HRESULT hr = S_OK;

if(FAILED(::CoInitialize(NULL)))
return;

AppendX();

//Wait here for the user to see the output.
printf("\n\nPress any key to continue...");
getch();
::CoUninitialize();
}


///////////////////////////////////////////////////////////
// //
// AppendX Function //
// //
///////////////////////////////////////////////////////////
VOID AppendX(VOID)
{

HRESULT hr = S_OK;

// Define ADO object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace.
_RecordsetPtr pRstByRoyalty = NULL;
_RecordsetPtr pRstAuthors = NULL;
_CommandPtr pcmdByRoyalty = NULL;
_ParameterPtr pprmByRoyalty = NULL;
_ConnectionPtr pConnection = NULL;


//Define Other variables
IADORecordBinding *picRs = NULL; //Interface Pointer declared.(VC++ Extensions)
CEmployeeRs emprs; //C++ class object

_bstr_t strCnn("Provider=sqloledb;Data Source=MyServer;"
"Initial Catalog=pubs;User Id=sa;Password=;");

_bstr_t strMessage, strAuthorID;

int intRoyalty;
VARIANT vtRoyalty;

try
{
//Open a Connection.
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
hr = pConnection->Open(strCnn,"","",adConnectUnspecified);
pConnection->CursorLocation = adUseClient;

//Open Command Object with one Parameter
TESTHR(pcmdByRoyalty.CreateInstance(__uuidof(Command)));
pcmdByRoyalty->CommandText = "byroyalty";
pcmdByRoyalty->CommandType = adCmdStoredProc;

//Get parameter value and append parameter
printf("Enter Royalty: ");
scanf("%d",&intRoyalty);

//Define Integer/variant.
vtRoyalty.vt = VT_I2;
vtRoyalty.iVal = intRoyalty;
pprmByRoyalty = pcmdByRoyalty->CreateParameter("percentage",adInteger,adParamInput,sizeof(int),vtRoyalty);
pcmdByRoyalty->Parameters->Append(pprmByRoyalty);

pprmByRoyalty->Value = vtRoyalty;

//Create Recordset by executing the command
pcmdByRoyalty->ActiveConnection = pConnection;
pRstByRoyalty = pcmdByRoyalty->Execute(NULL,NULL,adCmdStoredProc);

//Open the authors table to get author names for display
TESTHR(pRstAuthors.CreateInstance(__uuidof(Recordset)));

//You have to explicitly pass the default Cursor type and LockType to the Recordset here
hr = pRstAuthors->Open("authors",_variant_t((IDispatch*)pConnection,true),adOpenForwardOnly,adLockReadOnly,adCmdTable);

//Open an IADORecordBinding interface pointer which we'll use for Binding Recordset to a class
TESTHR(pRstAuthors->QueryInterface(__uuidof(IADORecordBinding),(LPVOID*)&picRs));

//Bind the Recordset to a C++ Class here
TESTHR(picRs->BindToRecordset(&emprs));

//Print current data in the recordset, adding
//author names from author table.
printf("Authors with %d percent royalty ",intRoyalty);

while(!(pRstByRoyalty->EndOfFile))
{
strAuthorID = pRstByRoyalty->Fields->Item["au_id"]->Value;
pRstAuthors->Filter = "au_id = '"+strAuthorID+"'";

printf("\n" "%s, %s %s",emprs.lau_idStatus == adFldOK ? emprs.m_szau_id : "<NULL>",\
emprs.lau_fnameStatus == adFldOK ? emprs.m_szau_fname : "<NULL>",\
emprs.lau_lnameStatus == adFldOK ? emprs.m_szau_lname : "<NULL>");

pRstByRoyalty->MoveNext();

}

//Release the IADORecordset Interface here
if (picRs)
picRs->Release();

pRstByRoyalty->Close();
pRstAuthors->Close();
pConnection->Close();
}

catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());

PrintProviderError(pConnection);

printf("\n Source : %s \n Description : %s \n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
}

}


///////////////////////////////////////////////////////////
// //
// PrintProviderError Function //
// //
///////////////////////////////////////////////////////////

VOID PrintProviderError(_ConnectionPtr pConnection)
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection's Error collection.
ErrorPtr pErr = NULL;
long nCount = 0;
long i = 0;

if( (pConnection->Errors->Count) > 0)
{
nCount = pConnection->Errors->Count;
// Collection ranges from 0 to nCount -1.
for(i = 0; i < nCount; i++)
{
pErr = pConnection->Errors->GetItem(i);
printf("Error number: %x\n Error Description: %s\n", pErr->Number,(LPCSTR) pErr->Description);
}
}
}
// EndAppendCpp

AppendX.h:

// BeginAppendH
#include "icrsint.h"


//This Class extracts only author id,fname,lastname

class CEmployeeRs : public CADORecordBinding
{
BEGIN_ADO_BINDING(CEmployeeRs)

//Column au_id is the 1st field in the recordset

ADO_VARIABLE_LENGTH_ENTRY2(1, adVarChar, m_szau_id,
sizeof(m_szau_id), lau_idStatus, TRUE)

ADO_VARIABLE_LENGTH_ENTRY2(2, adVarChar, m_szau_lname,
sizeof(m_szau_lname), lau_lnameStatus, TRUE)

ADO_VARIABLE_LENGTH_ENTRY2(3, adVarChar, m_szau_fname,
sizeof(m_szau_fname), lau_fnameStatus, TRUE)

END_ADO_BINDING()

public:

CHAR m_szau_id[20];
ULONG lau_idStatus;

CHAR m_szau_fname[40];
ULONG lau_fnameStatus;

CHAR m_szau_lname[40];
ULONG lau_lnameStatus;

};
// EndAppendH

回复
beyondtkl 2004-12-02
用ADO.

_CommandPtr pCommand;
pCommand.CreateInstance(_uuidof(Command));
pCommand->PutRefActiveConnection(pConnection);
pCommand->CommandText = _bstr_t("eboss_lp_AddHardWare");
pCommand->CommandType = adCmdStoredProc;
pCommand->Parameters->Refresh();
// 给存储过程参数传值
。。。
pCommand->Execute(&vtMissing,&vtMissing,adCmdStoredProc|adExecuteNoRecords);
cResult = (BYTE)pCommand->Parameters->Item[(_variant_t)(long)0]->Value.lVal;//here
回复
相关推荐
发帖
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2004-12-02 10:13
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……