连接Access需要用到系统的两个动态链接库,分别是msado15.dll和msadox.dll,然后就可以连接数据库了。 连接语句:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=路径
WIN7旗舰版自带有,其它系统未知,自己尝试
ADOConn.cpp文件
测试回复可下载 动态链接库下载 测试项目:AccessDemo
WIN7旗舰版自带有,其它系统未知,自己尝试
//用XP下面的MSADO15来编译,不然WIN7版本高于XP,XP下面将无法使用 //no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF") #import "C:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF", "adoEOF") //编译提示:“ADOX:不是类名或命名空间”加入下面1行代码即可 #import "C:\\Program Files\\Common Files\\System\\ado\\msadox.dll"
// ADOConn.h: interface for the ADOConn class. // ////////////////////////////////////////////////////////////////////// #pragma once #include #include #pragma comment(lib,"shlwapi.lib") //用XP下面的MSADO15来编译,不然WIN7版本高于XP,XP下面将无法使用 //no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF") #import "D:\\develop\\dll_lib\\msado15.dll" no_namespace rename("EOF", "adoEOF") //编译提示:“ADOX:不是类名或命名空间”加入下面1行代码即可 #import "D:\\develop\\dll_lib\\msadox.dll" #define DAT_NAME "reply.mdb" #define TABLE_NAME "reply" #define COLUMN_ID "id" #define COLUMN_KEY "_key" #define COLUMN_REPLY "_reply" #define COLUMN_COMMENT "_comment" #define COLUMN_CLASS "_cid" #define SQL_LENGTH 512 #define TABLE_CLASS_NAME "reply_class" #define COLUMN_CLASS_ID "id" #define COLUMN_CLASS_COMMENT "_comment" #define DEFAULT_CLASS "日常用语" #define KEY_SIZE 1024 #define REPLY_SIZE 1024 struct ReplyMsg { char _id[5]; //数据库ID char _cid[5]; //类型ID char _key[KEY_SIZE]; char _reply[REPLY_SIZE]; char _comment[MAX_PATH]; std::vector _key_part; ReplyMsg() { _key_part.reserve(10); memset(_id,0,sizeof(_id)); memset(_cid,0,sizeof(_cid)); memset(_key,0,sizeof(_key)); memset(_reply,0,sizeof(_reply)); memset(_comment,0,sizeof(_comment)); } void Init(char *key,char *replay,char *comment) { strcpy_s(_key,sizeof(_key),key); strcpy_s(_reply,sizeof(_reply),replay); strcpy_s(_comment,sizeof(_comment),comment); ParseKey(); } void Init(char *id,char *key,char *replay,char *comment,char *cid) { strcpy_s(_id,sizeof(_id),id); strcpy_s(_key,sizeof(_key),key); strcpy_s(_reply,sizeof(_reply),replay); strcpy_s(_comment,sizeof(_comment),comment); strcpy_s(_cid,sizeof(_cid),cid); ParseKey(); } private: void ParseKey() { char tmp[1024]; strncpy_s(tmp,1024,_key,sizeof(_key)); char *pSplit = NULL; char *pNext = NULL; int nCount = 0; const char *delim = " "; pSplit = strtok_s(tmp,delim,&pNext); while(pSplit != NULL) { CString tempKey(pSplit); _key_part.push_back(tempKey); pSplit = strtok_s(NULL,delim,&pNext); } } }; //回复类型 struct MsgClass { char _id[5]; char _comment[MAX_PATH]; MsgClass() { memset(_id,0,sizeof(_id)); memset(_comment,0,sizeof(_comment)); } void Init(char *comment) { strcpy_s(_comment,sizeof(_comment),comment); } void Init(char *id,char *comment) { strcpy_s(_id,sizeof(_id),id); strcpy_s(_comment,sizeof(_comment),comment); } }; class ADOConn { private: //添加一个指向Connection对象的指针 _ConnectionPtr m_pConnection; static ADOConn *_instance; ADOConn(); public: static ADOConn* getInstance(); virtual ~ADOConn(); BOOL OnInitADOConn(); void AddTable(); void DeleteTable(char *tName); void AddRecord(ReplyMsg* pMsg); int TotalRecords(void); void QueryRecords(std::vector *_vector); void DeleteRecord(ReplyMsg* pMsg); void UpdateRecord(ReplyMsg* pMsg); bool ExecuteSql(char* strSql); bool IsInit(void); //class 类型 void AddClassTable(void); void AddClassRecord(MsgClass *pClass); void DeleteClassRecord(MsgClass *pClass); void UpdateClassRecord(MsgClass *pClass); void QueryClassRecords(std::vector *_vector); };
ADOConn.cpp文件
#include "stdafx.h" #include "ADOConn.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif using namespace ADOX; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// ADOConn* ADOConn::_instance = NULL; ADOConn::ADOConn() { m_pConnection = NULL; if(!AfxOleInit())//COM库的初始化 AfxMessageBox("COM初始失败"); //::CoInitialize(NULL);////非MFC工程初始化OLE/COM库环境 this->_instance = this; } ADOConn::~ADOConn() { //关闭记录集和连接 if(m_pConnection != NULL) m_pConnection->Close(); //::CoUninitialize();//非MFC工程释放 } ADOConn* ADOConn::getInstance() { if(_instance == NULL) _instance = new ADOConn; return _instance; } BOOL ADOConn::OnInitADOConn() { try { char path[MAX_PATH]; CString strPath; ::GetModuleFileName(NULL,path,MAX_PATH); strPath = path; strPath = strPath.Left(strPath.ReverseFind('\\')+1); strPath += DAT_NAME; m_pConnection.CreateInstance("ADODB.Connection");//初始化Connection指针 // 打开本地Access库student.mdb char strname[SQL_LENGTH] = {0}; //Microsoft.ACE.OLEDB.12.0是连接access2007之后的数据库使用的 //Microsoft.Jet.OLEDB.4.0是连接access2003等数据库使用的 //此处为连接Access的连接字符串,用ADO连接SQL也是此模式,不同的就是变化此处的连接字符串 sprintf_s(strname,"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s",strPath); CFileFind m_FileFind; if(!m_FileFind.FindFile(strPath)) { //没有该mdb文件则去创建 _CatalogPtr m_pCatalog = NULL; //__uuidof居然还出错 HRESULT hr = m_pCatalog.CreateInstance("ADOX.Catalog"); if(FAILED(hr)) _com_issue_error(hr); else m_pCatalog->Create(strname); //Create mdb } //打开连接数据库 m_pConnection->Open(strname,"","",adModeUnknown); } catch(_com_error e) { AfxMessageBox(e.ErrorMessage()); return FALSE; } return TRUE; } ///////////////以下是消息数据库内容////////////////////// void ADOConn::AddTable() { try { char strSQL[SQL_LENGTH] = {0}; sprintf_s(strSQL,"select * from %s",TABLE_NAME); bool exist = ExecuteSql(strSQL); if (!exist) { memset(strSQL,0,SQL_LENGTH); sprintf_s(strSQL,"create table %s(%s counter(1,1) primary key,%s varchar(255),%s varchar(255),%s varchar(255),%s int)" ,TABLE_NAME,COLUMN_ID,COLUMN_KEY,COLUMN_REPLY,COLUMN_COMMENT,COLUMN_CLASS); _variant_t RecordsAffected; m_pConnection->Execute(strSQL,&RecordsAffected,adCmdText); } }catch(_com_error e) { AfxMessageBox(e.ErrorMessage()); } } void ADOConn::DeleteTable(char *tName) { try { _variant_t RecordsAffected; char strSQL[SQL_LENGTH] = {0}; sprintf_s(strSQL,"drop table %s",tName); m_pConnection->Execute(strSQL,&RecordsAffected,adCmdText); }catch(_com_error e) { AfxMessageBox(e.ErrorMessage()); } } ////////////SQL语句功能////////// //create table %s(id counter(1,1) primary key,username varchar(50),age int) //AddColumn ALTER TABLE test_table ADD newcolumn1 INTEGER //AddColumn ALTER TABLE new ADD newcolumn1 INTEGER //SetIdIndex CREATE UNIQUE INDEX id ON new(ID) void ADOConn::AddRecord(ReplyMsg* pMsg) { try { _variant_t RecordsAffected; char strSQL[SQL_LENGTH] = {0}; sprintf_s(strSQL,"insert into %s(%s,%s,%s,%s) values ('%s','%s','%s',%d)",TABLE_NAME, COLUMN_KEY,COLUMN_REPLY,COLUMN_COMMENT,COLUMN_CLASS,pMsg->_key ,pMsg->_reply,pMsg->_comment,atoi(pMsg->_cid)); m_pConnection->Execute(strSQL,&RecordsAffected,adCmdText); }catch(_com_error e) { AfxMessageBox(e.ErrorMessage()); } } int ADOConn::TotalRecords(void) { try { _RecordsetPtr m_pRecordset; _variant_t RecordsAffected; m_pRecordset = m_pConnection->Execute("select count(*) from test_table where id > 0" ,&RecordsAffected,adCmdText); _variant_t vCount = m_pRecordset->GetCollect((long)0); ///取得第一个字段的值放入vCount变量 m_pRecordset->Close();///关闭记录集 return vCount.lVal; }catch(_com_error e) { AfxMessageBox(e.ErrorMessage()); } return 0; } void ADOConn::QueryRecords(std::vector *_vector) { if(_vector == NULL) return; _vector->clear(); try { char strSQL[SQL_LENGTH] = {0}; sprintf_s(strSQL,"select * from %s",TABLE_NAME); _RecordsetPtr m_pRecordset; _variant_t RecordsAffected; m_pRecordset = m_pConnection->Execute(strSQL,&RecordsAffected,adCmdText); _variant_t v_id; _variant_t v_key; _variant_t v_reply; _variant_t v_comment; _variant_t v_cid; while(!m_pRecordset->adoEOF) { v_id = m_pRecordset->GetCollect(COLUMN_ID); v_key = m_pRecordset->GetCollect(COLUMN_KEY); v_reply = m_pRecordset->GetCollect(COLUMN_REPLY); v_comment = m_pRecordset->GetCollect(COLUMN_COMMENT); v_cid = m_pRecordset->GetCollect(COLUMN_CLASS); _bstr_t _id = (_bstr_t)v_id; _bstr_t _key = (_bstr_t)v_key; _bstr_t _reply = (_bstr_t)v_reply; _bstr_t _comment = (_bstr_t)v_comment; _bstr_t _cid = (_bstr_t)v_cid; char *szID = _id; char *szKey = _key; char *szReply = _reply; char *szComment = _comment; char *szCID = _cid; ReplyMsg _msg; _msg.Init(szID,szKey,szReply,szComment,szCID); if(_vector != NULL) _vector->push_back(_msg); m_pRecordset->MoveNext(); } m_pRecordset->Close(); }catch(_com_error e) { AfxMessageBox(e.ErrorMessage()); } } void ADOConn::DeleteRecord(ReplyMsg* pMsg) { try { _variant_t RecordsAffected; char strSQL[SQL_LENGTH] = {0}; sprintf_s(strSQL,"delete from %s where %s=%d",TABLE_NAME,COLUMN_ID,atoi(pMsg->_id)); m_pConnection->Execute(strSQL,&RecordsAffected,adCmdText); }catch(_com_error e) { AfxMessageBox(e.ErrorMessage()); } } void ADOConn::UpdateRecord(ReplyMsg* pMsg) { try { _variant_t RecordsAffected; char strSQL[SQL_LENGTH] = {0}; sprintf_s(strSQL,"update %s set %s='%s',%s='%s',%s='%s',%s='%s' where %s=%d", TABLE_NAME,COLUMN_KEY,pMsg->_key,COLUMN_REPLY,pMsg->_reply,COLUMN_COMMENT, pMsg->_comment,COLUMN_CLASS,pMsg->_cid,COLUMN_ID,atoi(pMsg->_id)); m_pConnection->Execute(strSQL,&RecordsAffected,adCmdText); }catch(_com_error e) { AfxMessageBox(e.ErrorMessage()); } } bool ADOConn::ExecuteSql(char* strSql) { try { _variant_t RecordsAffected; char strSQL[SQL_LENGTH] = {0}; sprintf_s(strSQL,strSql); m_pConnection->Execute(strSQL,&RecordsAffected,adCmdText); }catch(_com_error e) { return false; } return true; } bool ADOConn::IsInit(void) { return m_pConnection != NULL; } ///////////////以下是消息类型数据库内容////////////////////// void ADOConn::AddClassTable(void) { try { char strSQL[SQL_LENGTH] = {0}; sprintf_s(strSQL,"select * from %s",TABLE_CLASS_NAME); bool exist = ExecuteSql(strSQL); if (!exist) { memset(strSQL,0,SQL_LENGTH); sprintf_s(strSQL,"create table %s(%s counter(1,1) primary key,%s varchar(255))" ,TABLE_CLASS_NAME,COLUMN_CLASS_ID,COLUMN_CLASS_COMMENT); _variant_t RecordsAffected; m_pConnection->Execute(strSQL,&RecordsAffected,adCmdText); //不存入插入一条默认的数据 MsgClass m_class; m_class.Init(DEFAULT_CLASS); AddClassRecord(&m_class); } }catch(_com_error e) { AfxMessageBox(e.ErrorMessage()); } } void ADOConn::AddClassRecord(MsgClass *pClass) { try { _variant_t RecordsAffected; char strSQL[SQL_LENGTH] = {0}; sprintf_s(strSQL,"insert into %s(%s) values ('%s')",TABLE_CLASS_NAME, COLUMN_CLASS_COMMENT,pClass->_comment); m_pConnection->Execute(strSQL,&RecordsAffected,adCmdText); }catch(_com_error e) { AfxMessageBox(e.ErrorMessage()); } } void ADOConn::DeleteClassRecord(MsgClass *pClass) { try { _variant_t RecordsAffected; char strSQL[SQL_LENGTH] = {0}; sprintf_s(strSQL,"delete from %s where %s=%d",TABLE_CLASS_NAME,COLUMN_CLASS_ID,atoi(pClass->_id)); m_pConnection->Execute(strSQL,&RecordsAffected,adCmdText); }catch(_com_error e) { AfxMessageBox(e.ErrorMessage()); } } void ADOConn::UpdateClassRecord(MsgClass *pClass) { try { _variant_t RecordsAffected; char strSQL[SQL_LENGTH] = {0}; sprintf_s(strSQL,"update %s set %s='%s' where %s=%d", TABLE_CLASS_NAME,COLUMN_CLASS_COMMENT,pClass->_comment,COLUMN_CLASS_ID,atoi(pClass->_id)); m_pConnection->Execute(strSQL,&RecordsAffected,adCmdText); }catch(_com_error e) { AfxMessageBox(e.ErrorMessage()); } } void ADOConn::QueryClassRecords(std::vector *_vector) { if(_vector == NULL) return; _vector->clear(); try { char strSQL[SQL_LENGTH] = {0}; sprintf_s(strSQL,"select * from %s",TABLE_CLASS_NAME); _RecordsetPtr m_pRecordset; _variant_t RecordsAffected; m_pRecordset = m_pConnection->Execute(strSQL,&RecordsAffected,adCmdText); _variant_t v_id; _variant_t v_comment; while(!m_pRecordset->adoEOF) { v_id = m_pRecordset->GetCollect(COLUMN_CLASS_ID); v_comment = m_pRecordset->GetCollect(COLUMN_CLASS_COMMENT); _bstr_t _id = (_bstr_t)v_id; _bstr_t _comment = (_bstr_t)v_comment; char *szID = _id; char *szComment = _comment; MsgClass _class; _class.Init(szID,szComment); if(_vector != NULL) _vector->push_back(_class); m_pRecordset->MoveNext(); } m_pRecordset->Close(); }catch(_com_error e) { AfxMessageBox(e.ErrorMessage()); } }
测试回复可下载 动态链接库下载 测试项目:AccessDemo
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2302
- 用户1336
- 访客10968998
每日一句
Qingming Festival invites us to honor ancestors with quiet reflection and respect.
清明节邀请我们以静思与敬意祭奠祖先。
清明节邀请我们以静思与敬意祭奠祖先。
新会员