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

📄 odbcrecordset.cpp

📁 几个老外写的源代码大家快快下载
💻 CPP
📖 第 1 页 / 共 2 页
字号:
double	CDBField::AsDouble()	const
{
	if( IsNull() )
		return	0.0;

	switch( m_dwType ) {
	case	DBVT_NULL:
				return	0.0;

	case	DBVT_BOOL:
				return	m_boolVal ? 1.0 : 0.0;

	case	DBVT_UCHAR:
				return	(double)m_chVal;

	case	DBVT_SHORT:
				return	(double)m_iVal;

	case	DBVT_LONG:
				return	(double)m_lVal;

	case	DBVT_SINGLE:
				return	(double)m_fltVal;

	case	DBVT_DOUBLE:
				return	m_dblVal;

	case	DBVT_DATE:
				//	Cannot convert date to double
				ASSERT( FALSE );
				break;

	case	DBVT_STRING:
				ASSERT( m_pstring != NULL );
				return	atof( *m_pstring );

	case	DBVT_BINARY:
				//	Cannot conver long binary to double
				ASSERT( FALSE );
				break;
	}
	//	Undefined data type
	ASSERT( FALSE );
	return	0.0;
}

COleDateTime	CDBField::AsDate()	const
{
	COleDateTime	date;
	if( IsNull() ) {
		date.SetStatus( COleDateTime::null );
		return	date;
	}

	switch( m_dwType ) {
	case	DBVT_NULL:
			date.SetStatus( COleDateTime::null );
			return	date;

	case	DBVT_BOOL:
			date.SetStatus( COleDateTime::invalid );
			return	date;

	case	DBVT_UCHAR:
			date.SetStatus( COleDateTime::invalid );
			return	date;

	case	DBVT_SHORT:
			return	COleDateTime( (time_t)m_iVal );

	case	DBVT_LONG:
			return	COleDateTime( (time_t)m_lVal );

	case	DBVT_SINGLE:
			return	COleDateTime( (time_t)m_fltVal );

	case	DBVT_DOUBLE:
			return	COleDateTime( (time_t)m_dblVal );

	case	DBVT_DATE:
			ASSERT( m_pdate != NULL );
			return	COleDateTime(	m_pdate->year, m_pdate->month, m_pdate->day,
									m_pdate->hour, m_pdate->minute, m_pdate->second );

	case	DBVT_STRING:
			ASSERT( m_pstring != NULL );
			date.ParseDateTime( *m_pstring );
			return	date;

	case	DBVT_BINARY:
			//	Cannot conver long binary to date
			ASSERT( FALSE );
			break;
	}
	//	Undefined data type
	ASSERT( FALSE );
	date.SetStatus( COleDateTime::invalid );
	return	date;
}

CString		CDBField::AsString()	const
{
	CString	cValue;

	switch( m_dwType ) {
	case	DBVT_NULL:
			return	cValue;

	case	DBVT_BOOL:
			return	CString( m_boolVal ? "T" : "F" );

	case	DBVT_UCHAR:
			return	CString( m_chVal );

	case	DBVT_SHORT:
			cValue.Format( "%hd", m_iVal );
			return	cValue;

	case	DBVT_LONG:
			cValue.Format( "%ld", m_lVal );
			return	cValue;

	case	DBVT_SINGLE:
			cValue.Format( "%f", m_fltVal );
			return	cValue;

	case	DBVT_DOUBLE:
			cValue.Format( "%f", m_dblVal );
			return	cValue;

	case	DBVT_DATE:
		{
			ASSERT( m_pdate != NULL );
			COleDateTime	date( m_pdate->year, m_pdate->month, m_pdate->day,
								  m_pdate->hour, m_pdate->minute, m_pdate->second );
			return	date.Format();
		}
	case	DBVT_STRING:
			ASSERT( m_pstring != NULL );
			return	*m_pstring;

	case	DBVT_BINARY:
			ASSERT( m_pbinary != NULL );
			::BinaryToString( cValue, *m_pbinary, true );
			return	cValue;
	}
	//	Undefined data type
	ASSERT( FALSE );
	return	cValue;
}

CLongBinary*	CDBField::AsBinary()	const
{
	switch( m_dwType ) {
	case	DBVT_NULL:
			return	NULL;

	case	DBVT_BOOL:
	case	DBVT_UCHAR:
	case	DBVT_SHORT:
	case	DBVT_LONG:
	case	DBVT_SINGLE:
	case	DBVT_DOUBLE:
	case	DBVT_DATE:
	case	DBVT_STRING:
			//	Cannot convert to long binary
			ASSERT( FALSE );
			break;

	case	DBVT_BINARY:
			return	m_pbinary;
	}
	//	Undefined data type
	ASSERT( FALSE );
	return	m_pbinary;
}
////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////
//	Assignment operators
////////////////////////////////////////////////////////////////

CDBField& CDBField::operator =( const bool bVal )
{
	switch( m_dwType ) {
	case	DBVT_NULL:
			//	Undefined data type
			ASSERT( FALSE );

	case	DBVT_BOOL:
			m_boolVal = bVal;
			return	*this;

	case	DBVT_UCHAR:
			m_chVal = (bVal) ? 'T' : 'F';
			return	*this;

	case	DBVT_SHORT:
			m_iVal = (bVal) ? 1 : 0;
			return	*this;

	case	DBVT_LONG:
			m_lVal = (bVal) ? 1 : 0;
			return	*this;

	case	DBVT_SINGLE:
			m_fltVal = (float)((bVal) ? 1.0 : 0.0);
			return	*this;

	case	DBVT_DOUBLE:
			m_dblVal = (double)((bVal) ? 1.0 : 0.0);
			return	*this;

	case	DBVT_DATE:
			//	Cannot convert to datetime
			ASSERT( FALSE );
			return	*this;

	case	DBVT_STRING:
			ASSERT( m_pstring != NULL );
			m_pstring->Format( "%c", (bVal) ? 'T' : 'F' );
			return	*this;

	case	DBVT_BINARY:
			//	CRecordset does not support writing to CLongBinary fields
			ASSERT( FALSE );
			return	*this;
	}
	//	Undefined data type
	ASSERT( FALSE );
	return	*this;
}
CDBField& CDBField::operator =( const char chVal )
{
	return	operator =( (unsigned char) chVal );
}
CDBField& CDBField::operator =( const unsigned char chVal )
{
	switch( m_dwType ) {
	case	DBVT_NULL:
			//	Undefined data type
			ASSERT( FALSE );

	case	DBVT_BOOL:
			m_boolVal = (chVal == 'T' || chVal == '1');
			return	*this;

	case	DBVT_UCHAR:
			m_chVal = (unsigned char)chVal;
			return	*this;

	case	DBVT_SHORT:
			m_iVal = (short)chVal;
			return	*this;

	case	DBVT_LONG:
			m_lVal = (long)chVal;
			return	*this;

	case	DBVT_SINGLE:
			m_fltVal = (float)chVal;
			return	*this;

	case	DBVT_DOUBLE:
			m_dblVal = (double)chVal;
			return	*this;

	case	DBVT_DATE:
			//	Cannot convert to datetime
			ASSERT( FALSE );
			return	*this;

	case	DBVT_STRING:
			ASSERT( m_pstring != NULL );
			m_pstring->Format( "%c", chVal );
			return	*this;

	case	DBVT_BINARY:
			//	CRecordset does not support writing to CLongBinary fields
			ASSERT( FALSE );
			return	*this;
	}
	//	Undefined data type
	ASSERT( FALSE );
	return	*this;
}
CDBField& CDBField::operator =( const short sVal )
{
	switch( m_dwType ) {
	case	DBVT_NULL:
			//	Undefined data type
			ASSERT( FALSE );

	case	DBVT_BOOL:
			m_boolVal = (sVal != 0);
			return	*this;

	case	DBVT_UCHAR:
			m_chVal = (unsigned char)sVal;
			return	*this;

	case	DBVT_SHORT:
			m_iVal = (short)sVal;
			return	*this;

	case	DBVT_LONG:
			m_lVal = (long)sVal;
			return	*this;

	case	DBVT_SINGLE:
			m_fltVal = (float)sVal;
			return	*this;

	case	DBVT_DOUBLE:
			m_dblVal = (double)sVal;
			return	*this;

	case	DBVT_DATE:
			//	Cannot convert to datetime
			ASSERT( FALSE );
			return	*this;

	case	DBVT_STRING:
			ASSERT( m_pstring != NULL );
			m_pstring->Format( "%hd", sVal );
			return	*this;

	case	DBVT_BINARY:
			//	CRecordset does not support writing to CLongBinary fields
			ASSERT( FALSE );
			return	*this;
	}
	//	Undefined data type
	ASSERT( FALSE );
	return	*this;
}
CDBField& CDBField::operator =( const int iVal )
{
	switch( m_dwType ) {
	case	DBVT_NULL:
			//	Undefined data type
			ASSERT( FALSE );

	case	DBVT_BOOL:
			m_boolVal = (iVal != 0);
			return	*this;

	case	DBVT_UCHAR:
			m_chVal = (unsigned char)iVal;
			return	*this;

	case	DBVT_SHORT:
			m_iVal = (short)iVal;
			return	*this;

	case	DBVT_LONG:
			m_lVal = (long)iVal;
			return	*this;

	case	DBVT_SINGLE:
			m_fltVal = (float)iVal;
			return	*this;

	case	DBVT_DOUBLE:
			m_dblVal = (double)iVal;
			return	*this;

	case	DBVT_DATE:
			//	Cannot convert to datetime
			ASSERT( FALSE );
			return	*this;

	case	DBVT_STRING:
			ASSERT( m_pstring != NULL );
			m_pstring->Format( "%d", iVal );
			return	*this;

	case	DBVT_BINARY:
			//	CRecordset does not support writing to CLongBinary fields
			ASSERT( FALSE );
			return	*this;
	}
	//	Undefined data type
	ASSERT( FALSE );
	return	*this;
}
CDBField& CDBField::operator =( const long lVal )
{
	switch( m_dwType ) {
	case	DBVT_NULL:
			//	Undefined data type
			ASSERT( FALSE );

	case	DBVT_BOOL:
			m_boolVal = (lVal != 0);
			return	*this;

	case	DBVT_UCHAR:
			m_chVal = (unsigned char)lVal;
			return	*this;

	case	DBVT_SHORT:
			m_iVal = (short)lVal;
			return	*this;

	case	DBVT_LONG:
			m_lVal = (long)lVal;
			return	*this;

	case	DBVT_SINGLE:
			m_fltVal = (float)lVal;
			return	*this;

	case	DBVT_DOUBLE:
			m_dblVal = (double)lVal;
			return	*this;

	case	DBVT_DATE:
			//	Cannot convert to datetime
			ASSERT( FALSE );
			return	*this;

	case	DBVT_STRING:
			ASSERT( m_pstring != NULL );
			m_pstring->Format( "%ld", lVal );
			return	*this;

	case	DBVT_BINARY:
			//	CRecordset does not support writing to CLongBinary fields
			ASSERT( FALSE );
			return	*this;
	}
	//	Undefined data type
	ASSERT( FALSE );
	return	*this;
}
CDBField& CDBField::operator =( const float fltVal )
{
	switch( m_dwType ) {
	case	DBVT_NULL:
			//	Undefined data type
			ASSERT( FALSE );

	case	DBVT_BOOL:
			m_boolVal = (fltVal != 0.0);
			return	*this;

	case	DBVT_UCHAR:
			m_chVal = (unsigned char)fltVal;
			return	*this;

	case	DBVT_SHORT:
			m_iVal = (short)fltVal;
			return	*this;

	case	DBVT_LONG:
			m_lVal = (long)fltVal;
			return	*this;

	case	DBVT_SINGLE:
			m_fltVal = (float)fltVal;
			return	*this;

	case	DBVT_DOUBLE:
			m_dblVal = (double)fltVal;
			return	*this;

	case	DBVT_DATE:
			//	Cannot convert to datetime
			ASSERT( FALSE );
			return	*this;

	case	DBVT_STRING:
			ASSERT( m_pstring != NULL );
			m_pstring->Format( "%f", fltVal );
			return	*this;

	case	DBVT_BINARY:
			//	CRecordset does not support writing to CLongBinary fields
			ASSERT( FALSE );
			return	*this;
	}
	//	Undefined data type
	ASSERT( FALSE );
	return	*this;
}
CDBField& CDBField::operator =( const double dblVal )
{
	switch( m_dwType ) {
	case	DBVT_NULL:
			//	Undefined data type
			ASSERT( FALSE );

	case	DBVT_BOOL:
			m_boolVal = (dblVal != 0.0);
			return	*this;

	case	DBVT_UCHAR:
			m_chVal = (unsigned char)dblVal;
			return	*this;

	case	DBVT_SHORT:
			m_iVal = (short)dblVal;
			return	*this;

	case	DBVT_LONG:
			m_lVal = (long)dblVal;
			return	*this;

	case	DBVT_SINGLE:
			m_fltVal = (float)dblVal;
			return	*this;

	case	DBVT_DOUBLE:
			m_dblVal = (double)dblVal;
			return	*this;

	case	DBVT_DATE:
			//	Cannot convert to datetime
			ASSERT( FALSE );
			return	*this;

	case	DBVT_STRING:
			ASSERT( m_pstring != NULL );
			m_pstring->Format( "%f", dblVal );
			return	*this;

	case	DBVT_BINARY:
			//	CRecordset does not support writing to CLongBinary fields
			ASSERT( FALSE );
			return	*this;
	}
	//	Undefined data type
	ASSERT( FALSE );
	return	*this;
}
CDBField& CDBField::operator =( const COleDateTime& dtVal )
{
	switch( m_dwType ) {
	case	DBVT_NULL:
			//	Undefined data type
			ASSERT( FALSE );

	case	DBVT_BOOL:
	case	DBVT_UCHAR:
	case	DBVT_SHORT:
	case	DBVT_LONG:
	case	DBVT_SINGLE:
	case	DBVT_DOUBLE:
			//	Cannot convert to the current data type
			ASSERT( FALSE );
			return	*this;

	case	DBVT_DATE:
			ASSERT( m_pdate != NULL );
			m_pdate->year	= dtVal.GetYear();
			m_pdate->month	= dtVal.GetMonth();
			m_pdate->day	= dtVal.GetDay();
			m_pdate->hour	= dtVal.GetHour();
			m_pdate->minute	= dtVal.GetMinute();
			m_pdate->second = dtVal.GetSecond();
			m_pdate->fraction = 0;
			return	*this;

	case	DBVT_STRING:
			ASSERT( m_pstring != NULL );
			*m_pstring = dtVal.Format();
			return	*this;

	case	DBVT_BINARY:
			//	CRecordset does not support writing to CLongBinary fields
			ASSERT( FALSE );
			return	*this;
	}
	//	Undefined data type
	ASSERT( FALSE );
	return	*this;
}
CDBField& CDBField::operator =( const CString& cVal )
{
	return	operator =( (LPCTSTR)cVal );
}
CDBField& CDBField::operator =( const LPCTSTR szVal )
{
	switch( m_dwType ) {
	case	DBVT_NULL:
			//	Undefined data type
			ASSERT( FALSE );

	case	DBVT_BOOL:
			m_boolVal = (szVal != NULL || atoi( szVal ) != 0 );
			return	*this;

	case	DBVT_UCHAR:
			m_chVal = (unsigned char)szVal[0];
			return	*this;

	case	DBVT_SHORT:
			m_iVal = (short)atoi( szVal );
			return	*this;

	case	DBVT_LONG:
			m_lVal = (long)atol( szVal );
			return	*this;

	case	DBVT_SINGLE:
			m_fltVal = (float)atof( szVal );
			return	*this;

	case	DBVT_DOUBLE:
			m_dblVal = (double)atof( szVal );
			return	*this;

	case	DBVT_DATE:
		{
			ASSERT( m_pdate != NULL );
			COleDateTime	dt;
			dt.ParseDateTime( szVal );
			m_pdate->year	= dt.GetYear();
			m_pdate->month	= dt.GetMonth();
			m_pdate->day	= dt.GetDay();
			m_pdate->hour	= dt.GetHour();
			m_pdate->minute	= dt.GetMinute();
			m_pdate->second = dt.GetSecond();
			m_pdate->fraction = 0;
			return	*this;
		}

	case	DBVT_STRING:
			ASSERT( m_pstring != NULL );
			*m_pstring = szVal;
			return	*this;

	case	DBVT_BINARY:
			//	CRecordset does not support writing to CLongBinary fields
			ASSERT( FALSE );
			return	*this;
	}
	//	Undefined data type
	ASSERT( FALSE );
	return	*this;
}
////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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