(1)通過JET數(shù)據(jù)庫引擎對ACCESS2000數(shù)據(jù)庫的連接
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\test.mdb","","",adModeUnknown);
(2)通過DSN數(shù)據(jù)源對任何支持ODBC的數(shù)據(jù)庫進行連接:
m_pConnection->Open("Data Source=adotest;UID=sa;PWD=;","","",adModeUnknown);
(3)不通過DSN對SQL SERVER數(shù)據(jù)庫進行連接:
m_pConnection->Open("driver={SQL Server};Server=127.0.0.1;DATABASE=vckbase;UID=sa;PWD=139","","",adModeUnknown);
其中Server是SQL服務(wù)器的名稱,DATABASE是庫的名稱
Connection對象除Open方法外還有許多方法,我們先介紹Connection對象中兩個有用的屬性ConnectionTimeOut與State ConnectionTimeOut用來設(shè)置連接的超時時間,需要在Open之前調(diào)用,例如:
m_pConnection->ConnectionTimeout = 5;///設(shè)置超時時間為5秒 m_pConnection->Open("Data Source=adotest;","","",adModeUnknown);
State屬性指明當(dāng)前Connection對象的狀態(tài),0表示關(guān)閉,1表示已經(jīng)打開,我們可以通過讀取這個屬性來作相應(yīng)的處理,例如:
if(m_pConnection->State) m_pConnection->Close(); ///如果已經(jīng)打開了連接則關(guān)閉它
|