ADO MoveFirst, MoveLast, MoveNext, 与 MovePrevious 方法


MoveFirst方法

This method is used to move to the first record in a Recordset object. It also make the first record the current record.
Method方法的作用是:将记录指针移动到记录集中的第一条记录。它以第一条记录作为当前记录

Note: Calling MoveFirst or MoveLast when the Recordset is empty generates an error.
注意:当记录集为空时,请求MoveFirst或MoveLast将产生一个错误。


MoveLast方法

This method is used to move to the last record in a Recordset object. It also make the last record the current record.
Method方法的作用是:将记录指针移动到记录集中的最后一条记录。它以最后一条记录作为当前记录。

Note: Calling MoveFirst or MoveLast when the Recordset is empty generates an error.
注意:当记录集为空时,请求MoveFirst或MoveLast将产生一个错误。

Note: An error will occur if the Recordset object does not support bookmarks or backward cursor movement.
注意:如果记录集对象不支持书签或不能向后移动指针,那么将产生错误。


MoveNext方法

This method is used to move to the next record in a Recordset object. It also make the “next” record the current record.
Method方法的作用是:将记录指针移动到记录集中的下一条记录。它以下一条记录作为当前记录。

Note: If you call this method when the current record is the last record, it generates an error.
注意:当当前记录指针位于最后一条记录时,如果你请求这个方法,将产生错误。


MovePrevious 方法

This method is used to move to the previous record in a Recordset object. It also make the “previous” record the current record.
Method方法的作用是:将记录指针移动到记录集中的上一条记录。它以上一条记录作为当前记录。

Note: An error will occur if the Recordset object does not support bookmarks or backward cursor movement.
注意:如果记录集对象不支持书签或不能向后移动指针,那么将产生错误。

Note: If you call this method when the current record is the first record, it generates an error.
注意:当当前记录为记录集中第一条记录时,如果你请求该方法,那么它将产生一个错误。


***************************************************************************************************************************

例如:

在程序中,点击“上一条”按钮时,代码为:

void CADODlg::OnButtonPrev() 
{
m_pRecordset->MovePrevious(); 
if(m_pRecordset->adoBOF) 
m_pRecordset->MoveFirst(); 
DispRecord();
}


点击“下一条”按钮时,代码为:

void CADODlg::OnButtonNext() 
{
m_pRecordset->MoveNext(); 
if(m_pRecordset->adoEOF) 
m_pRecordset->MoveLast(); 
   DispRecord(); 

}


点击“添加”按钮时,代码为:

void CADODlg::OnButtonAdd() 
{
RefreshData();
try 

// 写入各字段值 
m_pRecordset->AddNew(); 

catch(_com_error *e) 

AfxMessageBox(e->ErrorMessage()); 
 } 
}


点击删除按钮时,代码为:

void CADODlg::OnButtonDel() 
{
try 

AfxMessageBox(“删除当前记录”); 
m_pRecordset->Delete(adAffectCurrent); 
m_pRecordset->Update(); 
m_pRecordset->MoveNext(); 
if(m_pRecordset->adoEOF) 
m_pRecordset->MoveLast(); 
DispRecord(); 

catch(_com_error *e) 

AfxMessageBox(e->ErrorMessage()); 
}
}


点击更新按钮时,代码为:

void CADODlg::OnButtonUpdate() 
{
UpdateData(TRUE); 
m_pRecordset->PutCollect(“stuID”,_bstr_t(m_stuid));  // m_stuid等都是编辑框关联的控件(CString类型)
m_pRecordset->PutCollect(“name”,_bstr_t(m_name)); 
m_pRecordset->PutCollect(“sex”,_bstr_t(m_sex)); 
m_pRecordset->PutCollect(“subject”,_bstr_t(m_subject)); 
m_pRecordset->PutCollect(“age”,long(m_age)); 
m_pRecordset->Update(); 
  m_pRecordset->MoveLast(); 
}


清空编辑框里的内容的函数为:

void CADODlg::RefreshData()
{
m_age=0; 
m_name=”“; 
m_sex=”男”; 
m_stuid=”“; 
m_subject=”“; 
UpdateData(FALSE); 
}


在这里,DispRecord函数的代码为:

void CADODlg::DispRecord()
{
_variant_t  theValue; 
if(!m_pRecordset->adoEOF)   //判断记录集是否到末尾,如果没到,就可以读取记录中相应的值

        //获取学号的值 
        theValue=m_pRecordset->GetCollect(“stuID”); 

if(theValue.vt!=VT_NULL) 
m_stuid=(char*)_bstr_t(theValue); 


        //获取学生姓名 
        theValue=m_pRecordset->GetCollect(“name”); 
        if(theValue.vt!=VT_NULL) 
m_name=(char *)_bstr_t(theValue); 


        //获取学生年龄 
        theValue=m_pRecordset->GetCollect(“age”); 
        if(theValue.vt!=VT_NULL) 
            m_age=theValue.iVal; 


        //获取学生性别 
        theValue=m_pRecordset->GetCollect(“sex”); 
        if(theValue.vt!=VT_NULL) 
m_sex=(char *)_bstr_t(theValue); 


//获取学生专业 
theValue=m_pRecordset->GetCollect(“subject”); 
        if(theValue.vt!=VT_NULL) 
m_subject=(char *)_bstr_t(theValue); 

   UpdateData(FALSE); 

}


参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值