MFC连接有密码的Access数据库并查询相关字段

VS2015  win10 64位系统

1.引用msado15.dll库文件(将此文件放在生成目录下)

链接:https://pan.baidu.com/s/1xg1jCcxxU9HRx84F7qYR4w 提取码:3wps 

2.在头文件中添加

#import "../Debug/msado15.dll"  no_namespace rename("EOF", "adoEOF")

3.定义变量

_ConnectionPtr    m_pConnection;//连接access数据库的链接对象  
_RecordsetPtr      m_pRecordset;//结果集对象  

4.在初始化函数中
连接数据库

 

try {

		CoInitialize(NULL);
		m_pConnection = _ConnectionPtr(__uuidof(Connection));
		m_pConnection->Open("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = ../Debug/DRDB_cn.mdb;Persist Security Info=False;Jet OLEDB:Database Password= Pxray", "", "", adConnectUnspecified);
	}
	catch (_com_error e) {

		AfxMessageBox(_T("数据库连接失败!"));
		return;
	}

 5.查询数据库中某个表格内容

try
	{
		_variant_t RecordsAffected;
		m_pRecordset.CreateInstance(__uuidof(Recordset));
		CString search_sql;
		search_sql = "SELECT * FROM PartPosition";
		m_pRecordset = m_pConnection->Execute(search_sql.AllocSysString(), NULL, adCmdText);
		while (!m_pRecordset->adoEOF)
		{
			CString id;

			id = m_pRecordset->GetCollect("ID");
			
			m_pRecordset->MoveNext();
		}
		m_pRecordset->Close();
	}
	catch (_com_error e)
	{
		AfxMessageBox(_T("表格读取失败!"));
		return 1;
	}


     
           

   

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值