关于数据库操作的小疑问

phyllis 2002-07-19 02:48:28
我使用ado操作数据库对象
用如下打开数据库
strSQL.Format("SELECT * FROM MoneyTable");
m_pRecordset->Open(_variant_t(strSQL), _variant_t((IDispatch*)theApp.m_pConnection, true),adOpenStatic, adLockOptimistic, adCmdText);

在生成的数据表中,我想把当前操作的游标移到
日期字段date为“1999-12-31"并且字段name为"John"的
寻找到的第一条记录上去
请问应该用m_pRecordset->?
怎么样使用控制阿?
...全文
50 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wyfzy521 2002-07-19
  • 打赏
  • 举报
回复
Find Method Example (VC++)
This example uses the Recordset object's Find method to locate and count the number of business titles in the Pubs database. The example assumes the underlying provider does not support similar functionality.

// BeginFindCpp
#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 "FindX.h"

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

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

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

FindX();

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

//////////////////////////////////////////////////////////
// //
// FindX Function //
// //
//////////////////////////////////////////////////////////

void FindX(void)
{
HRESULT hr = S_OK;

// Define ADO object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace.
_ConnectionPtr pConnection = NULL;
_RecordsetPtr pRstTitles = NULL;
IADORecordBinding *picRs = NULL; //Interface Pointer declared.
CTitlesRs titlers; //C++ class object

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

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

// Open title Table
TESTHR(pRstTitles.CreateInstance(__uuidof(Recordset)));

pRstTitles->Open("SELECT title_id FROM titles",
_variant_t((IDispatch *)pConnection),
adOpenStatic, adLockReadOnly, adCmdText);

// The default parameters are sufficient to search forward
// through a Recordset.

pRstTitles->Find ("title_id LIKE 'BU%'",0,adSearchForward,"");

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

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

// Skip the current record to avoid finding the same
// row repeatedly. The bookmark is redundant because Find
// searches from the current position.
int count = 0;

//Continue if last find succeeded.
while (!(pRstTitles->EndOfFile))
{
printf("Title ID: %s\n",titlers.lt_titleidStatus == adFldOK ?
titlers.m_szt_titleid : "<NULL>");
count++; //Count the last title found.

_variant_t mark = pRstTitles->Bookmark; //Note current pos.
pRstTitles->Find("title_id LIKE 'BU%'", 1, adSearchForward,
mark);
}

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

// Clean up objects before exit.
pRstTitles->Close();
pConnection->Close();
printf("The number of business titles is %d\n",count);

}

catch(_com_error &e)
{
// Notify the user of errors if any.
// Pass a connection pointer accessed from the Recordset.
PrintProviderError(pConnection);
PrintComError(e);
}
}

//////////////////////////////////////////////////////////
// //
// 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,
(LPCSTR)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);
}
// EndFindCpp

FindX.h

// BeginFindH
#include "icrsint.h"

//This Class extracts only titleId from Titles table.
class CTitlesRs : public CADORecordBinding
{
BEGIN_ADO_BINDING(CTitlesRs)

// Column title_id is the 1st field in the recordset
// from Titles table.
ADO_VARIABLE_LENGTH_ENTRY2(1, adVarChar, m_szt_titleid,
sizeof(m_szt_titleid), lt_titleidStatus, FALSE)

END_ADO_BINDING()

public:
CHAR m_szt_titleid[150];
ULONG lt_titleidStatus;
};
// EndFindH

phyllis 2002-07-19
  • 打赏
  • 举报
回复
太好了阿,给我个例子吧
我没有,那个东东太贵了
可以寄我这个信箱
lanyuai@hotmail.com
wyfzy521 2002-07-19
  • 打赏
  • 举报
回复
你有没有MSDN2001,没有的话,我给你一个例子。有的话你自己看看。
wyfzy521 2002-07-19
  • 打赏
  • 举报
回复
你有没有MSDN2001,没有的话,我给你一个例子。有的话你自己看看。
phyllis 2002-07-19
  • 打赏
  • 举报
回复
find?
怎么用阿?
wyfzy521 2002-07-19
  • 打赏
  • 举报
回复
使用Find方法吧,或者,Seek也可以。

4,011

社区成员

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

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