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

📄 bdedatabase.cpp

📁 程序1
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	{
		// Throw exception here
		strError.Format("Failed to retrieve data for field %d.",
			nFieldNumber);
	  free(pRecBuf);
		free(pDestBuf);
		throw new CBdeException(dbiResult, nBdeExError, m_szTableName, 
			m_szDatabaseName, strError);
		return "";
	} 

	free(pDestBuf);
  free(pRecBuf);
	return strValue;
} // end of GetFieldAsString



// function to get the field value as an integer
// throws an exception for incompatible data types
LONG CBdeDatabase::GetFieldAsInteger(UINT16 nFieldNumber, BOOL* pbBlank)
{
	// Throw an exception if database is in Edit mode
	if (!CheckValidCursor("Failed to get field value.")) return 0L;
	if (!CheckNotEditMode("Failed to get field value.")) return 0L;

	DBIResult dbiResult = DBIERR_NONE;
	LONG nValue = 0L;
	CString strError;
  CURProps curProps; // Table Properties
	pFLDDesc pFldDesc; // field information
  pBYTE pRecBuf; // Record Buffer
	int nBdeExError = 0;

	
	// get the cursor properties for the table
  dbiResult = DbiGetCursorProps(m_hCursor, &curProps);
	if (dbiResult != DBIERR_NONE)
  {
		strError.Format("Failed to get cursor properties.");
		throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
		return 0L;
  }

	// make sure the field number is valid
	if (nFieldNumber < 1 || nFieldNumber > curProps.iFields)
	{
		strError.Format("Invalid field index %d.  Valid numbers are between 1 and %d.", 
			nFieldNumber, curProps.iFields);
		throw new CBdeException(DBIERR_NONE, BDEEXERR_INVALIDFIELDINDEX,
			m_szTableName, m_szDatabaseName, strError);
		return 0L;
	}


	// allocate memory for the field descriptors
	pFldDesc = (pFLDDesc)malloc(curProps.iFields * sizeof(FLDDesc));
	if (pFldDesc == NULL)
	{
		// Throw exception for DBIERR_NOMEMORY
		throw new CBdeException(DBIERR_NOMEMORY);
		return 0L;
	}

	dbiResult = DbiGetFieldDescs(m_hCursor, pFldDesc);
	if (dbiResult != DBIERR_NONE)
	{
		free(pFldDesc);
		strError.Format("Failed to retrieve field information.");
		throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
		return 0L;
	}

		
	// save the size and type of the field
	UINT16 nSize = pFldDesc[nFieldNumber-1].iLen;
	UINT16 nFieldType = pFldDesc[nFieldNumber-1].iFldType;

	free(pFldDesc); // clean up the field descriptor
	pFldDesc = NULL;


	// allocate the record buffer for the table
  pRecBuf = (pBYTE) malloc(curProps.iRecBufSize * sizeof(BYTE));
	if (pRecBuf == NULL)
  {
		strError.Format("Insufficient memory to get field number %d.", nFieldNumber);
		throw new CBdeException(DBIERR_NOMEMORY, m_szTableName, m_szDatabaseName, strError);
    return 0L;
  }

	// fill the record buffer with information
	dbiResult = DbiGetRecord(m_hCursor, dbiNOLOCK, pRecBuf, NULL);
	if (dbiResult != DBIERR_NONE)
	{
		free(pRecBuf);
		strError.Format("Failed to get record information.");
		throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
	  return 0L;
	}

	// allocate memory for the string buffer to recieve data
	pBYTE pDestBuf = (pBYTE)malloc(nSize*sizeof BYTE);
	memset(pDestBuf, 0, nSize*sizeof BYTE);


	if (pDestBuf == NULL)
	{
		// Throw memory exception here
		free(pRecBuf);
		strError.Format("Failed to get record information.");
		throw new CBdeException(DBIERR_NOMEMORY, m_szTableName, m_szDatabaseName, strError);
		return 0L;
	}
	
	// get the actual field value

	switch (nFieldType)
	{
		case fldINT16:
		{
			INT16 n;
			dbiResult = DbiGetField(m_hCursor, nFieldNumber, pRecBuf, (pBYTE)&n, pbBlank);
			if (*pbBlank == FALSE)
			{
				nValue = (LONG)n;
			}
			break;
		}
		case fldUINT16:
		{
			UINT16 n;
			dbiResult = DbiGetField(m_hCursor, nFieldNumber, pRecBuf, (pBYTE)&n, pbBlank);
			if (*pbBlank == FALSE)
			{
				nValue = (LONG)n;
			}
			break;
		}
		case fldINT32:
		{
			INT32 n;
			dbiResult = DbiGetField(m_hCursor, nFieldNumber, pRecBuf, (pBYTE)&n, pbBlank);
			if (*pbBlank == FALSE)
			{
				nValue = (LONG)n;
			}
			break;
		}
		case fldUINT32:
		{
			UINT32 n;
			dbiResult = DbiGetField(m_hCursor, nFieldNumber, pRecBuf, (pBYTE)&n, pbBlank);
			if (*pbBlank == FALSE)
			{
				nValue = (LONG)n;
			}
			break;
		}
		case fldFLOAT: // need to handl floats for dBase files
		{
			double f;
			dbiResult = DbiGetField(m_hCursor, nFieldNumber, pRecBuf, (pBYTE)&f, pbBlank);
			if (*pbBlank == FALSE)
			{
				nValue = (LONG)f;
			}
			break;
		}
		case fldBOOL:
		{
			dbiResult = DbiGetField(m_hCursor, nFieldNumber, pRecBuf, (pBYTE)pDestBuf, pbBlank);
			if (*pbBlank == FALSE)
      {
        if (*(BYTE*)pDestBuf == 0)
        {
					nValue = 0L;
        }
        else
        {
					nValue = 1L;
        }
      }
			break;
		}
		default:
		{
			nBdeExError = BDEEXERR_FIELDNOTINTEGER;
		}
	} // end of swtich

	// process the final exceptions
	if (dbiResult != DBIERR_NONE || nBdeExError != 0)
	{
		// Throw exception here
		strError.Format("Failed to retrieve data for field %d.",
			nFieldNumber);
	  free(pRecBuf);
		free(pDestBuf);
		throw new CBdeException(dbiResult, nBdeExError, m_szTableName, 
			m_szDatabaseName, strError);
		return 0L;
	} 

	free(pDestBuf);
  free(pRecBuf);
	return nValue;
} // end of GetFieldAsInteger



// function to get the field value as a float
// works on float, currency, BCD, and all integer types
double CBdeDatabase::GetFieldAsFloat(UINT16 nFieldNumber, BOOL* pbBlank)
{
	// Throw an exception if database is in Edit mode
	if (!CheckValidCursor()) return 0.0;
	if (!CheckNotEditMode()) return 0.0;

	DBIResult dbiResult = DBIERR_NONE;
	double fValue = 0.0;
	CString strError;
  CURProps curProps; // Table Properties
	pFLDDesc pFldDesc; // field information
  pBYTE pRecBuf; // Record Buffer
	int nBdeExError = 0;

	
	// get the cursor properties for the table
  dbiResult = DbiGetCursorProps(m_hCursor, &curProps);
	if (dbiResult != DBIERR_NONE)
  {
		strError.Format("Failed to get cursor properties.");
		throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
		return 0.0;
  }

	// make sure the field number is valid
	if (nFieldNumber < 1 || nFieldNumber > curProps.iFields)
	{
		strError.Format("Invalid field index %d.  Valid numbers are between 1 and %d.", 
			nFieldNumber, curProps.iFields);
		throw new CBdeException(DBIERR_NONE, BDEEXERR_INVALIDFIELDINDEX,
			m_szTableName, m_szDatabaseName, strError);
		return 0.0;
	}

	// allocate memory for the field descriptors
	pFldDesc = (pFLDDesc)malloc(curProps.iFields * sizeof(FLDDesc));
	if (pFldDesc == NULL)
	{
		// Throw exception for DBIERR_NOMEMORY
		throw new CBdeException(DBIERR_NOMEMORY);
		return 0.0;
	}

	dbiResult = DbiGetFieldDescs(m_hCursor, pFldDesc);
	if (dbiResult != DBIERR_NONE)
	{
		free(pFldDesc);
		strError.Format("Failed to retrieve field information.");
		throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
		return 0.0;
	}

		
	// save the size and type of the field
	UINT16 nSize = pFldDesc[nFieldNumber-1].iLen;
	UINT16 nFieldType = pFldDesc[nFieldNumber-1].iFldType;

	free(pFldDesc); // clean up the field descriptor
	pFldDesc = NULL;


	// allocate the record buffer for the table
  pRecBuf = (pBYTE) malloc(curProps.iRecBufSize * sizeof(BYTE));
	if (pRecBuf == NULL)
  {
		strError.Format("Insufficient memory to get field number %d.", nFieldNumber);
		throw new CBdeException(DBIERR_NOMEMORY, m_szTableName, m_szDatabaseName, strError);
    return 0.0;
  }

	// fill the record buffer with information
	dbiResult = DbiGetRecord(m_hCursor, dbiNOLOCK, pRecBuf, NULL);
	if (dbiResult != DBIERR_NONE)
	{
		free(pRecBuf);
		strError.Format("Failed to get record information.");
		throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
	  return 0.0;
	}

	// allocate memory for the string buffer to recieve data
	pBYTE pDestBuf = (pBYTE)malloc(nSize*sizeof BYTE);
	memset(pDestBuf, 0, nSize*sizeof BYTE);


	if (pDestBuf == NULL)
	{
		// Throw memory exception here
		free(pRecBuf);
		strError.Format("Failed to get record information.");
		throw new CBdeException(DBIERR_NOMEMORY, m_szTableName, m_szDatabaseName, strError);
		return 0.0;
	}
	
	// get the actual field value
	switch (nFieldType)
	{
		case fldFLOAT:
		{
			double f;
			dbiResult = DbiGetField(m_hCursor, nFieldNumber, pRecBuf, (pBYTE)&f, pbBlank);
			if (*pbBlank == FALSE)
			{
				fValue = f;
			}
			break;
		}
		case fldBCD:
		{
			dbiResult = DbiGetField(m_hCursor, nFieldNumber, pRecBuf, 
				(pBYTE)pDestBuf, pbBlank);
			if (*pbBlank == FALSE)
			{
				double lfFloat;
				DbiBcdToFloat((FMTBcd *)pDestBuf,	&lfFloat);
				fValue = lfFloat;
			}
			break;
		}
		case fldINT16:
		{
			INT16 n;
			dbiResult = DbiGetField(m_hCursor, nFieldNumber, pRecBuf, (pBYTE)&n, pbBlank);
			if (*pbBlank == FALSE)
			{
				fValue = (double)n;
			}
			break;
		}
		case fldUINT16:
		{
			UINT16 n;
			dbiResult = DbiGetField(m_hCursor, nFieldNumber, pRecBuf, (pBYTE)&n, pbBlank);
			if (*pbBlank == FALSE)
			{
				fValue = (double)n;
			}
			break;
		}
		case fldINT32:
		{
			INT32 n;
			dbiResult = DbiGetField(m_hCursor, nFieldNumber, pRecBuf, (pBYTE)&n, pbBlank);
			if (*pbBlank == FALSE)
			{
				fValue = (double)n;
			}
			break;
		}
		case fldUINT32:
		{
			UINT32 n;
			dbiResult = DbiGetField(m_hCursor, nFieldNumber, pRecBuf, (pBYTE)&n, pbBlank);
			if (*pbBlank == FALSE)
			{
				fValue = (double)n;
			}
			break;
		}
		default:
		{
			nBdeExError = BDEEXERR_FIELDNOTFLOAT;
		}
	} // end of swtich


	if (dbiResult != DBIERR_NONE || nBdeExError != 0)
	{
		// Throw exception here
		strError.Format("Failed to retrieve data for field %d.",
			nFieldNumber);
	  free(pRecBuf);
		free(pDestBuf);
		throw new CBdeException(dbiResult, nBdeExError, m_szTableName, 
			m_szDatabaseName, strError);
		return 0.0;
	} 

	free(pDestBuf);
  free(pRecBuf);
	return fValue;
} // end of GetFieldAsFloat




COleDateTime CBdeDatabase::GetFieldAsDate(UINT16 nFieldNumber, BOOL* pbBlank)
{
	DBIResult dbiResult = DBIERR_NONE;
	int nBdeExError = 0;
	CString strError;
  CURProps curProps; // Table Properties
	pFLDDesc pFldDesc; // field information
  pBYTE pRecBuf; // Record Buffer
	COleDateTime dtRetVal = 0.0;
	dtRetVal.m_status = COleDateTime::null; 

	// Throw an exception if database is in Edit mode
	if (!CheckValidCursor("Failed to get field value.")) return dtRetVal;
	if (!CheckNotEditMode("Failed to get field value.")) return dtRetVal;

	
	// get the cursor properties for the table
  dbiResult = DbiGetCursorProps(m_hCursor, &curProps);
	if (dbiResult != DBIERR_NONE)
  {
		strError.Format("Failed to get cursor properties.");
		throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
		return dtRetVal;
  }

	// make sure the field number is valid
	if (nFieldNumber < 1 || nFieldNumber > curProps.iFields)
	{
		strError.Format("Invalid field index %d.  Valid numbers are between 1 and %d.", 
			nFieldNumber, curProps.iFields);
		throw new CBdeException(DBIERR_NONE, BDEEXERR_INVALIDFIELDINDEX,
			m_szTableName, m_szDatabaseName, strError);
		return dtRetVal;
	}

	// allocate memory for the field descriptors
	pFldDesc = (pFLDDesc)malloc(curProps.iFields * sizeof(FLDDesc));
	if (pFldDesc == NULL)
	{
		// Throw exception for DBIERR_NOMEMORY
		throw new CBdeException(DBIERR_NOMEMORY);
		return dtRetVal;
	}

	// get the field descriptors to get the type of the field
	dbiResult = DbiGetFieldDescs(m_hCursor, pFldDesc);
	if (dbiResult != DBIERR_NONE)
	{
		free(pFldDesc);
		strError.Format("Failed to retrieve field information.");
		throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
		return dtRetVal;
	}

	// save the size and type of the field
	UINT16 nSize = pFldDesc[nFieldNumber-1].iLen;
	UINT16 nFieldType = pFldDesc[nFieldNumber-1].iFldType;

	free(pFldDesc); // clean up the field descriptor
	pFldDesc = NULL;


	// allocate the record buffer for the table
  pRecBuf = (pBYTE) malloc(curProps.iRecBufSize * sizeof(BYTE));
	if (pRecBuf == NULL)
  {
		strError.Format("Insufficient memory to get field number %d.", nFieldNumber);
		throw new CBdeException(DBIERR_NOMEMORY, m_szTableName, m_szDatabaseName, strError);
    return dtRetVal;
  }

	// fill the record buffer with information
	dbiResult = DbiGetRecord(m_hCursor, dbiNOLOCK, pRecBuf, NULL);
	if (dbiResult != DBIERR_NONE)
	{
		free(pRecBuf);
		strError.Format("Failed to get record information.");
		throw new CBdeException(dbiResult, m_szTableName, m_szDatabaseName, strError);
	  return dtRetVal;

⌨️ 快捷键说明

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