void CTestadoView::OnInitialUpdate() { CView::OnInitialUpdate(); HRESULT hr; try { hr = m_pConnection.CreateInstance("ADODB.Connection");//创建Connection对象 if(SUCCEEDED(hr)) { hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb","","",adModeUnknown);///连接数据库 ///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51; } } } catch(_com_error e)///捕捉异常 { CString errormessage; errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage()); AfxMessageBox(errormessage);///显示错误信息 } } |
CTestadoView::~CTestadoView() { m_pConnection->Close(); m_pConnection.Release(); } |
void CTestadoView::OnAddtable() { _variant_t RecordsAffected; m_pConnection->Execute("CREATE TABLE new(ID INTEGER,username TEXT,old INTEGER)",&RecordsAffected,adCmdText); } |
void CTestadoView::OnDeleteTable() { _variant_t RecordsAffected; m_pConnection->Execute("DROP TABLE new",&RecordsAffected,adCmdText); } |
void CTestadoView::OnAddColumn() { _variant_t RecordsAffected; m_pConnection->Execute("ALTER TABLE new ADD newcolumn1 INTEGER",&RecordsAffected,adCmdText); } |
void CTestadoView::OnAddColumn() { _variant_t RecordsAffected; m_pConnection->Execute("ALTER TABLE new ADD newcolumn1 INTEGER",&RecordsAffected,adCmdText); } |
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); } |
void CTestadoView::OnOldAddone() { _variant_t RecordsAffected; m_pConnection->Execute("UPDATE new SET old = old+1",&RecordsAffected,adCmdText); }} |
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();///关闭记录集 CString Message; Message.Format("共有%d条记录",vCount.lVal); AfxMessageBox(Message);///显示当前记录条数 } |
void CTestadoView::OnSetIdIndex() { _variant_t RecordsAffected; m_pConnection->Execute("CREATE UNIQUE INDEX id ON new(ID)",&RecordsAffected,adCmdText); } |
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();///关闭记录集 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();///关闭记录集 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();///关闭记录集 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();///关闭记录集 m_pRecordset.Release(); CString Message; Message.Format("平均值是%d",(long)vCount); AfxMessageBox(Message); }} |
首先我们需要包含头文件afxdb.h,可以将#include
但执行成功后,可以调用以下的函数滚动光标,读取数据。
MoveFirst | 移动光标到第一条记录处 |
MoveNext | 移动光标到后一条记录处 |
MovePrev | 移动光标到前一条记录处 |
MoveLast | 移动光标到最后一条记录处 |
IsBOF | 检测光标是否在第一条记录上 |
IsEOF | 检测光标是否在最后一条记录上 |
GetFieldValue | 得到结果中数据 |
假设CDatabase m_dbConn为成员变量 假设有一个表有如下SQL语句产生:CREATE TABLE table1(loc_id not null) */ void CYourClass::ConnectToDB() {//连接数据库 BOOL fOK = m_dbConn.Open("test"); TRACE("connect fOK=%d\n",m_dbConn); } void CYourClass::Select() |
此外CRecordset::GetFieldValue有很多种原型,你可以通过指定列位置或是字段名来获取数据:
void GetFieldValue( LPCTSTR lpszName, CDBVariant& varValue, short nFieldType = DEFAULT_FIELD_TYPE ); void GetFieldValue( short nIndex, CDBVariant& varValue, short nFieldType = DEFAULT_FIELD_TYPE ); void GetFieldValue( LPCTSTR lpszName, CString& strValue ); void GetFieldValue( short nIndex, CString& strValue ); |
如果使用CDBVariant类型变量来获取结果,你可以得到任何类型的结果。在CDBVariant::m_dwType成员变量中记录了该变量所包含的数据类型,根据该变量的值你可以确定数据类型并引用CDBVariant对象中的相应成员变量。