vc
++中使用mfc通過ado連線資料庫方法小結(不包括異常的捕捉)
這裡主要講mfc與access資料庫(sql2000方法差不多,主要是連線字串不同)
1、在stdafx
.h標頭檔案中加入此句子
#import
"c:\program files\common files\system\ado\msado15.dll"
no_namespace rename
("eof"
,"rseof"
)//不要命名空間,重命//名eof為rseof
這句話意味使用msado15
.dll動態連線庫檔案,裡面就有ado
2、實現從資料庫中提取顯示資料
coinitialize
(null
);
//初始化com元件
_connectionptr pconn
(__uuidof
(connection
));
//例項化乙個connection物件pconn
_recordsetptr prst
(__uuidof
(recordset
));
//例項化乙個recordset物件prst
_commandptr pcmd
(__uuidof
(command
));
//例項化乙個command物件pcmd
pconn
->
connectionstring
="provider=microsoft.jet.oledb.4.0;data source=student.mdb"
;//通過pconn物件連線字串,連線到access資料庫,這裡access資料庫在c根目錄下
pcmd
->
put_activeconnection
(_variant_t
((idispatch
*)pconn
));pcmd
->
commandtext
="select * from stu"
;
//通過pcmd物件訪問資料庫
prst
=pcmd
->
execute
(null
,null
,adcmdtext
);while
(!prst
->
rseof
)prst
->
close
();
//關閉物件
pconn
->
close
();pcmd
.release
();
//釋放物件
prst
.release
();pconn
.release
();couninitialize
();
//解除安裝com元件
3、實現插入記錄
coinitialize
(null
);_connectionptr pconn
(__uuidof
(connection
));_recordsetptr prst
(__uuidof
(recordset
));_commandptr pcmd
(__uuidof
(command
));_variant_t recordsaffected
;
//申請乙個_variant_t型別的的變數
pconn
->
connectionstring
="provider=microsoft.jet.oledb.4.0;data source=student.mdb"
;pconn
->
open(""
,"",""
,adconnectunspecified
);prst
=pconn
->
execute
("insert into stu (學號,姓名,**) values ('2222','dddd','123456')"
,&recordsaffected
,adcmdtext
);//prst->close(); //若有此句可以實現插入,但會產生runtime錯誤提示
pconn
->
close
();pcmd
.release
();prst
.release
();pconn
.release
();couninitialize
();4
、實現刪除記錄
coinitialize
(null
);_connectionptr pconn
(__uuidof
(connection
));_recordsetptr prst
(__uuidof
(recordset
));_commandptr pcmd
(__uuidof
(command
));_variant_t recordsaffected
;
//申請乙個_variant_t型別的的變數
pconn
->
connectionstring
="provider=microsoft.jet.oledb.4.0;data source=student.mdb"
;pconn
->
open(""
,"",""
,adconnectunspecified
);prst
=pconn
->
execute
("delete from stu where 學號='2'"
,&recordsaffected
,adcmdtext
);pconn
->
close
();pcmd
.release
();prst
.release
();pconn
.release
();couninitialize
();
VC 中使用MFC通過ADO連線資料庫方法小結
vc 中使用mfc通過ado連線資料庫方法小結 不包括異常的捕捉 這裡主要講mfc與access資料庫 sql2000方法差不多,主要是連線字串不同 1 在stdafx.h標頭檔案中加入此句子 import c program files common files system ado msado1...
MFC通過ADO連線SQL SERVER資料庫
以乙個mfc的dialog應用程式為例。1.首先是引入msado15.dll。在stdafx.h中加入 import c program files common files system ado msado15.dll no namespace rename eof adoeof 2.初始化com...
MFC通過ADO連線 SQL 2005
1 新建乙個類,型別為generic class,名稱為cdatabase 2 新增匯入檔案 import c program files common files system ado msado15.dll no namespace rename eof adoeof rename bof ad...