ADO 读取 ACCESS
1 #pragma once 2 #include <vector> 3 // 加入ADO支持库, 4 #import "C:\Program Files\Common Files\System\ado\msado15.dll" \ 5 no_namespace \ 6 rename ("EOF", "adoEOF") 7 8 template<typename stDBDATA> 9 class CADO 10 { 11 public: 12 CADO(); 13 virtual ~CADO(); 14 public: 15 static void dump_com_error(_com_error &e); 16 bool Init(); 17 bool Start(); 18 void Uninit(); 19 public: 20 void OnWrite(char* name, int age); 21 void OnRead(); 22 void OnDelete(); 23 void OnModify(); 24 void OnExcute(); 25 26 private: 27 28 _ConnectionPtr m_pConnection; 29 30 _CommandPtr m_pCommand; 31 _RecordsetPtr m_pRecordset; 32 _RecordsetPtr m_pRecordset1; 33 char* m_strCommand; 34 35 }; 36 37 template<class stDBDATA> 38 CADO<stDBDATA>::CADO() 39 { 40 Init(); 41 } 42 43 template<class stDBDATA> 44 CADO<stDBDATA>::~CADO() 45 { 46 Uninit(); 47 } 48 49 template<class stDBDATA> 50 void CADO<stDBDATA>::dump_com_error(_com_error &e) 51 { 52 char ErrorStr[MAX_PATH]; 53 54 _bstr_t bstrSource(e.Source()); 55 _bstr_t bstrDescription(e.Description()); 56 sprintf_s(ErrorStr, "\n\tADO Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n\n", 57 e.Error(), e.ErrorMessage(), (LPCTSTR)bstrSource, (LPCTSTR)bstrDescription); 58 printf(ErrorStr); 59 } 60 61 template<class stDBDATA> 62 bool CADO<stDBDATA>::Init() 63 { 64 // 初始化COM,创建ADO连接等操作 65 int S_Result = OleInitialize(NULL); 66 HRESULT hr = m_pConnection.CreateInstance(__uuidof(Connection)); 67 /*HRESULT hr = m_pConnection.CreateInstance("ADODB.Connection");*/ 68 if(FAILED(hr)) 69 { 70 printf("m_pConnection.CreateInstance调用失败\n"); 71 return false; 72 } 73 try 74 { 75 // 打开本地Access库Demo.mdb 76 m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Demo.mdb","","",adModeUnknown); 77 } 78 catch(_com_error& e) 79 { 80 dump_com_error(e); 81 return false; 82 } 83 84 return true; 85 } 86 87 template<class stDBDATA> 88 bool CADO<stDBDATA>::Start() 89 { 90 // 使用ADO创建数据库记录集 91 m_pRecordset.CreateInstance(__uuidof(Recordset)); 92 try 93 { 94 m_pRecordset->Open("SELECT * FROM USERTABLE", // 查询User表中所有字段 95 m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针 96 adOpenDynamic, 97 adLockOptimistic, 98 adCmdText); 99 } 100 catch(_com_error& e) 101 { 102 dump_com_error(e); 103 return false; 104 } 105 106 return true; 107 } 108 109 template<class stDBDATA> 110 void CADO<stDBDATA>::Uninit() 111 { 112 // 关闭ADO连接状态 113 if(m_pConnection->State) 114 { 115 m_pConnection->Close(); 116 m_pConnection= NULL; 117 } 118 119 if(m_pRecordset->State) 120 { 121 m_pRecordset->Close(); 122 m_pRecordset.Release(); 123 m_pRecordset = NULL; 124 } 125 } 126 127 template<class stDBDATA> 128 void CADO<stDBDATA>::OnWrite(char* name, int age) 129 { 130 long age_l = age; 131 try 132 { 133 // 写入各字段值 134 m_pRecordset->AddNew(); 135 m_pRecordset->PutCollect("Name", LPSTR(name)); 136 m_pRecordset->PutCollect("Age", age_l); 137 m_pRecordset->Update(); 138 } 139 catch(_com_error& e) 140 { 141 dump_com_error(e); 142 } 143 } 144 145 template<class stDBDATA> 146 void CADO<stDBDATA>::OnRead() 147 { 148 _variant_t var; 149 150 char strName[MAX_PATH]; 151 int age; 152 153 try 154 { 155 if(!m_pRecordset->BOF) 156 { 157 m_pRecordset->MoveFirst(); 158 } 159 else 160 { 161 printf("表内数据为空\n"); 162 return; 163 } 164 165 while(!m_pRecordset->adoEOF) 166 { 167 var = m_pRecordset->GetCollect("Name"); 168 if(var.vt != VT_NULL) 169 { 170 strcpy_s(strName, _bstr_t(var)); 171 } 172 var = m_pRecordset->GetCollect("Age"); 173 if(var.vt != VT_NULL) 174 { 175 age = atol(_bstr_t(var)); 176 } 177 178 stDBDATA data(strName, age); 179 data.print(); 180 181 m_pRecordset->MoveNext(); 182 } 183 } 184 catch(_com_error& e) 185 { 186 dump_com_error(e); 187 } 188 } 189 190 template<class stDBDATA> 191 void CADO<stDBDATA>::OnDelete() 192 { 193 try 194 { 195 m_pRecordset->MovePrevious(); 196 // 删除当前行记录 197 m_pRecordset->Delete(adAffectCurrent); 198 m_pRecordset->Update(); 199 200 } 201 catch(_com_error& e) 202 { 203 dump_com_error(e); 204 } 205 } 206 207 template<class stDBDATA> 208 void CADO<stDBDATA>::OnModify() 209 { 210 char m_Name[MAX_PATH] = "fffff"; 211 long m_Age = 25; 212 213 // 修改当前记录的字段值 214 try 215 { 216 m_pRecordset->PutCollect("Name", _variant_t(m_Name)); 217 m_pRecordset->PutCollect("Age", m_Age); 218 m_pRecordset->Update(); 219 } 220 catch(_com_error& e) 221 { 222 dump_com_error(e); 223 } 224 } 225 226 template<class stDBDATA> 227 void CADO<stDBDATA>::OnExcute() 228 { 229 _CommandPtr m_pCommand; 230 m_strCommand = "delete from USERTABLE where Name = 'fffff'"; 231 232 m_pCommand.CreateInstance(__uuidof(Command)); 233 m_pCommand->ActiveConnection = m_pConnection; // 将库连接赋于它 234 m_pCommand->CommandText = _bstr_t((LPCTSTR)m_strCommand); // SQL语句 235 try 236 { 237 // 执行SQL语句,返回记录集,此记录不能做插入操作 238 // 故为了不和m_pRecordset相冲突, 放入新定义的m_pRecordset1 239 m_pRecordset1 = m_pCommand->Execute( NULL, NULL, adCmdText); 240 } 241 catch(_com_error& e) 242 { 243 dump_com_error(e); 244 } 245 }
1 #include "ado.h" 2 3 struct stDB 4 { 5 char name[MAX_PATH]; 6 int age; 7 8 stDB() 9 { 10 11 } 12 13 stDB(char* _name, int _age) 14 { 15 strcpy_s(name,MAX_PATH, _name); 16 age = _age; 17 } 18 19 void print() 20 { 21 printf("Name:%s------Age:%d\n", name, age); 22 } 23 }; 24 25 int main() 26 { 27 CADO<stDB> ado; 28 ado.Start(); 29 ado.OnRead(); 30 ado.OnWrite("q1", 10); 31 ado.OnModify(); 32 ado.OnDelete(); 33 ado.OnExcute(); 34 ado.OnRead(); 35 system("pause"); 36 return 0; 37 }