ado 利用智慧型指標進行資料庫操作

2021-06-18 01:28:22 字數 3895 閱讀 1373

在caboutdlg標頭檔案中定義兩個ado智慧型指標類例項,並在對話方塊中加入乙個listctrl。

_connectionptr m_pconnection;

_recordsetptr m_precordset;

clistctrl m_list; 

ado庫包含三個智慧型指標:_connectionptr、_commandptr和_recordsetptr。

_connectionptr通常被用來建立乙個資料連線或執行一條不返回任何結果的sql語句,如乙個儲存過程。

_commandptr返回乙個記錄集。它提供了一種簡單的方法來執行返回記錄集的儲存過程和sql語句。在使用_commandptr介面時,可以利用全域性_connectionptr介面,也可以在_commandptr介面裡直接使用連線串。_recordsetptr是乙個記錄集物件。與以上兩種物件相比,它對記錄集提供了更多的控制功能,如記錄鎖定、游標控制等。 

在使用ado程式的事件響應中onbutton1加入以下**: 

void cadotestdlg::onbutton1() 

m_precordset->close();

m_pconnection->close();

}catch (_com_error e)//異常處理

m_precordset->close(); //注意!!!不要多次關閉!!!!否則會出錯

m_pconnection->close();

m_precordset = null;

m_pconnection = null;

} 程式中通過_variant_t和_bstr_t轉換com物件和c++型別的資料, _variant_t類封裝了ole自治variant資料型別。在c++中使用_variant_t類要比直接使用variant資料型別容易得多。 

好,編譯後該程式就能執行了,但記住執行前要建立乙個叫adotest的odbc資料來源。該程式將把表middle中的big_name字段值顯示在列表控制項中。

4.執行sql命令並取得結果記錄集

為了取得結果記錄集,我們定義乙個指向recordset物件的指標:_recordsetptr m_precordset;

並為其建立recordset物件的例項: m_precordset.createinstance("adodb.recordset");

sql命令的執行可以採用多種形式,下面我們一進行闡述。

(1)利用connection物件的execute方法執行sql命令

execute方法的原型如下所示:

_recordsetptr connection15::execute ( _bstr_t commandtext, variant * recordsaffected, long      options ) 其中commandtext是命令字串,通常是sql命令。引數recordsaffected是操作完成後所影響的行數, 引數options表示commandtext中內容的型別,options可以取如下值之一:

adcmdtext:表明commandtext是文字命令

adcmdtable:表明commandtext是乙個表名

adcmdproc:表明commandtext是乙個儲存過程

adcmdunknown:未知

execute執行完後返回乙個指向記錄集的指標,下面我們給出具體**並作說明。 

_variant_t recordsaffected;

///執行sql命令:create table建立**users,users包含四個字段:整形id,字串username,整形old,日期型birthday

m_pconnection->execute("create table users(id integer,username text,old integer,birthday datetime)",&recordsaffected,adcmdtext);

///往**裡面新增記錄

m_pconnection->execute("insert into users(id,username,old,birthday) values (1, 'washington',25,'1970/1/1')",&recordsaffected,adcmdtext);

///將所有記錄old欄位的值加一

m_pconnection->execute("update users set old = old+1",&recordsaffected,adcmdtext);

///執行sql統計命令得到包含記錄條數的記錄集

m_precordset =  m_pconnection->execute("select count(*) from users",&recordsaffected,adcmdtext);

_variant_t vindex = (long)0;

_variant_t vcount = m_precordset->getcollect(vindex);///取得第乙個欄位的值放入vcount變數

上兩句可以寫成— _variant_t vcount = m_precordset->getcollect((_variant_t)((long)0));

m_precordset->close();///關閉記錄集

cstring message;

message.format("共有%d條記錄",vcount.lval);

afxmessagebox(message);///顯示當前記錄條數

(2)利用command物件來執行sql命令

_commandptr m_pcommand;

m_pcommand.createinstance("adodb.command");

_variant_t vnull;

vnull.vt = vt_error;

vnull.scode = disp_e_paramnotfound;///定義為無引數

m_pcommand->activeconnection = m_pconnection;///非常關鍵的一句,將建立的連線賦值給它

m_pcommand->commandtext = "select * from users";///命令字串

m_precordset = m_pcommand->execute(&vnull,&vnull,adcmdtext);///執行命令,取得記錄集

在這段**中我們只是用command物件來執行了select查詢語句,command物件在進行儲存過程的呼叫中能真正體現它的作用。下次我們將詳細介紹。 

(3)直接用recordset物件進行查詢取得記錄集 

例項——

void cgmsadlg::ondbselect() 

{// todo: add your control notification handler code here

_recordsetptr rs1;  //定義recordset物件

_bstr_t connect("dsn=gms;uid=sa;pwd=;");//定義連線字串

_bstr_t source ("select count(*) from buaa.mdb010");  //要執行的sql語句

::coinitialize(null); //初始化rs1物件

hresul hr = rs1.createinstance( __uuidof( recordset ) );

//省略對返回值hr的判斷 

rs1->open( source,

connect,

adopenforwardonly,

adlockreadonly,

-1 ); 

_variant_t temp=rs1->getcollect(_variant_t((long)0));

cstring strtemp=(char* )(_bstr_t)temp;

messagebox("ok!"+strtemp);

ADO進行資料庫開發 轉貼

1.匯入ado庫 在stdafx.h中,加入如下 import c program files common files system ado msado15.dll no namespace rename eof adoeof rename bof adobof afxoleinit mfc 或者...

利用游標進行資料庫資料備份

今天同事問我乙個問題 他需要每天將tablea中指定條件下的資料copy到tableb中,並刪除tableb中已被copy的資料,應該用什麼方法高效一些?聽到這個方法之後第一反映就是想到在我上家公司的時候,也做了同樣的資料備份工作,不過當時指令碼是我老大寫的,我只是看了一眼,大概是將需要備份的資料查...

Python進行資料庫操作

python要對資料庫進行操作,首先要進行 python sql 連線,在 python 中進行資料庫連線的模組有 mysqldb pymysql,兩種連線方式用法一樣。以pymysql為例進行講解 安裝方法 linux ubuntu sudo pip install pymysql windows...