⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 axrecordset.cpp

📁 一个非常好用的ADO封装类,程序员不再需要跟烦人的COM接口打交道,写数据库程序不再麻烦!
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		delete pvtBookmark;
    DoFieldExchange(FALSE);
  }

	PositionEnum recPos;

  hr = m_piRecordset->get_AbsolutePosition(&recPos);
	if FAILED( hr )
		ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::AbsolutePosition"), hr);

	return ((long)recPos);
}

// Method: ActiveCommand
//   Desc: Returns a reference to the associated CAxCommand object provided
//         the record set was initially opened by means of a CAxCommand.
//
//   Args: none
//
// Return: Reference to ADOCommand object. NULL if record set was
//         opened directly.
//
CAxCommand* CAxRecordset::ActiveCommand()
{
  return (m_pCommand);
}

// Method: ActiveConnection
//   Desc: Returns a reference to the associated CAxConnection object. 
//
//   Args: none
//
// Return: Reference to ADOConnection object.
//
CAxConnection* CAxRecordset::ActiveConnection()
{
  return (m_pConnection);
}

// Method: IsBOF - Proxy for ADO's BOF method
//   Desc: Determine if the current record pointer is before
//         the first record.
//
//   Args: none
//
// Return: Returns true is the record pointer is located before the
//         first record or if the recordset contains no records.
//         Otherwise false.
//
bool CAxRecordset::IsBOF()
{
	VALID_ADO_OBJECT(m_piRecordset);
	VARIANT_BOOL vbBOF = 0;

  HRESULT hr = m_piRecordset->get_BOF(&vbBOF);
	if FAILED( hr )
		ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::IsBOF"), hr);

	return ( vbBOF < 0 );
}

// Method: Bookmark
//   Desc: Set/Return a unique record identifier in the form of a 
//         _variant_t class. If the Bookmark method is called with
//         no arguments, a new variant is created and returned to
//         the caller. The host application needs to save this
//         refernece and delete it when no longer needed. Otherwise,
//         a memory leak will occur. If a variant argument is supplied,
//         Bookmark will attempt to restore the record position as 
//         specified in the variant argument.
//
//   Args: _variant_t*
//           Reference to an existing variant type. This is generally
//           the variant reference that was returned from a previous
//           call to Bookmark
//
// Return: Reference to a newly created instance of a _variant_t class
//         if the method was called with no argument. Otherwise, NULL
//
_variant_t* CAxRecordset::Bookmark(_variant_t* pvtBookmark)
{
	VALID_ADO_OBJECT(m_piRecordset);
  HRESULT hr = S_OK;
  _variant_t *pvtBkMrk = NULL;

  if ( pvtBookmark && pvtBookmark->vt == VT_R8 )
  {
    hr = m_piRecordset->put_Bookmark(*pvtBookmark);
	  if FAILED( hr )
		  ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::Bookmark"), hr);

  	DoFieldExchange(FALSE);
  }
  else
  {
    if ( AbsolutePosition() > 0 )
    {
      pvtBkMrk  = new _variant_t;
      hr = m_piRecordset->get_Bookmark(pvtBkMrk );
      if FAILED( hr )
      {
        delete pvtBkMrk;
		    ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::Bookmark"), hr);
      }
    }
  }

  return (pvtBkMrk );
}

// Method: CacheSize
//   Desc: Set/Return the number of records that are kept in
//         local memory. The default value is 1. Can be changed
//         throughout the life of the record set object.
//
//   Args: nCacheSize
//           Specified the number of records to keep in local memory.
//           The value must be 1 or greater.
//
// Return: The current CacheSize setting.
//
long CAxRecordset::CacheSize(long nCacheSize)
{
	VALID_ADO_OBJECT(m_piRecordset);
  HRESULT hr = S_OK;

  if ( nCacheSize > 0 )
  {
    hr = m_piRecordset->put_CacheSize(nCacheSize);
	  if FAILED( hr )
		  ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::CacheSize"), hr);
  }

  long cacheSize;
  hr = m_piRecordset->get_CacheSize(&cacheSize);
  if FAILED( hr )
		ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::CacheSize"), hr);

  return (cacheSize);
}

// Method: CursorLocation
//   Desc: Set/Return the location of the cursor service.
//         Read-Write while the record set is closed. Read-only
//         if the record set is open. Default is adUseServer.
//         Host applications will generally want to set this
//         to adUseClient.
//
//   Args: pCursorLocation
//           Reference to a CursorLocationEnum variable.
//            adUseNone	= 1,
//            adUseServer	= 2,
//            adUseClient	= 3,
//            adUseClientBatch	= 3
//
// Return: The current CursorLocationEnum setting.
//
CursorLocationEnum CAxRecordset::CursorLocation(long eCursorLocation)
{
	VALID_ADO_OBJECT(m_piRecordset);
  HRESULT hr = S_OK;

  if ( eCursorLocation >= adUseNone && eCursorLocation <= adUseClient )
  {
    hr = m_piRecordset->put_CursorLocation((CursorLocationEnum)eCursorLocation);
	  if FAILED( hr )
		  ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::CursorLocation"), hr);
  }

  CursorLocationEnum curLoc;
  hr = m_piRecordset->get_CursorLocation(&curLoc);
	if FAILED( hr )
		ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::CursorLocation"), hr);

  return (curLoc);
}

// Method: CursorType
//   Desc: Set/Return the type of cursor used for the recordset.
//         Read-Write while the recordset is closed. Read-only
//         if the recordset is open. Default is adOpenForwardOnly.
//
//   Args: pCursorLocation
//           Reference to a CursorTypeEnum variable.
//            adOpenUnspecified	= -1,
//            adOpenForwardOnly	= 0,
//            adOpenKeyset	= 1,
//            adOpenDynamic	= 2,
//            adOpenStatic	= 3
//
// Return: The current CursorTypeEnum setting.
//
CursorTypeEnum CAxRecordset::CursorType(CursorTypeEnum eCursorType)
{
	VALID_ADO_OBJECT(m_piRecordset);
  HRESULT hr = S_OK;

  if ( eCursorType >= adOpenForwardOnly && eCursorType <= adOpenStatic )
  {
    hr = m_piRecordset->put_CursorType(eCursorType);
	  if FAILED( hr )
		  ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::CursorType"), hr);
  }

  hr = m_piRecordset->get_CursorType(&m_eCursorType);
	if FAILED( hr )
		ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::CursorType"), hr);

   
	return (m_eCursorType);
}

// Method: EditMode
//   Desc: Return the editing status for the current record.
//
//   Args: none
//
// Return: The current EditModeEnum setting.
//          adEditNone	= 0,
//          adEditInProgress	= 0x1,
//          adEditAdd	= 0x2,
//          adEditDelete	= 0x4
//
EditModeEnum CAxRecordset::EditMode()
{
	VALID_ADO_OBJECT(m_piRecordset);
	EditModeEnum editMode;

  HRESULT hr = m_piRecordset->get_EditMode(&editMode);
	if FAILED( hr )
		ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::EditMode"), hr);

	return (editMode);
}

// Method: IsEOF - Proxy for ADO's EOF method
//   Desc: Determine if the current record pointer is immediately
//         after the last record.
//
//   Args: none
//
// Return: Returns true is the record pointer is located after the
//         last record or if the recordset contains no records.
//         Otherwise false.
//
bool CAxRecordset::IsEOF()
{
	VALID_ADO_OBJECT(m_piRecordset);
	VARIANT_BOOL vbEOF = 0;

	if FAILED( m_piRecordset->get_EOF(&vbEOF) )
		return false;

	return ( vbEOF < 0);
}
 
// Method: Filter a set of records from a current recordset.
//   Desc: Sets/Returns the current filter. A variant value
//         is used which may contain a filter string (VT_BSTR)
//         or a predefined FilterGroupEnum (VT_I2). The client
//         application is responsible for determining, setting
//         and extracting variant values. If a filter string
//         is supplied, use a statement similar to that found
//         in an SQL WHERE clause, i.e. _T("AccountID Like 'BA%'")
//         Call method with no argument to get the current setting.
//
//   Args: *pvtFilter a reference to a _variant_t type.
//          If using FilterGroupEnum, values may be:
//            adFilterNone	= 0,
//            adFilterPendingRecords	= 1,
//            adFilterAffectedRecords	= 2,
//            adFilterFetchedRecords	= 3,
//            adFilterPredicate	= 4,
//            adFilterConflictingRecords	= 5
//
// Return:  Current filter string.
//
_variant_t CAxRecordset::Filter(_variant_t *pvtFilter)
{
	VALID_ADO_OBJECT(m_piRecordset);
  HRESULT hr = S_OK;

  if ( pvtFilter )
  {
    hr = m_piRecordset->put_Filter(*pvtFilter);
	  if FAILED( hr )
		  ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::Filter"), hr);
  }

  VARIANT vtFilter;

  hr = m_piRecordset->get_Filter(&vtFilter);
	if FAILED( hr )
		ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::Filter"), hr);

  return (vtFilter);
}

//Not supported on VS6
#if _MSC_VER > 1200
LPCTSTR CAxRecordset::Index(LPCTSTR lpszIndex)
{
  VALID_ADO_OBJECT(m_piRecordset);
  HRESULT hr = S_OK;

  if ( Supports(adIndex) )
  {
    hr = m_piRecordset->put_Index(_bstr_t(lpszIndex));
    if FAILED( hr )
	    ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::Index"), hr);
  }

  BSTR bstrIndex;
  hr = m_piRecordset->get_Index(&bstrIndex);
  if FAILED( hr )
	  ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::Index"), hr);

#ifdef _UNICODE
  return (_bstr_t(bstrIndex).operator const wchar_t *());
#else
  return (_bstr_t(bstrIndex).operator const char*());
#endif
}
#endif

// Method: LockType
//   Desc: Sets/Returns the type of lock on records during edits.
//         Call method with no argument to return current setting.
//         Read-Write when recordset is closed. Read-only when
//         recordset is open.
//   Args: pLockType - Reference to a LockTypeEnum variable.
//          adLockUnspecified	= -1,
//          adLockReadOnly	= 1,
//          adLockPessimistic	= 2,
//          adLockOptimistic	= 3,
//          adLockBatchOptimistic	= 4
//
// Return:  Current LockTypeEnum setting.
//
LockTypeEnum CAxRecordset::LockType(LockTypeEnum eLockType)
{
	VALID_ADO_OBJECT(m_piRecordset);
  HRESULT hr = S_OK;

  if ( eLockType != adLockUnspecified )
  {
    hr = m_piRecordset->put_LockType(eLockType);
	  if FAILED( hr )
		  ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::Filter"), hr);
  }

	LockTypeEnum eLT;
  hr = m_piRecordset->get_LockType(&eLT);
	if FAILED( hr )
		ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::Filter"), hr);

  return (eLT);
}

// Method: MarshalOptions
//   Desc: Sets/Returns the option as to which records should be
//         marshaled back to the server.
//
//   Args: *pOption - Reference to a MarshalOptionsEnum variable.
//          adMarshalAll	= 0,
//          adMarshalModifiedOnly	= 1
//
// Return: Current MarshalOptionsEnum value.
//
MarshalOptionsEnum CAxRecordset::MarshalOptions(long eOptions)
{
	VALID_ADO_OBJECT(m_piRecordset);
  HRESULT hr = S_OK;

  if ( eOptions >= adMarshalAll && eOptions <= adMarshalModifiedOnly )
  {
    hr = m_piRecordset->put_MarshalOptions((MarshalOptionsEnum)eOptions);
	  if FAILED( hr )
		  ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::MarshalOptions"), hr);
  }

  MarshalOptionsEnum opt;
  hr = m_piRecordset->get_MarshalOptions(&opt);
	if FAILED( hr )
		ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::MarshalOptions"), hr);

  return (opt);
}

// Method: MaxRecords
//   Desc: Sets/Returns the maximum number of records that are
//         returned to a recordset when executing a query.
//         Read-Write when a recordset is closed. Read-only when
//         open. A value of 0 results in all valid records
//         being returned.
//
//   Args: nMaxRecords - Maximum number of records to return.
//
// Return: Current maximum records setting.
//
long CAxRecordset::MaxRecords(long nMaxRecords)
{
	VALID_ADO_OBJECT(m_piRecordset);
  HRESULT hr = S_OK;

  if ( nMaxRecords >= 0 )
  {
    //Can only set max records if recordset is closed
    if ( !_IsOpen() )
    {
      hr = m_piRecordset->put_MaxRecords(nMaxRecords);
      if FAILED( hr )
		    ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::MaxRecords"), hr);
    }
  }

  long max;
  hr = m_piRecordset->get_MaxRecords(&max);
  if FAILED( hr )
		ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::MaxRecords"), hr);

  return (max);
}

// Method: PageCount
//   Desc: Returns the number of logical pages in the recordset.
//
//   Args: none
//
// Return: Number of logical pages.
//
long CAxRecordset::PageCount()
{
	VALID_ADO_OBJECT(m_piRecordset);
  long count;

  HRESULT hr = m_piRecordset->get_PageCount(&count);
  if FAILED( hr )
	  ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::PageCount"), hr);
  
  return (count);
}

// Method: PageSize
//   Desc: Sets/Returns the number of records per logical page.
//
//   Args: nPageSize
//          Number of records in a logical page. Must be greater
//          than 0.
//
// Return: Number of records in a page.
//
long CAxRecordset::PageSize(long nPageSize)
{
	VALID_ADO_OBJECT(m_piRecordset);
  HRESULT hr = S_OK;

  if ( nPageSize > 0 )
  {
    hr = m_piRecordset->put_PageSize(nPageSize);
    if FAILED( hr )
	    ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::PageSize"), hr);
  }

  long pageSize;
  hr = m_piRecordset->get_PageSize(&pageSize);
  if FAILED( hr )
	  ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::PageSize"), hr);

  return (pageSize);
}

// Method: RecordCount
//   Desc: Returns the number of records in the recordset.
//
//   Args: none
//
// Return: Number of records.
//
long CAxRecordset::RecordCount()
{
	VALID_ADO_OBJECT(m_piRecordset);
	long nRecordCount;

  HRESULT hr = m_piRecordset->get_RecordCount(&nRecordCount);
	if FAILED( hr )
	  ThrowAxException(AXLIB_ERROR_NONE, _T("CAxRecordset::RecordCount"), hr);

	return (nRecordCount);
}

// Method: Sort
//   Desc: Set/Return a sort on one or more fields in a recordset.
//
//   Args: lpszSort
//          If non-NULL, identifies the name(s) of the fields
//          on which to sort. The syntax is the same as for
//          the SORT clause in an SQL statement. An empty
//          string value removes any existing sort. The
//          CursorLocation property must be set to adUseClient.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -