ADO中的_CommandPtr对象怎么用

daidai 2000-08-08 04:26:00
机械工业出版社的《visual C 6 数据库开发技术》上写的
pCmd->ActiveConnection = pConn;
pCmd->CommandText = "Select * from course";
_RecordsetPtr pRs;
pRs = pCmd->Execute();

但编译时说error C2660: 'Execute' : function does not take 0 parameters
里面的参数填什么呢?MSDN上只有VB的例子,好象对ADO VC的自动提示也没有,
我该怎么办?请高手指点
...全文
276 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xubin_sh 2000-08-08
  • 打赏
  • 举报
回复
Execute(VARIANT *RecordsAffected, VARIANT *Parameters, long Options,
_ADORecordset **ppirs)
给个例子吧(剪自MSDN):
#include <ole2.h>
#include <stdio.h>

#import "c:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF", "EndOfFile")

// Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void ExecuteX(void);
void ExecuteCommand(_CommandPtr pCmdTemp, _RecordsetPtr pRstTemp);
void PrintOutput(_RecordsetPtr pRstTemp);
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);

////////////////////////////////
// Main Function //
////////////////////////////////

void main()
{
if(FAILED(::CoInitialize(NULL)))
return;

ExecuteX();

::CoUninitialize();
}

///////////////////////////////////
// ExecuteX Function //
///////////////////////////////////

void ExecuteX(void)
{
HRESULT hr = S_OK;

// Define string variables.
_bstr_t strSQLChange("UPDATE Titles SET Type = "
"'self_help' WHERE Type = 'psychology'");
_bstr_t strSQLRestore("UPDATE Titles SET Type = "
"'psychology' WHERE Type = 'self_help'");
_bstr_t strCnn("Provider=sqloledb;Data Source=srv;"
"Initial Catalog=Pubs;User Id=sa;Password=;");

// Define ADO object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace.
_ConnectionPtr pConnection = NULL;
_CommandPtr pCmdChange = NULL;
_RecordsetPtr pRstTitles = NULL;

try
{
// Open connection.
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
pConnection->Open (strCnn, "", "", adConnectUnspecified);

// Create command object.
TESTHR(pCmdChange.CreateInstance(__uuidof(Command)));
pCmdChange->ActiveConnection = pConnection;
pCmdChange->CommandText = strSQLChange;

// Open Titles table, casting Connection pointer to an
// IDispatch type so converted to correct type of variant.
TESTHR(pRstTitles.CreateInstance(__uuidof(Recordset)));
pRstTitles->Open ("Titles", _variant_t((IDispatch *) pConnection,
true), adOpenStatic, adLockOptimistic, adCmdTable);

// Print report of original data.
printf(
"\n\nData in Titles table before executing the query: \n");

// Call function to print loop recordset contents.
PrintOutput(pRstTitles);

// Clear extraneous errors from the Errors collection.
pConnection->Errors->Clear();

// Call ExecuteCommand subroutine to execute pCmdChange command.
ExecuteCommand(pCmdChange, pRstTitles);

// Print report of new data.
printf(
"\n\n\tData in Titles table after executing the query: \n");
PrintOutput(pRstTitles);

// Use the Connection object's Execute method to
// execute SQL statement to restore data.
pConnection->Execute(strSQLRestore, NULL, adExecuteNoRecords);

// Retrieve the current data by requerying the recordset.
pRstTitles->Requery(adCmdUnknown);

// Print report of restored data.
printf(
"\n\n\tData after exec. query to restore original info: \n");
PrintOutput(pRstTitles);

// Clean up objects before exit.
pRstTitles->Close();
pConnection->Close();
}

catch (_com_error &e)
{
PrintProviderError(pConnection);
PrintComError(e);
}
}

//////////////////////////////////////////
// ExecuteCommand Function //
//////////////////////////////////////////

void ExecuteCommand(_CommandPtr pCmdTemp, _RecordsetPtr pRstTemp)
{
try
{
// CommandText property already set before function was called.
pCmdTemp->Execute(NULL, NULL, adCmdText);

// Retrieve the current data by requerying the Recordset.
pRstTemp->Requery(adCmdUnknown);
}

catch(_com_error &e)
{
// Notify user of any errors that result from
// executing the query.
// Pass a connection pointer accessed from the Recordset.
PrintProviderError(pRstTemp->GetActiveConnection());
PrintComError(e);
}
}

/////////////////////////////////////
// PrintOutput Function //
/////////////////////////////////////

void PrintOutput(_RecordsetPtr pRstTemp)
{
// Ensure at top of Recordset.
pRstTemp->MoveFirst();

// If EOF is true, then no data and skip print loop.
if( pRstTemp->EndOfFile )
{
printf("\tRecordset empty\n");
}
else
{
// Define temporary strings for output conversions.
// Initialize to first record's values.
_bstr_t bstrTitle;
_bstr_t bstrType;

// Enumerate Recordset and print from each.
while(!(pRstTemp->EndOfFile))
{
// Convert variant string to convertable string type.
bstrTitle = pRstTemp->Fields->GetItem("Title")->Value;
bstrType = pRstTemp->Fields->GetItem("Type")->Value;
printf("\t%s, %s \n",
(LPCSTR) bstrTitle,
(LPCSTR) bstrType);

pRstTemp->MoveNext();
}
}
}

///////////////////////////////////////////////
// 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;

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

//////////////////////////////////////
// PrintComError Function //
//////////////////////////////////////

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

// Print Com errors.
printf("Error\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR) bstrSource);
printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}
daidai 2000-08-08
  • 打赏
  • 举报
回复
谢谢,
那为什么我的pCmd没有自动提示了呢(其他的类都有),真是痛苦啊

daidai 2000-08-08
  • 打赏
  • 举报
回复
谢谢,
那为什么我的pCmd没有自动提示了呢,真是痛苦啊
huntout 2000-08-08
  • 打赏
  • 举报
回复
pCmd->Execute(NULL,NULL,adCmdText);
我花钱买的,结果没用上,太亏了 本系统采用ADO来访问SQL数据库,这里充分应用了C++类封装的功能,根据本系统应用的范围,将访问数据库的功能函数封装在一个类CMyDatabase。 正如前所述,ADO是访问数据库的一个方法,它提供了不同的接口。ADO库包含三个基本接口:_ConnectionPtr接口、_CommandPtr接口和_RecordsetPtr接口。 _ConnectionPtr接口返回一个记录集或一个空指针。通常使用它来创建一个数据连接或执行一条不返回任何结果的SQL语句,如一个存储过程。使用_ConnectionPtr接口返回一个记录集不是一个好的使用方法。对于要返回记录的操作通常用_RecordserPtr来实现。而用_ConnectionPtr操作时要想得到记录条数得遍历所有记录,而用_RecordserPtr时不需要。 _CommandPtr接口返回一个记录集。它提供了一种简单的方法来执行返回记录集的存储过程和SQL语句。在使用_CommandPtr接口时,你可以利用全局_ConnectionPtr接口,也可以在_CommandPtr接口里直接使用连接串。如果你只执行一次或几次数据访问操作,后者是比较好的选择。但如果你要频繁访问数据库,并要返回很多记录集,那么,你应该使用全局_ConnectionPtr接口创建一个数据连接,然后使用_CommandPtr接口执行存储过程和SQL语句。 _RecordsetPtr是一个记录集对象。与以上两种对象相比,它对记录集提供了更多的控制功能,如记录锁定,游标控制等。同_CommandPtr接口一样,它不一定要使用一个已经创建的数据连接,可以用一个连接串代替连接指针赋给_RecordsetPtr的connection成员变量,让它自己创建数据连接。如果你要使用多个记录集,最好的方法是同Command对象一样使用已经创建了数据连接的全局_ConnectionPtr接口 ,然后使用_RecordsetPtr执行存储过程和SQL语句。  根据这些,我们将数据库的各种操作封装到CMyDatabase类里。几个主要函数说明如下:

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

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

试试用AI创作助手写篇文章吧