📄 accessmdbdlg.cpp
字号:
// AccessMdbDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AccessMdb.h"
#include "AccessMdbDlg.h"
#include <stdlib.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAccessMdbDlg dialog
CAccessMdbDlg::CAccessMdbDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAccessMdbDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAccessMdbDlg)
m_Name = _T("");
m_Age = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CAccessMdbDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAccessMdbDlg)
DDX_Control(pDX, IDC_LIST1, m_AccessList);
DDX_Text(pDX, IDC_EDIT_NAME, m_Name);
DDX_Text(pDX, IDC_EDIT_AGE, m_Age);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAccessMdbDlg, CDialog)
//{{AFX_MSG_MAP(CAccessMdbDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_READ, OnRead)
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_BN_CLICKED(IDC_INSERT, OnInsert)
ON_BN_CLICKED(IDC_MODIFY, OnModify)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAccessMdbDlg message handlers
BOOL CAccessMdbDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_pConnection == NULL;
m_pRecordset = NULL;
// ::CoInitialize(NULL); //初始化OLE/COM库环境
return TRUE; // return TRUE unless you set the focus to a control
}
void CAccessMdbDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CAccessMdbDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CAccessMdbDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CAccessMdbDlg::OnRead()
{
// TODO: Add your control notification handler code here
// 初始化COM,创建ADO连接等操作
if(m_pConnection == NULL){
AfxOleInit();
m_pConnection.CreateInstance(__uuidof(Connection));
try
{
// 打开本地Access库Demo.mdb
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=../Demo.mdb",""
,"",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库Demo.mdb是否在当前路径下!");
return;
}
}
// 打开数据表
/* if(m_pRecordset == NULL){
m_pRecordset.CreateInstance(__uuidof(Recordset));
try{
m_pRecordset->Open("SELECT * FROM DemoTable", // 查询DemoTable表中所有字段
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e){
AfxMessageBox(e->ErrorMessage());
}
}
*/
_CommandPtr m_pCommand;
m_pCommand.CreateInstance(__uuidof(Command));
m_pCommand->ActiveConnection = m_pConnection; // 将库连接赋于它
m_pCommand->CommandText = "SELECT * FROM DemoTable"; // SQL语句
m_pRecordset = m_pCommand->Execute(NULL, NULL,adCmdText); // 执行SQL语句,返回记录集
DisplayData();
}
void CAccessMdbDlg::DisplayData()
{
// 读取表内数据
_variant_t var;
CString strName,strAge;
try{
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();
else{
AfxMessageBox("表内数据为空");
return;
}
m_AccessList.ResetContent();
// 读入库中各字段并加入列表框中
while(!m_pRecordset->adoEOF)
{
var = m_pRecordset->GetCollect("Name");
if(var.vt != VT_NULL)
strName = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Age");
if(var.vt != VT_NULL)
strAge = (LPCSTR)_bstr_t(var);
m_AccessList.AddString( strName + " --> "+strAge );
m_pRecordset->MoveNext();
}
// 默认列表指向第一项,同时移动记录指针并显示
m_AccessList.SetCurSel(0);
}
catch(_com_error *e){
AfxMessageBox(e->ErrorMessage());
}
}
void CAccessMdbDlg::OnExit()
{
// TODO: Add your control notification handler code here
if(m_pConnection != NULL && m_pConnection->State)
m_pConnection->Close();
m_pConnection= NULL;
OnCancel();
}
void CAccessMdbDlg::OnInsert()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(m_pConnection == NULL || m_pConnection == NULL){
AfxMessageBox("No connection");
return;
}
try
{
// 写入各字段值
m_pRecordset->AddNew();
m_pRecordset->PutCollect("Name", _variant_t(m_Name));
// m_pRecordset->PutCollect("Age", m_Age);
m_pRecordset->PutCollect("Age", atol(m_Age));
m_pRecordset->Update();
AfxMessageBox("插入成功!");
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
DisplayData();
}
void CAccessMdbDlg::OnModify()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
int n_Item = m_AccessList.GetCurSel();
try
{
m_pRecordset->MoveFirst();
for(int i = 0; i < n_Item; i++){
m_pRecordset->MoveNext();
}
m_pRecordset->PutCollect("Name", _variant_t(m_Name));
m_pRecordset->PutCollect("Age", atol(m_Age));
m_pRecordset->Update();
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
DisplayData();
}
void CAccessMdbDlg::OnDelete()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
int n_Item = m_AccessList.GetCurSel();
try
{
m_pRecordset->MoveFirst();
for(int i = 0; i < n_Item; i++){
m_pRecordset->MoveNext(); // 从0开始
}
m_pRecordset->Delete(adAffectCurrent); // 参数adAffectCurrent为删除当前记录
m_pRecordset->Update();
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
DisplayData();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -