📄 adodbmod.cpp
字号:
#include "stdafx.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endifCString GetAccessDBSRC(CString strFileName)
{
CString strSRC;
strSRC="DRIVER=Microsoft Access Driver (*.mdb);DBQ=";
strSRC+=strFileName;
strSRC+=";UID=sa;PWD=";
return strSRC;
}
CString GetAccessDBSRC(CString strFileName,CString strPWD)
{
CString strSRC;
strSRC="DRIVER=Microsoft Access Driver (*.mdb);DBQ=";
strSRC+=strFileName;
strSRC+=";PWD=";
strSRC+=strPWD;
return strSRC;
}BOOL ConnectDB(_ConnectionPtr& pConnection,CString strSRC)
{
if (FAILED(pConnection.CreateInstance(_uuidof(Connection))))
// if (FAILED(pConnection.CreateInstance("adodb.connection")))
{
AfxMessageBox("创建 ADO Connection 对象失败!",MB_OK | MB_ICONEXCLAMATION | MB_ICONWARNING);
return FALSE;
}
_bstr_t bstrSRC(strSRC);
try
{
pConnection->Open ( bstrSRC,"","",adConnectUnspecified);
}
catch(CException* pEx)
{
char pstrErrMsg[255];
pEx->GetErrorMessage(pstrErrMsg,255);
CString strErrMsg = pstrErrMsg;
CString strMsg;
strMsg = "连接数据库失败!\n" + strErrMsg;
AfxMessageBox(strMsg,MB_OK | MB_ICONEXCLAMATION | MB_ICONWARNING);
pConnection.Release();
return FALSE;
}
catch(...)
{
CString strErrMsg;
strErrMsg = "连接数据库失败!\n";
strErrMsg += strSRC;
AfxMessageBox(strErrMsg);
return FALSE;
}
return TRUE;
}
BOOL ExecuteSQL(_ConnectionPtr& pConnection,CString strSQL)
{
_bstr_t bstrSQL(strSQL);
_variant_t vRecsAffected(0L);
try
{
pConnection->Execute(bstrSQL, &vRecsAffected, adOptionUnspecified);
}
catch(CException* pEx)
{
CString strMsg;
char pszError[256];
pEx->GetErrorMessage (pszError,256);
strMsg = pszError;
AfxMessageBox(strMsg);
return FALSE;
}
catch(...)
{
CString strErrMsg;
strErrMsg = "数据库操作错误!\n";
strErrMsg += strSQL;
AfxMessageBox(strErrMsg);
return FALSE;
}
return TRUE;
}
BOOL ExecuteRecordset(CString strSRC,_RecordsetPtr& pRecordset,CString strSQL)
{
if (FAILED(pRecordset.CreateInstance(L"ADODB.Recordset")))
{
CString strMsg;
strMsg = "Create AdoRecordset Instance failed!";
strMsg += "\nsource:" + strSRC;
strMsg += "\nSQL:" + strSQL;
AfxMessageBox(strMsg);
return FALSE;
}
_variant_t varSRC(strSRC);
_variant_t varSQL(strSQL);
try
{
pRecordset->Open(varSQL,varSRC,adOpenStatic,adLockOptimistic,adCmdText);
}
catch(CException* pEx)
{
CString strMsg;
char pszError[256];
pEx->GetErrorMessage (pszError,256);
strMsg = pszError;
AfxMessageBox(strMsg);
pRecordset.Release();
return FALSE;
}
catch(...)
{
CString strErrMsg;
strErrMsg = "数据库操作错误!\n";
strErrMsg += strSQL;
AfxMessageBox(strErrMsg);
return FALSE;
}
return TRUE;;
}
CString GetFieldValue(_RecordsetPtr& pRcd,CString strField)
{
_bstr_t bstrField(strField);
_variant_t varValue;
varValue = pRcd->GetCollect (bstrField);
if (varValue.vt == VT_NULL || varValue.vt == VT_EMPTY)
return "";
CString strValue;
strValue = (char *)_bstr_t(varValue);
return strValue;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -