当前位置:网站首页>Vs 2019 MFC connects and accesses access database class library encapsulation through ace engine
Vs 2019 MFC connects and accesses access database class library encapsulation through ace engine
2022-06-12 07:30:00 【lzc881012】
There are many codes on the Internet for reference , Most will be with #import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF") Xiang Guan's content . These class content pairs VS2019 Previous versions may be useful , But for VS2019 It doesn't help much .VS2019 Give up the old “stdafx.h” Precompiled header file , And switch to “PCH.h” The header file . Therefore, it took some time to solve the problem msado15.dll Dynamic link library import problem .
Encapsulating class libraries AdoControlAccess.h head file
#include<odbcinst.h>
#include<afxdb.h>
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF")
class AdoControlAccess
{
public:
_ConnectionPtr m_Connection;
_RecordsetPtr m_Recorderset;
_CommandPtr m_CommandSql;
BOOL m_OpenDataBaseOpened;
public:
AdoControlAccess();
virtual~AdoControlAccess();
public:
_RecordsetPtr& GetRecordset(void);
_RecordsetPtr& OpenRecordSet(CString CmdSql);
void CloseConnectAndRecordset();
void AdoConnectionInit(_bstr_t strConnection, _bstr_t UserID, _bstr_t PassWord, long OpenAccessMode);
};
Encapsulating class libraries AdoControlAccess.CPP Implementation file
#include "pch.h"
#include "AdoControlAccess.h"
AdoControlAccess::AdoControlAccess()
{
m_OpenDataBaseOpened = FALSE;
}
AdoControlAccess::~AdoControlAccess()
{
}
_RecordsetPtr& AdoControlAccess::GetRecordset(void)
{
return m_Recorderset;
}
_RecordsetPtr& AdoControlAccess::OpenRecordSet(CString CmdSql)
{
ASSERT(!CmdSql.IsEmpty());
try
{
m_Recorderset.CreateInstance("ADODB.Recordset");
if (m_Recorderset==NULL)
{
MessageBox(NULL, _T("RecordSet Object creation failed ! Please confirm whether it is initialized COM Environmental Science ."), _T(" message :"), MB_OK | MB_ICONERROR);
printf("\n");
printf("RecordSetRecordSet Object creation failed ! Please confirm whether it is initialized COM Environmental Science !");
}
else
{
m_Recorderset->CursorLocation = adUseClient;
m_Recorderset->Open(_variant_t(CmdSql), m_Connection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
printf("\n");
printf("RecordSet Object successfully created !");
}
}
catch (_com_error Error)
{
OutputDebugString(Error.Description());
MessageBox(NULL, Error.Description(), _T(" message :"), MB_OK | MB_ICONERROR);
}
return m_Recorderset;
}
void AdoControlAccess::CloseConnectAndRecordset()
{
if (m_Connection->State)
{
if (m_Recorderset != NULL)
{
m_Recorderset->Close();
m_Recorderset = NULL;
}
m_Connection->Close();
m_Connection = NULL;
}
}
void AdoControlAccess::AdoConnectionInit(_bstr_t strConnection,_bstr_t UserID,_bstr_t PassWord,long OpenAccessMode)
{
/*OpenAccessMode*/
/*adModeUnknown:
adModeRead : read-only
adModeWrite : Just write
adModeReadWrite : Can read and write
adModeShareDenyRead : Block others Connection Object to open the connection with read permission
adModeShareDenyWrite : Block others Connection Object to open the connection with write permission
adModeShareExclusive : Block others Connection Object to open the connection
adModeShareDenyNone : Allow other programs or objects to connect with any permission */
long nI = ::CoInitialize(NULL);
try
{
m_Connection.CreateInstance("ADODB.Connection");
m_Connection->ConnectionTimeout=5;
m_Connection->Open(strConnection, UserID, PassWord, OpenAccessMode);
m_OpenDataBaseOpened = TRUE;
printf("\n");
printf(" Successfully connected to Access database !");
}
catch (_com_error Error)
{
OutputDebugString(Error.Description());
MessageBox(NULL,Error.Description(),_T(" message :"),MB_OK|MB_ICONERROR);
m_OpenDataBaseOpened = FALSE;
}
}Application instructions : Application header file
#include"MacroDefinition.h"
#include"CMyStatic.h"
#include"CEditJF.h"
#include"CMyButton.h"
#include"ToolBarEx.h"
#include"AdoControlAccess.h"
#include"UserDataModifyDlg.h"
#pragma once
#define WM_USERDLG_SHUNTDOWN WM_USER+102
class CUserLogonDlg : public CDialogEx
{
DECLARE_DYNAMIC(CUserLogonDlg)
public:
CUserLogonDlg(CWnd* pParent = nullptr);
virtual ~CUserLogonDlg();
CWnd* m_Parent;
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_DIALOGUSERLOGON };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX);
void DrawTitleBar(CDC* pDC);
DECLARE_MESSAGE_MAP()
public:
CRect m_rtButtExit;
BOOL m_bCloseShow;
afx_msg void OnClose();
virtual BOOL OnInitDialog();
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnNcHitTest(CPoint point);
afx_msg void OnNcLButtonDown(UINT nHitTest, CPoint point);
afx_msg void OnNcMouseMove(UINT nHitTest, CPoint point);
afx_msg void OpenAccessDataBase();
afx_msg void CloseAccessDataBaseAndRecorderset();
afx_msg void OnReadAdministratorInfo();
afx_msg void ModifyDataBaseRecordset(CString preModifyUserName,CString preModifyUserPwd,CString preModifyUserType);
afx_msg void AddNewRecordset(CString strUserName, CString strPwd, CString strUserType);
afx_msg void ErgodicAllUserInfo(CString InputUser, CString InputUserPwd, CString InputUserType);
CEditJF m_UserLogonName;
CEditJF m_UserLogonPassWord;
CMyStatic m_UserLogonDlgTitle;
CMyButton m_LogonMakeSure;
CMyButton m_UserLogOut;
CMyButton m_AddUser;
CMyButton m_UserModify;
CComboBoxXI m_LogonUserType;
CImageList m_LogonUserTypeImgList;
AdoControlAccess m_CUserDataBase;
_RecordsetPtr UserTableRecordset;
UserDataModifyDlg *m_CuserModifyDlg;
struct AdministratorInfo
{
CString AdName;
CString AdPwd;
CString AdType;
}AdminUser,PreLogonUser,TemUser,CurrentUser,UserWillBeModify, UpdateUserModifyByNewUser;
afx_msg void OnBnClickedButtonlogon();
afx_msg void OnBnClickedButtonlogonout();
afx_msg void OnBnClickedButtonadduser();
afx_msg void OnBnClickedButtonmodifyuser();
protected:
afx_msg LRESULT OnUsermodifydlgClose(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnUsermodifyUpdatedatabase(WPARAM wParam, LPARAM lParam);
};
Connection instructions : Access The default connection user name of the database is Admin
password = The access password set when the database is created
m_CUserDataBase.AdoConnectionInit("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=./User/UserDataBase.accdb;Persist Security Info=False;User ID=Admin;Jet OLEDB:Database Password= password ", "", "", adModeUnknown);VS2019 All cancelled SQL Database and Access Connection wizard for database connection , The database connection tool is reserved . The database connection tool is in the software interface " Tools "->“ Connect to database ” You can find . As shown in the figure below . Including the connection string can also be viewed through the software .

Data query (CUserLogonDlg For application classes )
void CUserLogonDlg::OnReadAdministratorInfo()
{
OpenAccessDataBase();
if (m_CUserDataBase.m_OpenDataBaseOpened==TRUE)
{
UserTableRecordset = m_CUserDataBase.GetRecordset();
UserTableRecordset = m_CUserDataBase.OpenRecordSet(_T("SELECT UserTable.[UserName], UserTable.[UserPassWord], UserTable.[UserType] FROM UserTable where UserType=' Administrators '"));
if (UserTableRecordset->RecordCount > 0)
{
UserTableRecordset->MoveFirst();
AdminUser.AdName = UserTableRecordset->GetCollect("UserName");
AdminUser.AdPwd = UserTableRecordset->GetCollect("UserPassWord");
AdminUser.AdType = UserTableRecordset->GetCollect("UserType");
printf("\n");
printf(" Successfully obtained the administrator account !");
CloseAccessDataBaseAndRecorderset();
}
else
{
printf("\n");
printf(" No information related to the administrator account was found !");
}
}
}Data update
void CUserLogonDlg::ModifyDataBaseRecordset(CString preModifyUserName, CString preModifyUserPwd, CString preModifyUserType)
{
CString SqlCmd = _T("SELECT UserTable.[UserName], UserTable.[UserPassWord], UserTable.[UserType] FROM UserTable Where UserName=");
SqlCmd = SqlCmd + _T("'") + preModifyUserName+_T("'") ;
SqlCmd = SqlCmd + _T(" And ") + _T("UserPassWord=") + _T("'") + preModifyUserPwd + _T("'");
SqlCmd = SqlCmd + _T(" And ") + _T("UserType=") + _T("'") + preModifyUserType + _T("'");
OpenAccessDataBase();
if (m_CUserDataBase.m_OpenDataBaseOpened == TRUE)
{
UserTableRecordset = m_CUserDataBase.GetRecordset();
UserTableRecordset = m_CUserDataBase.OpenRecordSet(SqlCmd);
if (UserTableRecordset->RecordCount > 0)
{
UserTableRecordset->PutCollect("UserName", _variant_t(UpdateUserModifyByNewUser.AdName));
UserTableRecordset->PutCollect("UserPassWord", _variant_t(UpdateUserModifyByNewUser.AdPwd));
UserTableRecordset->PutCollect("UserType", _variant_t(UpdateUserModifyByNewUser.AdType));
UserTableRecordset->Update();
MessageBox(_T(" The currently modified user data has been modified successfully !."), _T(" message :"), MB_OK | MB_ICONINFORMATION);
printf("\n");
printf(" The currently modified user data has been modified successfully .");
}
else
{
MessageBox(_T(" The database finds that the current user to be modified does not exist ! Please confirm and try again ."), _T(" message :"), MB_OK | MB_ICONINFORMATION);
printf("\n");
printf(" The database finds that the current user to be modified does not exist ! Please confirm and try again .");
}
CloseAccessDataBaseAndRecorderset();
}
}Data addition
void CUserLogonDlg::AddNewRecordset(CString strUserName,CString strPwd,CString strUserType)
{
if (strUserName == _T("") || strPwd == _T("") || strUserType == _T(""))
{
MessageBox(_T(" User name or password or user type input cannot be empty ! Please confirm and try again ."), _T(" message :"), MB_OK | MB_ICONINFORMATION);
printf("\n");
printf(" User name or password or user type input cannot be empty ! Please confirm and try again .");
return;
}
else
{
OpenAccessDataBase();
if (m_CUserDataBase.m_OpenDataBaseOpened == TRUE)
{
UserTableRecordset = m_CUserDataBase.GetRecordset();
UserTableRecordset = m_CUserDataBase.OpenRecordSet(_T("SELECT UserTable.[UserName], UserTable.[UserPassWord], UserTable.[UserType] FROM UserTable"));
if (UserTableRecordset->RecordCount > 0)
{
UserTableRecordset->MoveFirst();
for (int nCount = 0; nCount < UserTableRecordset->RecordCount; nCount++)
{
TemUser.AdName = UserTableRecordset->GetCollect("UserName");
if (TemUser.AdName== strUserName)
{
MessageBox(_T(" The user to be added already exists ! Please confirm and try again ."), _T(" message :"), MB_OK | MB_ICONINFORMATION);
printf("\n");
printf(" The user to be added already exists ! Please confirm and try again .");
break;
}
else if (strUserType == _T(" Administrators "))
{
MessageBox(_T(" Unable to add administrator account , Please select the correct user type ."), _T(" message :"), MB_OK | MB_ICONINFORMATION);
printf("\n");
printf(" Unable to add administrator account , Please select the correct user type .");
break;
}
else if (TemUser.AdName != strUserName&& strUserType != _T(" Administrators "))
{
UserTableRecordset->MoveNext();
}
if (UserTableRecordset->adoEOF)
{
if (TemUser.AdName != strUserName && strUserType != _T(" Administrators "))
{
UserTableRecordset->MoveLast();
UserTableRecordset->AddNew();
UserTableRecordset->PutCollect("UserName", _variant_t(strUserName));
UserTableRecordset->PutCollect("UserPassWord", _variant_t(strPwd));
UserTableRecordset->PutCollect("UserType", _variant_t(strUserType));
UserTableRecordset->Update();
UserTableRecordset->MoveFirst();
MessageBox(_T(" The new user has been successfully added to the user management database !"), _T(" message :"), MB_OK | MB_ICONINFORMATION);
printf("\n");
printf(" User added successfully !");
break;
}
}
}
CloseAccessDataBaseAndRecorderset();
}
}
}
TemUser.AdName = _T("");
TemUser.AdPwd = _T("");
TemUser.AdType = _T("");
}
边栏推荐
- Golang quickly generates model and queryset of database tables
- MySQL索引(一篇文章轻松搞定)
- BI技巧丨当月期初
- Machine learning from entry to re entry: re understanding of SVM
- Continuous local training for better initialization of Federated models
- VS 2019 MFC 通过ACE引擎连接并访问Access数据库类库封装
- The first demand in my life - batch uploading of Excel data to the database
- ‘CMRESHandler‘ object has no attribute ‘_ timer‘,socket. gaierror: [Errno 8] nodename nor servname pro
- Non IID data and continuous learning processes in federated learning: a long road ahead
- Nine project management issues that PM should understand
猜你喜欢

Summary of machine learning + pattern recognition learning (IV) -- decision tree

Interview intelligence questions

Unity用Shader实现UGU i图片边缘选中高亮

SQL -- course experiment examination

knife4j 初次使用

Learning to continuously learn paper notes + code interpretation

2022 simulated test platform operation of hoisting machinery command test questions

Keil installation of C language development tool for 51 single chip microcomputer

Exploring shared representations for personalized federated learning paper notes + code interpretation

Pyhon的第六天
随机推荐
Nine project management issues that PM should understand
Decoupling in D
ROS dynamic parameter configuration: use of dynparam command line tool (example + code)
Class as a non type template parameter of the template
Summary of software testing tools in 2021 - unit testing tools
Adaptive personalized federated learning paper interpretation + code analysis
The function of C language string Terminator
【高考那些事】准大学生看过来,选择方向和未来,自己把握
modelarts二
Win10 list documents
5 lines of code identify various verification codes
Study on display principle of seven segment digital tube
Detailed explanation of addressing mode in 8086
Detailed explanation of coordinate tracking of TF2 operation in ROS (example + code)
AI fanaticism | come to this conference and work together on the new tools of AI!
Gd32f4 (5): gd32f450 clock is configured as 200m process analysis
Pyhon的第四天
Right click the general solution of file rotation jam, refresh, white screen, flash back and desktop crash
2022电工(初级)考试题库及模拟考试
Keil installation of C language development tool for 51 single chip microcomputer