天天看點

vc mysql封裝類_[轉]VC下ADO資料庫操作的封裝類

1

19

20

23 classCPData24 {25 public:26

27 //預設初始化構造函數

28 CPData();29

30 //傳入連接配接的構造函數

31 CPData(_ConnectionPtr pConnection);32

33 //析構函數

34 virtual ~CPData();35 public:36

37 //連接配接資料庫

38 BOOL Connect(CString strUser,CString strPassword,CString strFileName="ConnectionParam.udl",int nOptions=-1,CString strConStr="Provider=sqloledb.1;Data Source=(local);Database=VLan");39

40 //關閉資料庫的連接配接

41 voidDisConnect();42

43 //資料庫查詢語句,用來對資料庫的各種字段進行查詢44 //如果成功傳回TRUE,否則傳回FALSE.查詢的結果存儲在類的共有成員變量m_pRecordset中45 //查詢結果的行數和列數分别儲存在共有成員變量m_nResultRow和m_nResultCol中

46 BOOL Select(CString strSql);47

48 //查詢語句,負責對僅僅查詢一個字段的情況進行處理49 //結果存放在CStringArray類型的變量pResult中

50 BOOL Select(CString strSql,CStringArray&Result);51

52 //對多個字段進行查詢

53 BOOL SelectMulitCol(CString strSql,CStringArray&Result);54

55 //打開表

56 BOOL OpenTable(CString strTable);57

58 //打開表

59 BOOL OpenTable(CString strTable,CStringArray&Result);60

61 //進行其它的更新操作

62 BOOL Execute(CString strSql);63 public:64 BOOL ExecuteTrans(CStringArray&aSql);65

66 //關閉結果集合

67 voidCloseRecordset();68

69 //得到操作結果的列數

70 longGetResultCol();71

72 //得到操作結果的條數

73 longGetResultRow();74

75 //得到操作結果

76 _RecordsetPtr GetResult();77 private:78

79 //資料庫操作傳回的結果條數

80 longm_nResultRow;81

82 //傳回的_RecordsetPtr中列數

83 longm_nResultCol;84

85 //連接配接指針

86 _ConnectionPtr m_pConnection;87

88 //指令執行指針

89 _CommandPtr m_pCommand;90

91 //結果集指針

92 _RecordsetPtr m_pRecordset;93 };94

95

98

99 //

100 //構造函數

101 //

102 //預設的構造函數

103 CPData::CPData()104 {105 //初始化

106 m_nResultRow = 0;107 m_nResultCol=0;108 m_pConnection =NULL;109 //建立對象

110 m_pRecordset.CreateInstance(_uuidof(Recordset));111 m_pCommand.CreateInstance(_uuidof(Command));112 }113

114 //傳入參數的構造函數

115 CPData::CPData(_ConnectionPtr pConnection)116 {117 m_pConnection =pConnection;118 m_nResultRow = 0;119 m_nResultCol=0;120 //建立對象

121 m_pRecordset.CreateInstance(_uuidof(Recordset));122 m_pCommand.CreateInstance(_uuidof(Command));123

124 }125 //

126 //析構函數

127 //

128 CPData::~CPData()129 {130 if(m_pRecordset->State!=adStateClosed)131 m_pRecordset->Close();132 m_pRecordset =NULL;133

134 if(m_pCommand->State!=adStateClosed)135 m_pCommand->Release();136 m_pCommand =NULL;137

138 if(m_pConnection->State!=adStateClosed)139 m_pConnection->Close();140 m_pConnection =NULL;141 }142

143 /

144 ///簡單操作函數145

146

147 //得到操作結果的行數

148 longCPData::GetResultRow()149 {150 return this->m_nResultRow;151 }152

153 //得到操作結果的列數

154 longCPData::GetResultCol()155 {156 return this->m_nResultCol;157 }158

159 //得到操作結果

160 _RecordsetPtr CPData::GetResult()161 {162 return this->m_pRecordset;163 }164

165 ///166 ///連接配接操作167 ///

168

169 //連接配接到資料庫170 //1.連接配接字元串可以自己構造,也可以從檔案中讀出

171 BOOL CPData::Connect(CString strUser,CString strPassword,CString strFileName,intnOptions,CString strConStr)172 {173 try{174 m_pConnection.CreateInstance(__uuidof(Connection));175 HRESULT hr;176 //如果用檔案方式配置資料源,進行配置

177 if(strFileName.Compare("")!=0&&CPFile::IsFileExist(strFileName))178 {179 CString con = "File Name="+strFileName;180 m_pConnection->ConnectionString =(_bstr_t)con;181 hr=m_pConnection->Open("","","",nOptions);182 }183 else

184 {185 //自己配置連接配接字元串

186 m_pConnection->ConnectionString =(_bstr_t)strConStr;187 hr=m_pConnection->Open("",_bstr_t(strUser),_bstr_t(strPassword),nOptions);188 }189 //進行連接配接190 //連接配接失敗

191 if(FAILED(hr))192 {193 AfxMessageBox("連接配接失敗!");194 returnFALSE;195 }196 }197 catch(_com_error&e)198 {199 AfxMessageBox(e.Description()+"B");200 returnFALSE;201 }202 returnTRUE;203 }204

205 //斷開連接配接

206 voidCPData::DisConnect()207 {208 if(m_pConnection->State!=adStateClosed)209 m_pConnection->Close();210 }211

212 ///213 ///更新操作214 ///

215 BOOL CPData::Execute(CString strSql)216 {217 try

218 {219 _variant_t vNULL;220 vNULL.vt =VT_ERROR;221

222 ///定義為無參數

223 vNULL.scode =DISP_E_PARAMNOTFOUND;224

225 ///非常關鍵的一句,将建立的連接配接指派給它

226 m_pCommand->ActiveConnection =m_pConnection;227

228 ///指令字串

229 m_pCommand->CommandText =(_bstr_t)strSql;230

231 ///執行指令,取得記錄集

232 m_pRecordset = m_pCommand->Execute(&vNULL,&vNULL,adCmdText);233

234 //确實,vNULL中的intVal是執行操作所影響的行數

235 m_nResultRow = 0;236 m_nResultRow =vNULL.intVal;237 }238 catch(_com_error&e)239 {240 m_nResultRow = 0;241 returnFALSE;242 }243 returnTRUE;244 }245

246 ///247 ///查詢操作248 ///

249 BOOL CPData::Select(CString strSql)250 {251 try

252 {253 m_nResultCol=0;254 m_nResultRow=0;255 m_pRecordset->CursorLocation=adUseClient; //設定遊标位置,設定為用戶端形式,否則GetRecordCount()傳回值不對

256 m_pRecordset->Open(_variant_t(strSql),_variant_t((IDispatch *)m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);257 m_nResultCol = m_pRecordset->Fields->GetCount();//得到查詢結果的列數

258 m_nResultRow = m_pRecordset->GetRecordCount(); //得到查詢結果的行數

259 }260 catch(_com_error&e)261 {262 AfxMessageBox(e.Description()+"D");263 returnFALSE;264 }265 returnTRUE;266 }267

268 //查詢語句,負責對僅僅查詢一個字段的情況進行處理269 //結果存放在CStringArray類型的變量pResult中

270 BOOL CPData::Select(CString strSql,CStringArray&Result)271 {272 if(Select(strSql)!=0)273 {274 Result.RemoveAll();275 for(int i=0;iFields->Item[(long)0]->Value;279 if(value.vt==3||value.vt==14)280 {281 CString strTrans;282 strTrans.Format("%ld",value.intVal);283 Result.Add(strTrans);284 }285 else

286 Result.Add(value.bstrVal);//287 m_pRecordset->MoveNext();288 }289 m_pRecordset->Close();290 returnTRUE;291 }292 else

293 {294 m_pRecordset->Close();295 returnFALSE;296 }297 }298

299 BOOL CPData::SelectMulitCol(CString strSql,CStringArray&Result)300 {301 if(Select(strSql)!=0)302 {303 Result.RemoveAll();304 _variant_t value;305 for(int i=0;iFields->Item[(long)(j)]->Value;310 if(value.vt==3||value.vt==14)311 {312 CString strTrans;313 strTrans.Format("%ld",value.intVal);314 Result.Add(strTrans);315 }316 else

317 if(value.vt==7)318 {319 COleDateTime time =value.date;320 CString strTemp;321 strTemp.Format("%d-%d-%d %s",time.GetYear(),time.GetMonth(),time.GetDay(),time.Format("%H:%M:%S"));322 Result.Add(strTemp);323 }324 else

325 Result.Add(value.bstrVal);//326 }327 m_pRecordset->MoveNext();328 }329 m_pRecordset->Close();330 returnTRUE;331 }332 else

333 {334 m_pRecordset->Close();335 returnFALSE;336 }337 }338

339 //打開整張表

340 BOOL CPData::OpenTable(CString strTable)341 {342 try

343 {344 m_nResultCol=0;345 m_nResultRow=0;346 m_pRecordset->CursorLocation=adUseClient; //設定遊标位置,設定為用戶端形式,否則GetRecordCount()傳回值不對

347 m_pRecordset->Open(_variant_t(strTable),_variant_t((IDispatch *)m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdTable);348 m_nResultCol = m_pRecordset->Fields->GetCount();//得到查詢結果的列數

349 m_nResultRow = m_pRecordset->GetRecordCount(); //得到查詢結果的行數

350 }351 catch(_com_error&e)352 {353 AfxMessageBox(e.Description()+"E");354 returnFALSE;355 }356 returnTRUE;357 }358

359 BOOL CPData::OpenTable(CString strTable,CStringArray&Result)360 {361 if(OpenTable(strTable)!=0)362 {363 Result.RemoveAll();364 _variant_t value;365 for(int i=0;iFields->Item[(long)(j)]->Value;370 if(value.vt==3||value.vt==14)371 {372 CString strTrans;373 strTrans.Format("%ld",value.intVal);374 Result.Add(strTrans);375 }376 else

377 if(value.vt==7)378 {379 COleDateTime time =value.date;380 CString strTemp;381 strTemp.Format("%d-%d-%d %s",time.GetYear(),time.GetMonth(),time.GetDay(),time.Format("%H:%M:%S"));382 Result.Add(strTemp);383 }384 else

385 Result.Add(value.bstrVal);//386 }387 m_pRecordset->MoveNext();388 }389 m_pRecordset->Close();390 returnTRUE;391 }392 else

393 {394 returnFALSE;395 }396 }397

398 /399 ///關閉結果集400 /

401 voidCPData::CloseRecordset()402 {403 if(m_pRecordset->State !=adStateClosed)404 m_pRecordset->Close();405 }406 BOOL CPData::ExecuteTrans(CStringArray&aSql)407 {408 try{409 int nNum =aSql.GetSize();410 m_pConnection->BeginTrans();411 for(int i=0;iExecute((_bstr_t)aSql.GetAt(i),NULL,adCmdText);415 }416 m_pConnection->CommitTrans();417 returnTRUE;418 }419 catch(_com_error&e)420 {421 m_pConnection->RollbackTrans();422 AfxMessageBox(e.Description()+"F");423 returnFALSE;424 }425 }