📄 querydel.cpp
字号:
// QueryDel.cpp : 实现文件
//
#include "stdafx.h"
#include "AS.h"
#include "QueryDel.h"
// CQueryDel 对话框
IMPLEMENT_DYNAMIC(CQueryDel, CDialog)
CQueryDel::CQueryDel(CWnd* pParent /*=NULL*/)
: CDialog(CQueryDel::IDD, pParent)
, m_sName(_T(""))
, m_sKey(_T(""))
{
}
CQueryDel::~CQueryDel()
{
}
void CQueryDel::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_sName);
DDX_Text(pDX, IDC_EDIT2, m_sKey);
}
BEGIN_MESSAGE_MAP(CQueryDel, CDialog)
ON_EN_CHANGE(IDC_EDIT1, &CQueryDel::OnEnChangeEdit1)
ON_EN_CHANGE(IDC_EDIT2, &CQueryDel::OnEnChangeEdit2)
ON_BN_CLICKED(IDC_BUTTON1, &CQueryDel::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CQueryDel::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON4, &CQueryDel::OnBnClickedButton4)
END_MESSAGE_MAP()
// CQueryDel 消息处理程序
void CQueryDel::OnEnChangeEdit1()
{
// TODO: 如果该控件是 RICHEDIT 控件,则它将不会
// 发送该通知,除非重写 CDialog::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
}
void CQueryDel::OnEnChangeEdit2()
{
// TODO: 如果该控件是 RICHEDIT 控件,则它将不会
// 发送该通知,除非重写 CDialog::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
}
void CQueryDel::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
//用于连接数据库
HRESULT hr = S_OK;
_ConnectionPtr pConn = NULL;
_RecordsetPtr pRst = NULL;
TCHAR szExePath[400] = {'\0'};
PTCHAR pLast = NULL;
//连接字符串
CString strOpen;
strOpen = "DRIVER={Microsoft Access Driver (*.mdb)};UID=;PWD=;DBQ=Person.mdb";
//查询语句
CString strQuery;
CString sTemp;
//初始化连接
CoInitialize(NULL);
hr = pConn.CreateInstance(__uuidof(Connection));
if(hr != S_OK)
{
AfxMessageBox(_T("打开Connection失败"));
return;
}
hr = S_OK;
//设置exe文件所在路径为当前路径
GetModuleFileName(NULL,szExePath,400);
if(pLast = _tcsstr(szExePath,_T("\\AS.exe")))
{
*pLast = '\0';
}
SetCurrentDirectory(szExePath);
//连接数据库
try
{
pConn->Open((_bstr_t)strOpen,"","",adConnectUnspecified);
}
catch(_com_error e)
{
AfxMessageBox(_T("Opening Connection...")+e.Description());
return;
}
strQuery.Format(_T("insert into User(Name,Key) values(\'%s\',\'%s\')"),m_sName,m_sKey);
pRst = pConn->Execute((_bstr_t)strQuery,NULL,adCmdText);
AfxMessageBox(_T("添加成功"));
//关闭数据库
try
{
pConn->Close();
}
catch(_com_error e)
{
AfxMessageBox(_T("Closing Connection...")+e.Description());
return;
}
pConn.Release();
CoUninitialize();
OnCancel();
return;
}
void CQueryDel::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
//用于连接数据库
HRESULT hr = S_OK;
_ConnectionPtr pConn = NULL;
_RecordsetPtr pRst = NULL;
TCHAR szExePath[400] = {'\0'};
PTCHAR pLast = NULL;
//连接字符串
CString strOpen;
strOpen = "DRIVER={Microsoft Access Driver (*.mdb)};UID=;PWD=;DBQ=Person.mdb";
//查询语句
CString strQuery;
CString sTemp;
//初始化连接
CoInitialize(NULL);
hr = pConn.CreateInstance(__uuidof(Connection));
if(hr != S_OK)
{
AfxMessageBox(_T("打开Connection失败"));
return;
}
hr = S_OK;
//设置exe文件所在路径为当前路径
GetModuleFileName(NULL,szExePath,400);
if(pLast = _tcsstr(szExePath,_T("\\AS.exe")))
{
*pLast = '\0';
}
SetCurrentDirectory(szExePath);
//连接数据库
try
{
pConn->Open((_bstr_t)strOpen,"","",adConnectUnspecified);
}
catch(_com_error e)
{
AfxMessageBox(_T("Opening Connection...")+e.Description());
return;
}
strQuery.Format(_T("delete from User where Name=\'%s\'"),m_sName);
pRst = pConn->Execute((_bstr_t)strQuery,NULL,adCmdText);
AfxMessageBox(_T("删除成功"));
//关闭数据库
try
{
pConn->Close();
}
catch(_com_error e)
{
AfxMessageBox(_T("Closing Connection...")+e.Description());
return;
}
pConn.Release();
CoUninitialize();
OnCancel();
return;
}
void CQueryDel::OnBnClickedButton4()
{
// TODO: 在此添加控件通知处理程序代码
OnCancel();
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -