ADO中如何得到_RecordsetPtr所取得记录集的字段个数?

alornwolf 2002-11-29 03:59:37
如题!!!
...全文
425 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fingerfox 2002-11-29
  • 打赏
  • 举报
回复
rs->Fields->GetCount();
despird 2002-11-29
  • 打赏
  • 举报
回复
lzd(雨中绝音)真是个好老师,可惜罗嗦点;
byter()的方法即可;
sparrowzhou(狗尾草) 审题错误。
lzd 2002-11-29
  • 打赏
  • 举报
回复
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")

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

// Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void CountX(void);
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);

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

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

CountX();

::CoUninitialize();
}

///////////////////////////////////////////////////////////
// //
// CountX Function //
// //
///////////////////////////////////////////////////////////

void CountX()
{
// Define ADO object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace
_RecordsetPtr pRstEmployee = NULL;

//Define Other Variables
HRESULT hr = S_OK;
_variant_t Index;
Index.vt = VT_I2;
int j = 0;

_bstr_t strCnn("Provider=SQLOLEDB;Data Source=srv;"
"Initial Catalog=Pubs;User Id=sa;Password=;");

try
{
// Open recordset with data from Employee table.
TESTHR(pRstEmployee.CreateInstance(__uuidof(Recordset)));
pRstEmployee->Open("Employee", strCnn, adOpenForwardOnly,
adLockReadOnly, adCmdTable);

// Print information about Fields collection.
printf("%d Fields in Employee\n", pRstEmployee->Fields->Count);
for (int intLoop = 0;
intLoop <= (pRstEmployee->Fields->Count-1);
intLoop++)
{
Index.iVal = intLoop;
printf(" %s\n",(LPSTR) pRstEmployee->Fields->
GetItem(Index)->Name);
}

// Print information about Properties collection.
printf("\n%d Properties in Employee\n", pRstEmployee->
Properties->Count);
for (intLoop = 0;
intLoop <= (pRstEmployee->Properties->Count - 1);
intLoop++)
{
j++;
Index.iVal = intLoop;
printf(" %s\n" ,(LPSTR)pRstEmployee->Properties->
GetItem(Index)->Name);
if (((j % 23) == 0) || (intLoop == 11))
{
printf("\nPress any key to continue...");
getch();

//Clear the screen for the next display
system("cls");
j = 0;
}
}
// Clean up objects before exit.
pRstEmployee->Close();
}

catch(_com_error &e)
{
// Notify user of any errors that result from
// executing the query.
// Pass a connection pointer accessed from the Recordset.
_variant_t vtConnect = pRstEmployee->GetActiveConnection();

switch(vtConnect.vt)
{
case VT_BSTR:
PrintComError(e);
break;
case VT_DISPATCH:
PrintProviderError(vtConnect);
break;
default:
printf("Errors occured.");
break;
}
}
}

///////////////////////////////////////////////////////////
// //
// 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("Error number: %x\t%s\n",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);
}

byter 2002-11-29
  • 打赏
  • 举报
回复
_RecordsetPtr rs;
。。。
(打开数据库)
。。。
_variant_t va = rs->Fields->Count;
sparrowzhou 2002-11-29
  • 打赏
  • 举报
回复
GetRecordCount()

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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