MFC利用ADO建立access数据源 ---包括访问带access密码与不带access密码两种方式)

简介: void CDlg_login::OnButton1() { CString c_user,c_password;m_user1.GetWindowText(c_user);m_password1.GetWindowText(c_password); if (c_user.

void CDlg_login::OnButton1()
{

CString c_user,c_password;
m_user1.GetWindowText(c_user);
m_password1.GetWindowText(c_password);


if (c_user.IsEmpty()||c_user.IsEmpty())
{
 MessageBox("用户名密码不能为空!","提示",64);
}

m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
CString strJet ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\\DataBase.mdb";
m_pConnection->Open(strJet.AllocSysString(),"","",adModeUnknown);

CString sql;
 sql.Format("select *from employees where name='%s' and password='%s'",c_user,c_password);
m_pRecordset->Open(sql.AllocSysString(), m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,-1);

if (c_user==(user = m_pRecordset->GetCollect("name").bstrVal) &&c_password== (password = m_pRecordset->GetCollect("password").bstrVal))
{
 CDialog::OnOK();
 CLOGINDlg dlg;
 dlg.DoModal();

}

 

 

 

实例二,连接数据据至登录对话框(其中部分代码注释加//,仅用来调试期间用,可供参考),代码实现如下:

try
 {
  this->UpdateData(true);

  //  if (this->m_username.IsEmpty()||this->m_password.IsEmpty())
  //  {
  //   MessageBox("用户名,密码不能为空!","登录提示",MB_ICONQUESTION);
  //  }

  ::CoInitialize(NULL);

  this->m_pConnection.CreateInstance(__uuidof(Connection));

  this->m_pRecordset.CreateInstance(__uuidof(Recordset));


  // this->m_pConnection->Open("DSN=staff_dns","","",0);//上面四行为打开数据源连接,此方法使用本地DSN数据源


  // CString strJet ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=staff.mdb";  //访问不带密码的access数据库


  CString strJet ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=staff.mdb;Jet OLEDB:Database Password=sdoa0806";   //访问带密码的access数据库方法

  this->m_pConnection->Open(strJet.AllocSysString(),"","",adModeUnknown);

  CString str;

  str.Format("select * from tb_staff where username='%s' and password='%s'",this->m_username,this->m_password);

  BSTR bstrSQL=str.AllocSysString();

  this->m_pRecordset->Open(bstrSQL,(IDispatch*)this->m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);

  if(!this->m_pRecordset->adoEOF)

  {


   //   CDebugDlg dlg1;
   //   UpdateData(false);
   //   GetDlgItemText(IDC_EDIT1,dlg1.m_user);
   //   MessageBox(dlg1.m_user);
   //   dlg1.m_StatusBar.SetPaneText(3,m_username);

   CDialog::OnOK();
   //MessageBox("调用成功!");
   OutputDebugString("登录成功!");

   UpdateData(TRUE);

   GetDlgItemText(IDC_EDIT_USERNAME,m_username);

   //MessageBox(m_username,"1");

   dlg_confirm.m_user=m_username;
   // MessageBox(dlg_confirm.m_user,"2");

   UpdateData(FALSE);

   dlg_confirm.DoModal();


  }

  else

   MessageBox("用户名,密码输入错误,登录失败!","登录提示",MB_ICONQUESTION);

 

 }
 catch (...)     //增加try...catch异常抛出
 {
  
   AfxMessageBox("数据库连接失败,确认数据库名称与路径是否正确!");  
   return ;
 } 

 

 this->m_pRecordset->Close();

 this->m_pConnection->Close();

 ::CoUninitialize();

}

技术改变世界! --狂诗绝剑
目录
相关文章
|
存储 资源调度 算法
Opencv(C++)系列学习---SIFT、SURF、ORB算子特征检测
Opencv(C++)系列学习---SIFT、SURF、ORB算子特征检测
699 0
|
算法
【MATLAB】语音信号识别与处理:一维信号NLM非局部均值滤波算法去噪及谱相减算法呈现频谱
【MATLAB】语音信号识别与处理:一维信号NLM非局部均值滤波算法去噪及谱相减算法呈现频谱
252 1
|
缓存 算法 JavaScript
分享一个开源一个新的雪花算法(雪花漂移)
  IdGenerator介绍   用一种全新的雪花漂移算法,让ID更短、生成速度更快。   核心在于缩短ID长度的同时,还能保持每毫秒并发处理量(50W/0.1s),且能保持伸缩能力。   需求来源   1.作为架构设计的你,想要解决数据库主键唯一的问题。   2.你希望这个主键是用最少的存储空间,索引速度更快。   3.你还会考虑在分库分表(合库合表)的时候,主键值能直接使用,并能反映业务时序。
880 0
|
4月前
|
人工智能 搜索推荐 Docker
手把手教你使用 Ollama 和 LobeChat 快速本地部署 DeepSeek R1 模型,创建个性化 AI 助手
DeepSeek R1 + LobeChat + Ollama:快速本地部署模型,创建个性化 AI 助手
4642 119
手把手教你使用 Ollama 和 LobeChat 快速本地部署 DeepSeek R1 模型,创建个性化 AI 助手
|
7月前
|
缓存 算法 前端开发
如何降低 SPA 单页面应用的内存占用
单页面应用(SPA)由于其良好的用户体验而被广泛使用,但随着应用复杂度的增加,内存占用问题日益突出。本文将介绍几种有效降低SPA内存占用的方法,包括代码分割、懒加载、状态管理优化等技术,帮助开发者提升应用性能。
|
9月前
|
算法 数据安全/隐私保护 C++
超级好用的C++实用库之MD5信息摘要算法
超级好用的C++实用库之MD5信息摘要算法
228 0
|
JavaScript 前端开发 API
JavaScript基础-事件监听与处理
【6月更文挑战第11天】本文介绍了JavaScript事件驱动编程的核心,包括事件流(捕获、目标、冒泡阶段)和监听方法(DOM Level 0、addEventListener/removeEventListener)。讨论了常见问题和易错点,如内存泄漏、事件委托和阻止默认行为的混淆,并提供了解决策略。通过代码示例展示了事件绑定、事件委托和阻止默认行为的用法,强调理解事件处理机制对于编写高效交互式Web应用的重要性。
729 6
|
存储 传感器 安全
【C++ std::variant】深入探索 C++ std::variant:构造方法与实践应用
【C++ std::variant】深入探索 C++ std::variant:构造方法与实践应用
553 5
|
11月前
|
安全 关系型数据库 Linux