使用#import方法的編程步驟
這裡建議您使用#import的方法,因為它易學、易用,代碼也比較簡潔。
1、 添加#import指令
開啟stdafx.h檔案,將下列內容添加到所有的include指令之後:
#include <icrsint.h> //Include support for VC++ Extensions
#import "C:/Program Files/Common Files/System/ADO/msado15.dll" /
no_namespace rename("EOF", "adoEOF")
其中icrsint.h檔案包含了VC++擴充的一些預先處理指令、宏等的定義,用於COM編程時使用。
2、定義_ConnectionPtr型變數,並建立資料庫連接
建立了與資料庫伺服器的串連後,才能進行其他有關資料庫的訪問和操作。ADO使用Connection對象來建立與資料庫伺服器的串連,所以它相當於MFC中的CDatabase類。和CDatabase類一樣,調用Connection對象的Open方法即可建立與伺服器的串連。
資料類型 _ConnectionPtr實際上就是由類模板_com_ptr_t而得到的一個具體的執行個體類,其定義可以到msado15.tlh、comdef.h 和comip.h這三個檔案中找到。在msado15.tlh中有:_COM_SMARTPTR_TYPEDEF(_Collection, __uuidof(_Collection));
經宏擴充後就得到了_ConnectionPtr類。_ConnectionPtr類封裝了Connection對象的Idispatch介面指標,及一些必要的操作。我們就是通過這個指標來操縱Connection對象。類似地,後面用到的_CommandPtr和_RecordsetPtr類型也是這樣得到的,它們分別表示命令對象指標和記錄集對象的指標。
(1)、串連到MS SQL Server
注意連接字串的格式,提供正確的連接字串是成功串連到資料庫伺服器的第一步,有關連接字串的詳細資料參見微軟MSDN Library光碟片。
本例連接字串中的server_name,database_name,user_name和password在編程時都應該替換成實際的內容。
_ConnectionPtr pMyConnect=NULL;
HRESULT hr=pMyConnect.CreateInstance(__uuidof(Connection)));
if(FAILED(hr))return;
_bstr_t strConnect="Provider=SQLOLEDB; Server=server_name;"
"Database=database_name; uid=user_name; pwd=password;";
//connecting to the database server now:
try{pMyConnect->Open(strConnect,"","",NULL);}
catch (_com_error &e)
{
::MessageBox(NULL,e.Description(),"警告",MB_OK │ MB_ICONWARNING);
}
注意Connection對象的Open方法中的連接字串參數必須是BSTR或_bstr_t類型。另外,本例是直接通過OLE DB Provider建立串連,所以無需建立資料來源。
(2)、通過ODBC Driver串連到Database Server連接字串格式與直接用ODBC編程時的差不多:
_bstr_t strConnect="DSN=datasource_name; Database=database_name; uid=user_name; pwd=password;";
此時與ODBC編程一樣,必須先建立資料來源。
3、定義_RecordsetPtr型變數,並開啟資料集
定義_RecordsetPtr型變數,然後通過它調用Recordset對象的Open方法,即可開啟一個資料集。所以Recordset對象與MFC中的CRecordset類類似,它也有目前記錄、目前記錄指標的概念。如:
_RecordsetPtr m_pRecordset;
if(!FAILED(m_pRecordset.CreateInstance( __uuidof( Recordset )))
{
m_pDoc->m_initialized=FALSE;
return;
}
try{
m_pRecordset->Open(_variant_t("mytable"),
_variant_t((IDispatch *)pMyConnect,true), adOpenKeyset,
adLockOptimistic, adCmdTable);
}
catch (_com_error &e)
{
::MessageBox(NULL,"無法開啟mytable表。","提示",
MB_OK │ MB_ICONWARNING);
}
Recordset對象的Open方法非常重要,它的第一個參數可以是一個SQL語句、一個表的名字或一個命令對象等等;第二個參數就是前面建立的連線物件的指標。此外,用Connection和Command對象的Execute方法也能得到記錄集,但是唯讀。
#import "c:/program files/common files/system/ado/msado15.dll" no_namespace rename("EOF","adoEOF")
#include <icrsint.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
void main(){
if(FAILED(CoInitialize(NULL)))return;
_ConnectionPtr pMyConnect=NULL;
HRESULT hr=pMyConnect.CreateInstance(__uuidof(Connection));
if(FAILED(hr))return;
if(pMyConnect->State) {
pMyConnect->Close();
pMyConnect= NULL;
}
try{
pMyConnect->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E://data.mdb", "","",adModeUnknown);
}
catch (_com_error &e)
{
}
//open the recordset:
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
try{
m_pRecordset->Open("SELECT * FROM student", pMyConnect.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
}
catch (_com_error &e)
{
}
string strName,strAge;
_variant_t var;
while(!m_pRecordset->adoEOF) {
strName=(string)(_bstr_t)m_pRecordset->Fields->GetItem("name")->Value;
cout<<strName;
m_pRecordset->MoveNext();
}
}