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

📄 arsdb.cpp

📁 自己编写的一个数据库
💻 CPP
📖 第 1 页 / 共 2 页
字号:

		// Obtain pointer to a particular field
		hr = pFields->get_Item(varIndex, &pField);
		if (FAILED(hr))
			AfxThrowARSException(ARSMSG_ERR_ADO_RECSET_FIELD_NOTFOUND, ARSMSG_ERR_NO_ERROR, pConnection);

		// Get field value
		hr = pField->get_Value(value);
		if (FAILED(hr))
			AfxThrowARSException(ARSMSG_ERR_ADO_RECSET_CANTSETFIELD, ARSMSG_ERR_NO_ERROR, pConnection);

		pField->Release();
		pFields->Release();

	}
	catch(...) {

		if (pField)
			pField->Release();
		if (pFields)
			pFields->Release();

		throw;
	}
}

CString CARSRecordset::ConvertVariantToString(VARIANT *value)
{
    float cy;
    CString str;
    COleVariant v;

    if ((value->vt != VT_BSTR) && (value->vt != VT_EMPTY) && (value->vt != VT_NULL))
       v.ChangeType(VT_BSTR, value);

    if (!((v.vt == VT_EMPTY) || (v.vt == VT_NULL))) 
        str = v.bstrVal;
    else if (value->vt != VT_BSTR)
        str = CString("<NULL>");
    else
        str = value->bstrVal;

    if ((value->vt == VT_CY) && (str != CString("<NULL>")) && !str.IsEmpty()) {
        sscanf(str, "%f", &cy);
        str.Format("%.2f", cy);
    }

    return str;
}

// void CARSRecordset::SetFieldValue(LPCSTR fieldName, VARIANT *value)
// Set Value of the field, named fieldName
void CARSRecordset::SetFieldValue(LPCSTR fieldName, VARIANT *value)
{
	HRESULT hr;
	COleVariant varIndex;
    ADOFields	*pFields = NULL;
	ADOField	*pField = NULL;

	if (!IsOpen())
		AfxThrowARSException(ARSMSG_ERR_ADO_RECSET_NOTOPEN);

	if (!value)
		AfxThrowARSException(ARSMSG_ERR_GEN_INVALID_PARAMETER);

	// We need a try block so that we can Release pField and pFields and then
	// we can rethrow exception
	try {

		// Obtain pointer to fields collection
		varIndex.SetString(fieldName, VT_BSTR);
		hr = pSet->get_Fields(&pFields);
		if (FAILED(hr))
			AfxThrowARSException(ARSMSG_ERR_ADO_RECSET_NOFIELDS, ARSMSG_ERR_NO_ERROR, pConnection);

		// Obtain pointer to a particular field
		hr = pFields->get_Item(varIndex, &pField);
		if (FAILED(hr))
			AfxThrowARSException(ARSMSG_ERR_ADO_RECSET_FIELD_NOTFOUND, ARSMSG_ERR_NO_ERROR, pConnection);

		// Update field value
		hr = pField->put_Value(*value);
		if (FAILED(hr))
			AfxThrowARSException(ARSMSG_ERR_ADO_RECSET_CANTSETFIELD, ARSMSG_ERR_NO_ERROR, pConnection);

		pField->Release();
		pFields->Release();

	}
	catch(...) {

		if (pField)
			pField->Release();
		if (pFields)
			pFields->Release();

		throw;
	}
}

// CARSRecord::AddNew()
// Populate appropriate fields of the collection and then call this
// function to create a new record
void CARSRecordset::AddNew()
{
	HRESULT hr;
    VARIANT rgvFields;
	VARIANT rgvValues;

	rgvFields.vt = VT_ERROR;
	rgvFields.scode = DISP_E_PARAMNOTFOUND;

	rgvValues.vt = VT_ERROR;
	rgvValues.scode = DISP_E_PARAMNOTFOUND;

	hr = pSet->AddNew(rgvFields, rgvValues);
	if (FAILED(hr))
		AfxThrowARSException(ARSMSG_ERR_ADO_RECSET_CANTADD, ARSMSG_ERR_NO_ERROR, pConnection);
    CopyDataFromDatabase();
}


// BOOL CARSRecordset::operator()(LPCSTR fieldName, CString &fd, BOOL doWrite, BOOL isNULL = FALSE)
// Get (if doWrite = FALSE) or Set (if dowrite = TRUE) value of String Field
// if isNULL = TRUE during write than set value to NULL
// returns TRUE if value retreived is not NULL
BOOL CARSRecordset::operator()(LPCSTR fieldName, CString &fd, BOOL doWrite, BOOL isNULL)
{
    BOOL ret = TRUE;
    COleVariant v;

    if (doWrite) {
        if (isNULL)
            v.vt = VT_NULL;
        else
            v.SetString((LPCSTR )fd, VT_BSTR);
        SetFieldValue(fieldName, &v);
    }
    else {
        GetFieldValue(fieldName, &v);
        
        if ((v.vt == VT_EMPTY) || (v.vt == VT_NULL)) {
            ret = FALSE;
            fd.Empty();
        }
        else {
            if ((v.vt != VT_BSTR) && (v.vt != VT_EMPTY) && (v.vt != VT_NULL))
                AfxThrowARSException(ARSMSG_ERR_ADO_RECSET_INVAL_FIELDTYPE);
            fd = v.bstrVal;
        }
    }

    return ret;
}

// BOOL CARSRecordset::operator()(LPCSTR fieldName, long&, BOOL doWrite, BOOL isNULL)
// Get (if doWrite = FALSE) or Set (if dowrite = TRUE) value of Integer4 Field
BOOL CARSRecordset::operator()(LPCSTR fieldName, long& fd, BOOL doWrite, BOOL isNULL)
{
    BOOL ret = TRUE;
    COleVariant v;

    if (doWrite) {
        if (isNULL) 
            v.vt = VT_NULL;
        else {
            v.vt = VT_I4;
            v.lVal = fd;
        }
        SetFieldValue(fieldName, &v);
    }
    else {
        GetFieldValue(fieldName, &v);

        if ((v.vt == VT_EMPTY) || (v.vt == VT_NULL))
            ret = FALSE;

        if ((v.vt != VT_I4) && (v.vt != VT_EMPTY) && (v.vt != VT_NULL))
            AfxThrowARSException(ARSMSG_ERR_ADO_RECSET_INVAL_FIELDTYPE);
        fd = v.lVal;
    }

    return ret;
}

// BOOL CARSRecordset::operator()(LPCSTR fieldName, short&, BOOL doWrite, BOOL isNULL)
// Get (if doWrite = FALSE) or Set (if dowrite = TRUE) value of Integer2 Field
BOOL CARSRecordset::operator()(LPCSTR fieldName, short& fd, BOOL doWrite, BOOL isNULL)
{
    BOOL ret = TRUE;
    COleVariant v;

    if (doWrite) {
        if (isNULL) 
            v.vt = VT_NULL;
        else {
            v.vt = VT_I2;
            v.iVal = fd;
        }
        SetFieldValue(fieldName, &v);
    }
    else {
        GetFieldValue(fieldName, &v);

        if ((v.vt == VT_EMPTY) || (v.vt == VT_NULL))
            ret = FALSE;

        if ((v.vt != VT_I2) && (v.vt != VT_EMPTY) && (v.vt != VT_NULL))
            AfxThrowARSException(ARSMSG_ERR_ADO_RECSET_INVAL_FIELDTYPE);
        fd = v.iVal;
    }

    return ret;
}


// BOOL CARSRecordset::operator()(LPCSTR fieldName, double&, BOOL doWrite, BOOL isNULL)
// Get (if doWrite = FALSE) or Set (if dowrite = TRUE) value of Real8 Field
BOOL CARSRecordset::operator()(LPCSTR fieldName, double& fd, BOOL doWrite, BOOL isNULL)
{
    BOOL ret = TRUE;
    COleVariant v;

    if (doWrite) {
        if (isNULL) 
            v.vt = VT_NULL;
        else {
            v.vt = VT_R8;
            v.dblVal = fd;
        }
        SetFieldValue(fieldName, &v);
    }
    else {
        GetFieldValue(fieldName, &v);

        if ((v.vt == VT_EMPTY) || (v.vt == VT_NULL))
            ret = FALSE;

        if ((v.vt != VT_R8) && (v.vt != VT_EMPTY) && (v.vt != VT_NULL))
            AfxThrowARSException(ARSMSG_ERR_ADO_RECSET_INVAL_FIELDTYPE);
        fd = v.dblVal;
    }

    return ret;
}


// BOOL CARSRecordset::operator()(LPCSTR fieldName, float&, BOOL doWrite, BOOL isNULL)
// Get (if doWrite = FALSE) or Set (if dowrite = TRUE) value of Real4 Field
BOOL CARSRecordset::operator()(LPCSTR fieldName, float& fd, BOOL doWrite, BOOL isNULL)
{
    BOOL ret = TRUE;
    COleVariant v;

    if (doWrite) {
        if (isNULL) 
            v.vt = VT_NULL;
        else {
            v.vt = VT_R4;
            v.fltVal = fd;
        }
        SetFieldValue(fieldName, &v);
    }
    else {
        GetFieldValue(fieldName, &v);

        if ((v.vt == VT_EMPTY) || (v.vt == VT_NULL))
            ret = FALSE;

        if ((v.vt != VT_R4) && (v.vt != VT_EMPTY) && (v.vt != VT_NULL))
            AfxThrowARSException(ARSMSG_ERR_ADO_RECSET_INVAL_FIELDTYPE);
        fd = v.fltVal;
    }

    return ret;
}


// BOOL CARSRecordset::operator()(LPCSTR fieldName, float&, BOOL doWrite, BOOL isNULL)
// Get (if doWrite = FALSE) or Set (if dowrite = TRUE) value of Boolean Field
BOOL CARSRecordset::operator()(LPCSTR fieldName, BOOL& fd, BOOL doWrite, BOOL isNULL)
{
    BOOL ret = TRUE;
    COleVariant v;

    if (doWrite) {
        if (isNULL) 
            v.vt = VT_NULL;
        else {
            v.vt = VT_BOOL;
            v.boolVal = fd ? VARIANT_TRUE : VARIANT_FALSE;
        }
        SetFieldValue(fieldName, &v);
    }
    else {
        GetFieldValue(fieldName, &v);

        if ((v.vt == VT_EMPTY) || (v.vt == VT_NULL))
            ret = FALSE;

        if ((v.vt != VT_BOOL) && (v.vt != VT_EMPTY) && (v.vt != VT_NULL))
            AfxThrowARSException(ARSMSG_ERR_ADO_RECSET_INVAL_FIELDTYPE);
        fd = (v.boolVal == VARIANT_FALSE) ? FALSE : TRUE;
    }

    return ret;
}


// BOOL CARSRecordset::operator()(LPCSTR fieldName, COleDateTime&, BOOL doWrite, BOOL isNULL)
// Get (if doWrite = FALSE) or Set (if dowrite = TRUE) value of DateTime Field
BOOL CARSRecordset::operator()(LPCSTR fieldName, COleDateTime& fd, BOOL doWrite, BOOL isNULL)
{
    BOOL ret = TRUE;
    COleVariant v;

    if (doWrite) {
        if (isNULL) 
            v.vt = VT_NULL;
        else {
            v.vt = VT_DATE;
            v.date = fd.m_dt;
        }
        SetFieldValue(fieldName, &v);
    }
    else {
        GetFieldValue(fieldName, &v);

        if ((v.vt == VT_EMPTY) || (v.vt == VT_NULL))
            ret = FALSE;

        if ((v.vt != VT_DATE) && (v.vt != VT_EMPTY) && (v.vt != VT_NULL))
            AfxThrowARSException(ARSMSG_ERR_ADO_RECSET_INVAL_FIELDTYPE);
        fd.m_dt = v.date;
    }

    return ret;
}

// BOOL CARSRecordset::operator()(LPCSTR fieldName, CY&, BOOL doWrite, BOOL isNULL)
// Get (if doWrite = FALSE) or Set (if dowrite = TRUE) value of DateTime Field
BOOL CARSRecordset::operator()(LPCSTR fieldName, CY& fd, BOOL doWrite, BOOL isNULL)
{
    BOOL ret = TRUE;
    COleVariant v;

    if (doWrite) {
        if (isNULL) 
            v.vt = VT_NULL;
        else {
            v.vt = VT_CY;
            v.cyVal = fd;
        }
        SetFieldValue(fieldName, &v);
    }
    else {
        GetFieldValue(fieldName, &v);

        if ((v.vt == VT_EMPTY) || (v.vt == VT_NULL))
            ret = FALSE;

        if ((v.vt != VT_CY) && (v.vt != VT_EMPTY) && (v.vt != VT_NULL))
            AfxThrowARSException(ARSMSG_ERR_ADO_RECSET_INVAL_FIELDTYPE);
        fd = v.cyVal;
    }

    return ret;
}

// Called upon Open(pConn, CursorType, LockMode, Options)
// must return source (either table name or query for extracting data)
LPCSTR CARSRecordset::GetSource() { 
    return NULL;
}

// Called from MoveFirst, Next() and so on to copy data from Database to internal variables
void CARSRecordset::CopyDataFromDatabase() 
{
}

// Called from Update(), AddNew() to copy data from internal variables to Database
void CARSRecordset::CopyDataToDatabase()
{
}


CARSException::CARSException(UINT _m_cause, UINT _m_detailed/* = 0*/, ADOConnection *pConn /*= NULL*/, UINT _m_helpID/* = 0*/) 
{ 
    m_cause = _m_cause; 
    m_detailed = _m_detailed;
    m_helpID = _m_helpID;
    ExtractConnError(pConn);
}

UINT CARSException::GetCause() 
{ 
    return m_cause;     
}

UINT CARSException::GetDetailed() 
{ 
    return m_detailed;     
}

UINT CARSException::GetHelpID() 
{
    return m_helpID;
}

BOOL CARSException::GetErrorMessage(LPTSTR lpszError, UINT nMaxError, PUINT pnHelpContext/* = NULL*/ ) 
{
    char buff[ARSMSG_MAX_MSG_SIZE];
    BOOL res;
    
    arsmsg_GetMessage(GetCause(), buff, &res);
    strncpy(lpszError, buff, nMaxError-1);
    lpszError[nMaxError-1] = '\0';
    
    arsmsg_GetMessage(GetDetailed(), buff, &res);
    nMaxError -= strlen(lpszError);
    lpszError += strlen(lpszError);
    if ((nMaxError > 1) && strlen(buff)) {
        strncat(lpszError, buff, nMaxError - 1);
        lpszError[nMaxError - 1] = '\0';
    }
    
    nMaxError -= strlen(buff);
    lpszError += strlen(buff);
    if ((nMaxError > 1) && m_ErrorStr.GetLength()) {
        strncat(lpszError, m_ErrorStr, nMaxError - 1);
        lpszError[nMaxError - 1] = '\0';
    }
    
    if (pnHelpContext)
        *pnHelpContext = m_helpID;
    
    return res;
}

int CARSException::ReportError( UINT nType /*= MB_OK*/, UINT nMessageID/* = 0*/ )
{
    
    char buff[ARSMSG_MAX_MSG_SIZE];
    
    GetErrorMessage(buff, ARSMSG_MAX_MSG_SIZE);
    
    return AfxMessageBox(buff, nType, GetHelpID());
}

void CARSException::ExtractConnError(ADOConnection *pConn) 
{
    long i,l,e;
    HRESULT hr;
    BSTR bestr = NULL;
    LPTSTR lptstr = NULL;
    ADOErrors *pErrors = NULL;
    ADOError *pError = NULL;
    COleVariant varIndex;
    CString stmp, strNewLine("\n");
    try {
        
        if (pConn) {
            hr = pConn->get_Errors(&pErrors);
            if (!FAILED(hr) && pErrors) {
                hr = pErrors->get_Count(&l);
                if (!FAILED(hr)) {
                    for(i = 0; i < l; i++) {
                        varIndex.vt = VT_I4;
                        varIndex.lVal = i;
                        pErrors->get_Item(varIndex, &pError);
                        if (!FAILED(hr) && pError) {
                            m_ErrorStr += strNewLine + arsmsg_GetMessage(ARSMSG_STR_ERROR_DESCRIPTION);
                            pError->get_Description(&bestr);                               
                            m_ErrorStr += _bstr_t(bestr, FALSE);
                            
                            
                            bestr[0] = 0;
                            m_ErrorStr += strNewLine + arsmsg_GetMessage(ARSMSG_STR_ERROR_SOURCE);
                            pError->get_Source(&bestr);
                            m_ErrorStr += _bstr_t(bestr, FALSE);
                            
                            bestr[0] = 0;
                            m_ErrorStr += strNewLine + arsmsg_GetMessage(ARSMSG_STR_ERROR_SQLSTATE);
                            pError->get_SQLState(&bestr);
                            m_ErrorStr += _bstr_t(bestr, FALSE);
                            
                            e = 0;
                            m_ErrorStr += strNewLine + arsmsg_GetMessage(ARSMSG_STR_ERROR_NUMBER);
                            pError->get_Number(&e);
                            stmp.Format("%x", e);
                            m_ErrorStr += stmp;
                            
                            e = 0;
                            m_ErrorStr += strNewLine + arsmsg_GetMessage(ARSMSG_STR_ERROR_NATIVE);
                            pError->get_NativeError(&e);
                            stmp.Format("%d", e);
                            m_ErrorStr += stmp;
                            
                            pError->Release();
                            pError = NULL;
                        }
                    }
                }
            }
        }
        
        if (pError)
            pError->Release();
        if (pErrors)
            pErrors->Release();
    }
    catch(...) {
        
        if (pError)
            pError->Release();
        if (pErrors)
            pErrors->Release();
        
    }
}

⌨️ 快捷键说明

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