MFC ADO資料庫操作

來源:互聯網
上載者:User

內容比較亂,作為草稿,對現有的ado資料庫操作函數方法進行匯總。

小函數
m_pRecordset->RecordCount//取得記錄數量

全域變數
#import "msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF")
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
1.串連資料來源OnInitADOConn();
//初始化OLE/COM庫環境
::CoInitialize(NULL);
try{
//建立Connection對象
m_pCOnnection.CreateInstance("ADODB.Connection");
//設定連接字串
_bstr_t strConnect="uid=;pwd=;DRIVER={Microsoft Access Driver(*mdb)};DBQ=database.mdb;";
m_pConnection->Open(strConnect,"","",adModeUnknown);
}
catch(_com_error e){
AfxMessageBox(e.Description);
}

2.關閉資料庫連接
if(m_pRecordset!=NULL)
 m_pRecordset->Close();
m_pConnection->Close();
::CoUninitialize();//釋放環境

3.擷取記錄集資料
傳入一個_bstr_t bstrSQL
(1)Open方法
try{
if(m_pConnection==NULL)//如果為空白,重新串連
 OnInitADOConn();
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}//adCmdText:bstrSQL是文本.adCmdTable:bstrSQL是表名
(2)Execute方法
_RecordsetPtr Execute(_bstr_t CommandTex,VARIANT* RecordsAffeced,long Options)
CommandText是SQL命令,RecordsAffeced是操作後影響的行數,Options是CommandText中內容的類型
Options:adCmdText文本,adCmdTable表名,adCmdStoredProc預存程序,adCmdUnknown類型未知
>>_variant_t RecordsAffected;
try{
if(m_pConnection==NULL)//是否串連資料庫
 OnInitADOConn();//又重新串連
 m_pConnection->Execute(bstrSQL,NULL,adCmdText);
}
catch(_com_error e)
{
e.Description();
return false;
}

4.遍曆記錄集
MoveNext,MoveFirst,MoveLast,MovePrevious;
(char *)(_bstr_t)m_pRecordset->GetCollect("姓名");//方法一
m_sName=(CStringW)(m_pAdoRecordset->Fields->Item[_variant_t("NAME")]->Value);//方法二
while(m_pRecordset->adoEOF==0)
{m_pRecordset->MoveNext();}
5.添加資料
try{
m_pRecordset->AddNew();//開始添加
m_pRecordset->PutCollect("列名",(_bstr_t)m_id);
m_pRecordset-->Update();//更新欄位
}
6.修改資料
try{
m_pRecordset->Move((long)pos,vtMissing);
m_pRecordset->PutCollect("姓名",(_bstr_t)m_name);
m_pRecordset-->Update();
}
7.刪除資料
m_pRecordset->Move((long)pos,vtMissing);
m_pAdoRecordset->Delete(adAffectCurrent);
m_pRecordset-->Update();
    try
    {
       //假設刪除第10條記錄
       m_pRecordset->MoveFirst();//注意一定要移到第一個
       m_pRecordset->Move(9);
       m_pRecordset->Delete(adAffectCurrent);
       //參數adAffectCurrent為刪除目前記錄
       m_pRecordset->Update();
    }
    catch(_com_error *e)
    {
       AfxMessageBox(e->ErrorMessage());
    }
8.儲存圖片( 參考下一篇部落格)
 char *m_pBuffer;//檔案資料
 DWORD m_filelen;//檔案長度
 VARIANT varblob;
 SAFEARRAY *psa;
 SAFEARRAYBOUND rgsabound[1];
 rgsabound[0].lLbound=0;
 rgsabound[0].cElements=m_filelen;
 psa=SafeArrayCreate(VT_UI1,1,rgsabound);
 for(long i=0;i<(long)m_filelen;i++)
 {
  SafeArrayPutElement(psa,&i,m_pBuffer++);
 }
 //記錄值
 varblob.vt=VT_ARRAY|VT_UI1;
 varblob.parray=psa;
 m_pRecordset->GetFields()->GetItem("PHOTO_DATA")->AppendChunk(varblob);
 m_pRecordset->Update();
讀取資料庫語音資料
long lDataSize=m_pRecordset->GetFields()->GetItem("voice")->ActualSize;//取得資料區域大小
char *m_pBuffer;
if(lDataSize>0)
{
//讀取資料到varBLOB中
_variant_t varBLOB;
varBLOB=m_pRecordse->GetFields()->GetItem("voice")->GetChunk(lDataSize);
if(varBLOB.vt==(VT_ARRAY|VT_UI1))
 {
 if(m_pBuffer=new char[lDataSize+1])
 {
 char *pBuf=0;
 SafeArrayAccessData(varBLOB.parray,(void**)&pBuf);
 memcpy(m_pBuffer,pBuf,lDataSize);//賦值資料到m_pBuffer
 SafeArrayUnaccessData(varBLOB.parray);
 }
 }
}

其他串連方法
開啟
 m_pAdoRecordset=NULL;
 m_pAdoConnect=NULL;//初始化
 iCurrentOne=-1;
 //*******************************************
 if(FAILED(::CoInitialize(NULL)))//這句話很重要!
 {
  ::AfxMessageBox(_T("fail to CInitialize(NULL)"));
  PostQuitMessage(-8);//?
 }
 HRESULT hr=m_pAdoConnect.CreateInstance(__uuidof(Connection));
 if(FAILED(hr)){
  ::AfxMessageBox(_T("fail to create instance for _ConnectPtr"));
  PostQuitMessage(-8);
 }
 bstr_t strConnect="DSN=FRDB;\
       DBQ=H:\\FRDB.accdb;\
       DriverID=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;";
 try{
  m_pAdoConnect->Open(strConnect,"clc","",NULL);
 }
 catch(_com_error &e)
 {
  ::AfxMessageBox(e.Description());
  PostQuitMessage(-8);
 }
 m_pAdoRecordset=NULL;
 hr=m_pAdoRecordset.CreateInstance(__uuidof(Recordset));
 if(FAILED(hr)){
  ::AfxMessageBox(_T("fail to create instance for _RecordsetPtr"));
  PostQuitMessage(-8);
 }
 m_pAdoRecordset->Open(_variant_t("Person"),_variant_t((IDispatch*)

m_pAdoConnect,true),adOpenKeyset,adLockOptimistic,adCmdTable);
關閉
 if(m_pAdoRecordset){ if(m_pAdoRecordset->State==adStateOpen){
   m_pAdoRecordset->Close(); }}
 if(m_pAdoConnect)
 { if(m_pAdoConnect->State==adStateOpen){
   m_pAdoConnect->Close();}}
  ::CoUninitialize();//關閉線程下的資料庫
添加資料
m_pAdoRecordset->Fields->GetItem(_variant_t("NAME"))->Value=_variant_t(m_sName);
載入資料
m_sName=(CStringW)(m_pAdoRecordset->Fields->Item[_variant_t("NAME")]->Value);

 

http://blog.hi.mop.com/blog/14401326/2743719.html

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.