数据库使用步骤。
先把对数据库的操作封装到类ADOConnection,再在App里面调用数据库的操作函数。
在App函数里面的InitInstance()函数里面打开数据库
ADOConnection m_adoConn; CUser m_curUser; InitInstance() { if ( !m_adoConn.Open( GetDBPath())) { AfxMessageBox(_T("Can't open database TrafficLED.mdb.")); return FALSE; } if ( m_adoConn.IsOpen()) { m_adoConn.Close(); } }
其中。
CString CInfoStationApp::GetModulePath() { char chPath[MAX_PATH]; char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; GetModuleFileName( NULL , chPath , MAX_PATH ); //得到当前应用程序路径 _splitpath(chPath, drive, dir, NULL,NULL); CString strPath; strPath.Format("%s%s", drive, dir); return strPath; } CString CInfoStationApp::GetDBPath() { CString strPath = GetModulePath() + _T("TrafficLED.mdb"); return strPath; } CString CInfoStationApp::GetIniPath() { CString strPath = GetModulePath() + _T("ProgInfo.ini"); return strPath; }
2.然后登陆
CString strSQL = _T("SELECT * FROM [User]") ; _RecordsetPtr pRes = theApp.m_adoConn.ExecuteSelectSQL( strSQL ); if ( pRes != NULL ) { while( !pRes->adoEOF ) { CString strAdmin = (CString)pRes->GetCollect((_variant_t)((long)0)).bstrVal; CString strPassword = (CString)pRes->GetCollect((_variant_t)((long)1)).bstrVal; int nPopedom = (int)pRes->GetCollect((_variant_t)((long)2)).intVal; m_mapUserPassword.insert( CUserPasswordMap::value_type(strAdmin,strPassword)); m_mapUserPopedom.insert( CUserPopedomMap::value_type(strAdmin,nPopedom)); pRes->MoveNext(); } pRes->Close(); pRes.Release(); }
其中,ADOConnection.h 文件为
#pragma once class ADOConnection { public: ADOConnection(void); virtual ~ADOConnection(void); public: BOOL Open(const CString& strCource) ; void Close(); BOOL IsOpen() ; BOOL ExecuteNoSelectSQL(const CString& strSQL); _RecordsetPtr ExecuteSelectSQL(const CString& strSQL) ; _ConnectionPtr* GetADOSmartpConnection(); private: _ConnectionPtr* m_pConn; _ConnectionPtr& m_pConnection; };
ADOConnection.cpp 文件为
#include "StdAfx.h" #include "adoconnection.h" ADOConnection::ADOConnection(void):m_pConnection(*m_pConn),m_pConn(new _ConnectionPtr) { } ADOConnection::~ADOConnection(void) { this->Close(); if ( m_pConn != NULL) { delete m_pConn; m_pConn = NULL; } } BOOL ADOConnection::Open( const CString& strCource ) { HRESULT hr = 0; try { hr = CoInitialize(NULL); hr = this->m_pConnection.CreateInstance("ADODB.Connection"); if (SUCCEEDED(hr)) { // strCource = _T("driver={SQL Server};Server=(local);DATABASE=MessagePublish;UID=sa;PWD=123456") ; //SQL Server连接串 // strCource = _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= MessagePublish.mbd") ; //Access连接串 CString strConn = _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=") + strCource + _T(";Jet OLEDB:Database Password=!@#$%^&*()_+;") ; hr = m_pConnection->Open( _bstr_t(strConn),"","",adModeUnknown); //SQL Server if ( this->IsOpen()) return TRUE; } } catch (_com_error e) { return FALSE; } return FALSE; } void ADOConnection::Close() { try { if( m_pConnection != NULL ) { if ( m_pConnection->State != adStateClosed ) { m_pConnection->Close(); } CoUninitialize(); } m_pConnection = NULL; } catch (_com_error e) { } } BOOL ADOConnection::IsOpen() { if( m_pConnection == NULL || m_pConnection->State == adStateClosed ) { return FALSE; } return TRUE; } BOOL ADOConnection::ExecuteNoSelectSQL( const CString& strSQL ) { try { _variant_t RecordsAffected; m_pConnection->Execute(_bstr_t(strSQL),&RecordsAffected,adCmdText); ULONG iInfluence = (long)RecordsAffected; return TRUE; } catch (_com_error e) { CString str = _T ( "执行SQL语句出错:" ) + strSQL ; //AfxMessageBox( str ); return FALSE; } } _RecordsetPtr ADOConnection::ExecuteSelectSQL( const CString& strSQL ) { try { _RecordsetPtr m_pRecordset; m_pRecordset.CreateInstance(__uuidof(Recordset)); m_pRecordset->Open(_bstr_t(strSQL) ,(IDispatch*)m_pConnection ,adOpenDynamic ,adLockOptimistic ,adCmdText); return m_pRecordset; } catch (_com_error e) { } return NULL; } _ConnectionPtr* ADOConnection::GetADOSmartpConnection() { return m_pConn; }
要遍历数据库里面的数据:
void CInfoStationDlg::GetAllSign() { CString strSQL = _T("SELECT * FROM [Sign]") ; _RecordsetPtr pRecordSet = theApp.m_adoConn.ExecuteSelectSQL( strSQL );//这里判断有没有成功打开数据库,如果成功的话,就返回一个对象; if (pRecordSet != NULL) { while ( !pRecordSet->adoEOF ) { CString strID= (CString)pRecordSet->GetCollect((_variant_t)((long)0)).bstrVal ; CString strIP= (CString)pRecordSet->GetCollect((_variant_t)((long)1)).bstrVal ; CString strMemo= (CString)pRecordSet->GetCollect((_variant_t)((long)2)).bstrVal ; CSign* pSign = new CSign(); if ( pSign != NULL ) { pSign->SetID( strID ); pSign->SetIP( strIP ); pSign->SetMemo( strMemo); m_listSign.AddTail( pSign ); } pRecordSet->MoveNext(); } } }
3增加数据
void CInfoStationDlg::OnBnClickedAdd() { CSign* pSign = new CSign(); if ( pSign != NULL ) { CDlgSign dlg( pSign , FALSE , this); if ( IDOK == dlg.DoModal()) { if ( theApp.m_adoConn.ExecuteNoSelectSQL( pSign->GetAddSQL() ) )//这里代表语句执行成功; { m_listSign.AddTail( pSign ); FillSignList(); m_index = m_listCtrlSign.GetItemCount() - 1 ; m_listCtrlSign.SetItemState( m_index , LVIS_SELECTED , LVIS_SELECTED ); m_listCtrlSign.EnsureVisible( m_index , FALSE ); } } else { delete pSign; pSign = NULL; } } }
4.修改数据;
POSITION pos = m_listCtrlSign.GetFirstSelectedItemPosition(); if ( pos ) { int nIndex = m_listCtrlSign.GetNextSelectedItem(pos); m_index = nIndex ; CSign* pSign = (CSign*)m_listCtrlSign.GetItemData(nIndex); if ( pSign != NULL ) { CString strOldID = pSign->GetID(); CDlgSign dlg( pSign , TRUE , this); if ( IDOK == dlg.DoModal()) { if ( theApp.m_adoConn.ExecuteNoSelectSQL( pSign->GetModifySQL( strOldID) ) ) { FillSignList(); } } m_listCtrlSign.SetItemState( m_index , LVIS_SELECTED , LVIS_SELECTED ); BOOL SaveProgram( const CString &strPath ); } } else { if ( m_listCtrlSign.SetItemState( m_listCtrlSign.GetItemCount() - 1 , LVIS_SELECTED , LVIS_SELECTED )) { OnBnClickedEdit(); } else return ; }
5.删除数据