3.23 增加记录
增加记录使用AddNew()函数,要求数据库必须是以允许增加的方式打开:
m_pSet->AddNew(); //在表的末尾增加新记录 m_pSet->SetFieldNull(&(m_pSet->m_type),FALSE); m_pSet->m_type="电动机"; ... //输入新的字段值 m_pSet-> Update(); //将新记录存入数据库 m_pSet->Requery(); //重建记录集 |
3.24 删除记录
直接使用Delete()函数,并且在调用Delete() 函数之后不需调用Update()函数:
m_pSet->Delete(); if(!m_pSet->IsEOF()) m_pSet->MoveNext(); else m_pSet->MoveLast(); |
3.25 修改记录
修改记录使用Edit()函数:
m_pSet->Edit(); //修改当前记录 m_pSet->m_type="发电机"; //修改当前记录字段值 ... m_pSet->Update(); //将修改结果存入数据库 m_pSet->Requery(); |
3.26 统计记录
统计记录用来统计记录集的总数。可以先声明一个CRecordset对象m_pSet。再绑定一个变量m_lCount,用来统计记录总数。执行如下语句:
m_pSet->Open(“Select Count(*) from 表名 where 限定条件”); RecordCount=m_pSet->m_lCount; m_pSet->Close(); |
RecordCount即为要统计的记录数。
或如下:
CRecordset m_Set(&db); //db 为CDatabase对象 CString strValue; m_Set.Open(Select count(*) from 表名 where 限定条件”); m_pSet.GetFieldValue((int)0,strValue); long count=atol(strValue); m_set.Close(); |
count为记录总数。 |