?? 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 + -