通过数据库 指针(m_pUserSet)对数据库进行操作
1. 打开数据库
if (m_pUserSet->IsOpen() == FALSE)
{
m_pUserSet->Open();
}
2. 添加记录
m_pUserSet->AddNew();
m_pUserSet->m_account = userAccount;
m_pUserSet->m_classification = userType;
m_pUserSet->m_password = userPwd;
m_pUserSet->m_name=userName;
m_pUserSet->Update();
3. 删除记录
CString sql;
sql.Format(_T("account = '%s' "), account);
//设置过滤条件
m_pUserSet->m_strFilter = sql;
//查询
m_pUserSet->Requery();
if (m_pUserSet->GetRecordCount() == 0)
{
AfxMessageBox(_T("删除用户不存在"));
return 3;
}
else
{
m_pUserSet->Delete(); //m_pUserSet指针已经指到查询的记录上,直接删除即可
return 0;
}
4. 修改记录
//设置过滤条件
CString sql;
sql.Format(_T("account = '%s' and password = '%s' and classification = '%s'"), m_userAccount, m_userPwd, m_userType);
m_pUserSet->m_strFilter = sql;
//查询
m_pUserSet->Requery();
if (m_pUserSet->GetRecordCount() == 0)
{
AfxMessageBox(_T("没有此用户"));
return;
}
//修改密码
m_pUserSet->Edit();
m_pUserSet->m_password = uiNewPwd;
//更新
if (m_pUserSet->CanUpdate())
{
m_pUserSet->Update(); //服务器端
m_userPwd = uiNewPwd;//本地变量
AfxMessageBox(_T("更新成功"));
}
else
{
AfxMessageBox(_T("更新失败"));
}
5. 数据库指针移动操作
(1) MoveLast(); MoveFitst(); MoveNext(); MovePrev();
(2) IsBOF();是否是第一个 IsEOF();是否是最后一个
本文详细介绍了如何使用数据库指针(m_pUserSet)进行数据库的基本操作,包括打开数据库、添加记录、删除记录、修改记录以及数据库指针的移动操作。通过具体的代码示例,读者可以学习如何实现这些功能。
797

被折叠的 条评论
为什么被折叠?



