VC中使用ADO共同完成數(shù)據(jù)庫的設(shè)置
發(fā)表時間:2023-07-29 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]ADO是應(yīng)用層的編程接口,它通過OLE DB提供的COM接口訪問數(shù)據(jù),它適合于各種客戶機/服務(wù)器應(yīng)用系統(tǒng)和基于Web的應(yīng)用,尤其在一些腳本語言中訪問數(shù)據(jù)庫操作是ADO的主要優(yōu)勢。ADO是一套用自動化...
ADO是應(yīng)用層的編程接口,它通過OLE DB提供的COM接口訪問數(shù)據(jù),它適合于各種客戶機/服務(wù)器應(yīng)用系統(tǒng)和基于Web的應(yīng)用,尤其在一些腳本語言中訪問數(shù)據(jù)庫操作是ADO的主要優(yōu)勢。ADO是一套用自動化技術(shù)建立起來的對象層次結(jié)構(gòu),它比其他的一些對象模型如DAO(Data Access Object)、RDO(Remote Data Object)等具有更好的靈活性,使用更為方便,并且訪問數(shù)據(jù)的效率更高。SQL是強大的數(shù)據(jù)庫操作系統(tǒng),通過ADO和SQL語句的配合,我們可以的實現(xiàn)對數(shù)據(jù)庫的一系列操作,例如創(chuàng)建數(shù)據(jù)庫、創(chuàng)建表、創(chuàng)建索引,實現(xiàn)數(shù)據(jù)庫的多重查詢、高級查詢和數(shù)據(jù)的匯總等技術(shù)。下面通過例程介紹如何通過ADO和SQL語句的配合實現(xiàn)對數(shù)據(jù)庫的操作。
第一步:通過Access創(chuàng)建數(shù)據(jù)庫test.mdb。
第二步:創(chuàng)建單文檔工程testado,所有的選項都取默認值。
第三步:COM庫的初始化
我們可以使用AfxOleInit()來初始化COM庫,這項工作通常在CWinApp::InitInstance()的重載函數(shù)中完成,請看如下代碼:
BOOL CADOTest1App::InitInstance()
{
AfxOleInit();
......
第四步:用#import指令引入ADO類型庫
我們在stdafx.h中加入如下語句:
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
這一語句有何作用呢?其最終作用同我們熟悉的#include類似,編譯的時候系統(tǒng)會為我們生成msado15.tlh,ado15.tli兩個C++頭文件來定義ADO庫。
第五步:在testadoview.h中定義一個指向Connection對象的指針:_ConnectionPtr _pConnection;
第六步:添加如下代碼:
void CTestadoView::OnInitialUpdate()
{
CView::OnInitialUpdate();
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");//創(chuàng)建Connection對象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb","","",adModeUnknown);///連接數(shù)據(jù)庫
///上面一句中連接字串中的Provider是針對ACCESS2000環(huán)境的,對于ACCESS97,需要改為:Provider=Microsoft.Jet.OLEDB.3.51; }
}
}
catch(_com_error e)///捕捉異常
{
CString errormessage;
errormessage.Format("連接數(shù)據(jù)庫失敗!\r\n錯誤信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///顯示錯誤信息
}
}
第七步:在析構(gòu)函數(shù)中關(guān)閉Connection對象并將其釋放,代碼如下:
CTestadoView::~CTestadoView()
{
m_pConnection->Close();
m_pConnection.Release();
}
第八步:添加菜單項"創(chuàng)建數(shù)據(jù)庫表",并添加相應(yīng)的消息處理函數(shù),然后添加代碼如下:
void CTestadoView::OnAddtable()
{
_variant_t RecordsAffected;
m_pConnection->Execute("CREATE TABLE new(ID INTEGER,username TEXT,old INTEGER)",&RecordsAffected,adCmdText);
}
運行程序,執(zhí)行菜單當中的命令"添加表",我們可以發(fā)現(xiàn)數(shù)據(jù)庫中已經(jīng)添加了一個表new,其中的字段有我們定義的字段。
第九步:添加菜單項"刪除數(shù)據(jù)庫表",并添加相應(yīng)的消息處理函數(shù),然后添加代碼如下:
void CTestadoView::OnDeleteTable()
{
_variant_t RecordsAffected;
m_pConnection->Execute("DROP TABLE new",&RecordsAffected,adCmdText);
}
運行程序,執(zhí)行菜單當中的命令"刪除表",我們可以發(fā)現(xiàn)數(shù)據(jù)庫中剛才添加的表new已被刪除。
第十步:添加菜單項"添加一列",并添加相應(yīng)的消息處理函數(shù),然后添加代碼如下:
void CTestadoView::OnAddColumn()
{
_variant_t RecordsAffected;
m_pConnection->Execute("ALTER TABLE new ADD newcolumn1 INTEGER",&RecordsAffected,adCmdText);
}
運行程序,執(zhí)行菜單當中的命令"添加一列",我們可以發(fā)現(xiàn)數(shù)據(jù)庫中剛才添加的表new中已添加了一個新列。
第十一步:添加菜單項"刪除一列",并添加相應(yīng)的消息處理函數(shù),然后添加代碼如下:
void CTestadoView::OnAddColumn()
{
_variant_t RecordsAffected;
m_pConnection->Execute("ALTER TABLE new ADD newcolumn1 INTEGER",&RecordsAffected,adCmdText);
}
運行程序,執(zhí)行菜單當中的命令"刪除一列",我們可以發(fā)現(xiàn)數(shù)據(jù)庫中剛才添加的表new中的新列已被刪除。
第十二步:添加菜單項"添加記錄",并添加相應(yīng)的消息處理函數(shù),然后添加代碼如下:
void CTestadoView::OnAddRecord()
{
_variant_t RecordsAffected;
for(int i = 1;i < 10; i ++)
{
CString strSQL;
strSQL.Format("INSERT INTO new(ID,username,old) VALUES (%d, 'Washington',%d)",i,i*9);
m_pConnection->Execute((_bstr_t)strSQL,&RecordsAffected,adCmdText);
}
運行程序,執(zhí)行菜單當中的命令"添加記錄",我們可以發(fā)現(xiàn)數(shù)據(jù)庫中剛才添加的表new中添加了九條新的記錄。
第十三步:添加菜單項"old字段加1",并添加相應(yīng)的消息處理函數(shù),然后添加代碼如下:
void CTestadoView::OnOldAddone()
{
_variant_t RecordsAffected;
m_pConnection->Execute("UPDATE new SET old = old+1",&RecordsAffected,adCmdText);
}}
運行程序,執(zhí)行菜單當中的命令"old記錄加1",我們可以發(fā)現(xiàn)數(shù)據(jù)庫中剛才添加的表new中的九條新的記錄的old字段都自動加1。
第十四步:添加菜單項"統(tǒng)計記錄數(shù)目",并添加相應(yīng)的消息處理函數(shù),然后添加代碼如下:
void CTestadoView::OnTotalRecords()
{
_RecordsetPtr m_pRecordset;
_variant_t RecordsAffected;
m_pRecordset =m_pConnection->Execute("SELECT COUNT(*) FROM new where ID > 0",&RecordsAffected,adCmdText);
_variant_t vIndex = (long)0;
_variant_t vCount = m_pRecordset->GetCollect(vIndex);
///取得第一個字段的值放入vCount變量
m_pRecordset->Close();///關(guān)閉記錄集
CString Message;
Message.Format("共有%d條記錄",vCount.lVal);
AfxMessageBox(Message);///顯示當前記錄條數(shù)
}
運行程序,執(zhí)行菜單當中的命令"統(tǒng)計記錄數(shù)目",我們可以得到數(shù)據(jù)庫中記錄的數(shù)目。
第十五步:添加菜單項"設(shè)置ID為索引",并添加相應(yīng)的消息處理函數(shù),然后添加代碼如下:
void CTestadoView::OnSetIdIndex()
{
_variant_t RecordsAffected;
m_pConnection->Execute("CREATE UNIQUE INDEX id ON new(ID)",&RecordsAffected,adCmdText);
}
運行程序,執(zhí)行菜單當中的命令"設(shè)置ID為索引",我們可以發(fā)現(xiàn)數(shù)據(jù)庫中ID被設(shè)置為索引。
第十六步:添加菜單項"數(shù)據(jù)匯總"、"old字段的總和"、"old字段的均值"、"old的最小值"、"old字段的最大值",并添加相應(yīng)的消息處理函數(shù),然后添加代碼如下:
void CTestadoView::OnOldMax()
{
_RecordsetPtr m_pRecordset;
_variant_t RecordsAffected;
m_pRecordset =m_pConnection->Execute("select MAX(old) from new",&RecordsAffected,adCmdText);
_variant_t vIndex = (long)0;
_variant_t vCount = m_pRecordset->GetCollect(vIndex);
///取得第一個字段的值放入vCount變量
m_pRecordset->Close();///關(guān)閉記錄集
m_pRecordset.Release();
CString Message;
Message.Format("最大值是%d",vCount.lVal);
AfxMessageBox(Message);
}
void CTestadoView::OnOldMin()
{
_RecordsetPtr m_pRecordset;
_variant_t RecordsAffected;
m_pRecordset =m_pConnection->Execute("select MIN(old) from new",&RecordsAffected,adCmdText);
_variant_t vIndex = (long)0;
_variant_t vCount = m_pRecordset->GetCollect(vIndex);
///取得第一個字段的值放入vCount變量
m_pRecordset->Close();///關(guān)閉記錄集
m_pRecordset.Release();
CString Message;
Message.Format("最小值是%d",vCount.lVal);
AfxMessageBox(Message);
}
void CTestadoView::OnOldTotal()
{
_RecordsetPtr m_pRecordset;
_variant_t RecordsAffected;
m_pRecordset =m_pConnection->Execute("select SUM(old) from new",&RecordsAffected,adCmdText);
_variant_t vIndex = (long)0;
_variant_t vCount = m_pRecordset->GetCollect(vIndex);
///取得第一個字段的值放入vCount變量
m_pRecordset->Close();///關(guān)閉記錄集
m_pRecordset.Release();
CString Message;
Message.Format("總和是%d",(long)vCount);
AfxMessageBox(Message);
}
void CTestadoView::OnOldAverage()
{
_RecordsetPtr m_pRecordset;
_variant_t RecordsAffected;
m_pRecordset =m_pConnection->Execute("select AVG(old) from new",&RecordsAffected,adCmdText);
_variant_t vIndex = (long)0;
_variant_t vCount = m_pRecordset->GetCollect(vIndex);
///取得第一個字段的值放入vCount變量
m_pRecordset->Close();///關(guān)閉記錄集
m_pRecordset.Release();
CString Message;
Message.Format("平均值是%d",(long)vCount);
AfxMessageBox(Message);
}}
運行程序,執(zhí)行菜單當中的匯總命令,我們可以得到相關(guān)的匯總信息。