這個版本比原先發的要更加全面一些,以下的**均經過驗證。
準備:(1)、引入ado類
#import "c:/program files/common files/system/ado/msado15.dll" /
no_namespace /
rename ("eof", "adoeof")
(2)、初始化com
在mfc中可以用afxoleinit();非mfc環境中用:
coinitialize(null);
couninitialize();
(3)#import 包含後就可以用3個智慧型指標了:_connectionptr、_recordsetptr和_commandptr
1.連線和關閉資料庫 (1)連線
例子:連線access資料庫
afxoleinit();//初始化
hresult hr;
try}
catch(_com_error e)///捕捉異常
(2)、關閉
//如果資料庫連線有效
if( m_pconnection->state )
m_pconnection->close();
m_pconnection = null;
(3)、設定連線時間 //設定連線時間-----------------------------------
pconnection->put_connectiontimeout(long(5));
2.開啟乙個結果集
(1)開啟,首先建立乙個_recordsetptr例項,然後呼叫open()得到一條sql語句的執行結果
_recordsetptrm_precordset;
m_precordset.createinstance(__uuidof(recordset)); // 在ado操作中建議語句中要常用try...catch()來捕獲錯誤資訊,
// 因為它有時會經常出現一些意想不到的錯誤。
trycatch(_com_error *e)
(2)關閉結果集
m_precordset->close();
3.操作乙個結果集
(1)、遍歷(讀取)
a)、用precordset->adoeof來判斷資料庫指標是否已經移到結果集的末尾了;m_precordset->bof判斷是否 在第一條記錄前面:
while(!m_precordset->adoeof)
b)、取得乙個欄位的值的辦法有兩種辦法:
這裡取得的是例項值,而不是標題一是
//表示取得第0個字段的值 m_precordset->getcollect("name");
或者 m_precordset->getcollect(_variant_t(long(0));
二是precordset->get_collect("column_name");
或者 precordset->get_collect(long(index));
(2)、新增
a)、呼叫m_precordset->addnew();
b)、呼叫m_precordset->putcollect();給每個字段賦值
c)、呼叫m_precordset->update();確認
(3)、修改
(4)、刪除
a)、把記錄指標移動到要刪除的記錄上,然後呼叫
delete(adaffectcurrent) try
catch(_com_error *e)
4.直接執行sql語句,除了要用到結果集其餘的大部分功能都可以直接用sql語言實現
(1)、用_commandptr和_recordsetptr配合
_commandptrm_pcommand;
m_pcommand.createinstance(__uuidof(command));
// 將庫連線賦於它
m_pcommand->activeconnection = m_pconnection;
// sql語句
m_pcommand->commandtext = "select * from demotable";
// 執行sql語句,返回記錄集
m_precordset = m_pcommand->execute(null, null,adcmdtext);
(2)、直接用_connectionptr執行sql語句
_recordsetptr connection15::execute ( _bstr_t commandtext,
variant * recordsaffected,
long options )
其中commandtext是命令字串,通常是sql命令。
引數recordsaffected是操作完成後所影響的行數,
引數options表示commandtext中內容的型別,options可以取如下值之一:
adcmdtext:表明commandtext是文字命令
adcmdtable:表明commandtext是乙個表名
adcmdproc:表明commandtext是乙個儲存過程
adcmdunknown:未知
例子:_variant_t recordsaffected;
m_pconnection->execute("update users set old = old+1",&recordsaffected,adcmdtext);
5.呼叫儲存過程
(1)、利用_commandptr
_commandptrm_pcommand;
m_pcommand.createinstance(__uuidof(command));
m_pcommand->activeconnection = m_pconnection; // 將庫連線賦於它
m_pcommand->commandtext = "demo";
m_pcommand->execute(null,null, adcmdstoredproc);
(2)、直接用_connectionptr直接呼叫
6.遍歷資料庫中的所有表名
_recordsetptr pset;
hresult hr;
try
pset->movenext();
} pset->close();
} m_pconnect->close();
}catch(_com_error e)///捕捉異常
7.遍歷乙個表中的所有字段
field * field = null;
long colcount=0;
hresult hr;
fields * fields = null;
cstring temp;
hr = m_precordset->get_fields(&fields);//得到記錄集的字段集和
if(succeeded(hr))
fields->get_count(&colcount);
//得到記錄集的字段集合中的字段的總個數
bstr bstr;
for(i=0;iitem[(long)i];
field->get_name(&maf);
temp = bstr;
afxmessagebox(temp);//輸出各個欄位的標題
}if(succeeded(hrr))
fields->release();//釋放指標
附:1、_variant_t
(1)、一般傳給這3個指標的值都不是mfc直接支援的資料型別,而要用_variant_t轉換一下
_variant_t(xx)可以把大多數型別的變數轉換成適合的型別傳入:
(2)、_variant_t var;_variant_t -> long: (long)var;
_variant_t -> cstring: cstring strvalue = (lpcstr)_bstr_t(var);
cstring -> _variant_t: _variant_t(strsql);
2、bstr寬字串與cstring相互轉換
bstr bstr;
cstring strsql;
cstring -> bstr: bstr = strsql.allocsysstring();
bstr -> cstring: strsql = (lpcstr)bstr;
3、_bstr_t與cstring相互轉換
_bstr_t bstr;
cstring strsql;
cstring -> _bstr_t: bstr = (_bstr_t)strsql;
_bstr_t -> cstring: strsql = (lpcstr)bstr;
4、關於時間
access:表示時間的字串#2004-4-5#
sql:表示時間的字串''2004-4-5''
datefield(時間字段) select * from my_table where datefield > #2004-4-10# try
catch(_com_error e)///捕捉異常
VC6 0中使用ADO操作Access資料庫
由於我的程式只是簡單的儲存 和標題,access小而簡單,所以就選擇access作為本小軟體的資料庫,並採用ado訪問資料庫。以下資料庫內容摘自孫鑫老師的 vc20講第20課資料庫訪問的ppt 資料庫訪問技術 1.odbc open database connectivity 開放資料庫互連。odb...
VC6 0中使用ADO操作Access資料庫
ado提供了一組非常簡單,將一般通用的資料訪問細節進行封裝的物件。由於odbc資料來源也提供了一般的ole db privider,所以ado不僅可以應用自身的ole db privider,而且還可以應用所有的odbc驅動程式。關於ole db和ado的其它詳細情況,讀者可以自行查閱相關書籍或ms...
VC6 0中使用64位整型
vc6.0通常用於編寫32位的程式,但是偶爾也需要用到64位整型以實現更精確的計算,在vc6.0中64位整型是用 int64定義的,如下為使用該型別資料計算階乘求和的乙個例子。需要注意的是printf要輸出64位整型數,引數應為 i64u。include int main printf i64u n...