⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adomfcdlg.cpp

📁 例且使用VC6.0+++ACESS连接一个简单的数据库,很简单,大[有家丁一五下
💻 CPP
字号:
// ADOMFCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ADOMFC.h"
#include "ADOMFCDlg.h"
#include <Atlbase.h>	//CComVariant

#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()

/////////////////////////////////////////////////////////////////////////////
// CADOMFCDlg dialog

CADOMFCDlg::CADOMFCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CADOMFCDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CADOMFCDlg)
	m_address = _T("");
	m_date = _T("");
	m_name = _T("");
	m_number = _T("");
	m_score = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CADOMFCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CADOMFCDlg)
	DDX_Text(pDX, IDE_ADDRESS, m_address);
	DDX_Text(pDX, IDE_DATE, m_date);
	DDX_Text(pDX, IDE_NAME, m_name);
	DDX_Text(pDX, IDE_NUMBER, m_number);
	DDX_Text(pDX, IDE_SCORE, m_score);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CADOMFCDlg, CDialog)
	//{{AFX_MSG_MAP(CADOMFCDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_NEW, OnButtAddNew)
	ON_BN_CLICKED(IDC_SAVE, OnButtSave)
	ON_BN_CLICKED(IDC_NEXT, OnButtNext)
	ON_BN_CLICKED(IDC_PREV, OnButtPrev)
	ON_BN_CLICKED(IDC_LAST, OnButtLast)
	ON_BN_CLICKED(IDC_FIRST, OnButtFirst)
	ON_BN_CLICKED(IDC_DELETE, OnButtDelete)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CADOMFCDlg message handlers

//定义一个鼠标HOOK句柄
HHOOK g_hMouse = NULL;

//安装鼠标钩子.
//LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
//{
//	return 1;
//}

BOOL CADOMFCDlg::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_pCon.CreateInstance(__uuidof(Connection));
	//打开字串.
	HRESULT hr = m_pCon->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\VC_mfc\\ADOMFC\\db1.mdb;Persist Security Info=False",
		"", "", -1);
	//创建保存记录集
	m_pRs.CreateInstance(__uuidof(Recordset));
	//打开保存记录
	m_pRs->Open("select * from Test", m_pCon.GetInterfacePtr(), adOpenDynamic,
		adLockOptimistic, adCmdText);
	//移动到第一个数据项
	m_pRs->MoveFirst();
	//显示出来
	DispFileds();

	//安装一个钩子过程,GetCurrentThreadId()获取当前线程id.
//	g_hMouse = SetWindowsHookEx(WH_MOUSE, MouseProc, NULL, GetCurrentThreadId());
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CADOMFCDlg::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 CADOMFCDlg::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 CADOMFCDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//显示信息
void CADOMFCDlg::DispFileds()
{
	//获取数据库里面的字段名.
	m_vFieldValue = m_pRs->GetCollect("NO");
	//VARIANT类型生成VC类型
	m_strFileValue = (char*)_bstr_t(m_vFieldValue);
	//获取学号
	m_number = m_strFileValue;
	m_vFieldValue.Clear();

	m_vFieldValue = m_pRs->GetCollect("NAME");
	//判断看NAME是否为空.
	if (m_vFieldValue.vt != VT_NULL)
	{
		m_strFileValue = (char*)_bstr_t(m_vFieldValue);
	}
	else
	{
		m_strFileValue = "";
	}
	m_name = m_strFileValue;
	m_vFieldValue.Clear();

	m_vFieldValue = m_pRs->GetCollect("ADDRESS");
	if (m_vFieldValue.vt != VT_NULL)
	{
		m_strFileValue = (char*)_bstr_t(m_vFieldValue);
	}
	else
	{
		m_strFileValue = "";
	}
	m_address = m_strFileValue;
	m_vFieldValue.Clear();

	m_vFieldValue = m_pRs->GetCollect("DATE");
	if (m_vFieldValue.vt != VT_NULL)
	{
		m_strFileValue = (char*)_bstr_t(m_vFieldValue);
	}
	else
	{
		m_strFileValue = "";
	}
	m_date = m_strFileValue;
	m_vFieldValue.Clear();

	m_vFieldValue = m_pRs->GetCollect("SCORE");
	if (m_vFieldValue.vt != VT_NULL)
	{
		m_strFileValue = (char*)_bstr_t(m_vFieldValue);
	}
	else
	{
		m_strFileValue = "";
	}
	m_score = m_strFileValue;
	m_vFieldValue.Clear();

	UpdateData(false);
}

//新建记录
void CADOMFCDlg::OnButtAddNew() 
{
	// TODO: Add your control notification handler code here
	//把当前保存数据的指针移动到最后一行
	m_pRs->MoveLast();
	//调用增加数据函数
	m_pRs->AddNew();
	//显示数据
	DispFileds();
}

//保存新建
void CADOMFCDlg::OnButtSave() 
{
	// TODO: Add your control notification handler code here
	setFileds();
}

//保存新建的记录
void CADOMFCDlg::setFileds()
{
	//初始化对话框,读数据
	UpdateData(true);
	//把对话框里的数据读入数据库表中
	//前一个CComVariant(0)的序号对应数据库表中字段的序号
	HRESULT hr = m_pRs->put_Collect(CComVariant(0), CComVariant(m_number));
	m_pRs->put_Collect(CComVariant(1), CComVariant(m_name));
	m_pRs->put_Collect(CComVariant(4), CComVariant(m_score));
	m_pRs->put_Collect(CComVariant(3), CComVariant(m_date));
	m_pRs->put_Collect(CComVariant(2), CComVariant(m_address));
	hr = m_pRs->Update();
	MessageBox("添加数据成功!");
}

//获取下一条记录
void CADOMFCDlg::OnButtNext() 
{
	// 调用函数获取下一条记录
	m_pRs->MoveNext();
	//判断是否结束
	//rename( "EOF", "adoEOF" )在StdAfx.h进行改名.防止和文件的EOF重复
	if (m_pRs->adoEOF == VARIANT_FALSE)
	{
		DispFileds();
	}
	else
	{
		//移到上一条记录,因为在表中最后一条为空.
		m_pRs->MovePrevious();
		MessageBox("最后一条记录!");
	}
	
}

//获取上一条记录
void CADOMFCDlg::OnButtPrev() 
{
	// TODO: Add your control notification handler code here
	if (m_pRs->GetBOF() == VARIANT_FALSE)
	{
		m_pRs->MovePrevious();
		if (m_pRs->GetBOF() == VARIANT_FALSE)
		{
			DispFileds();
		}
		else
		{
			return;
		}
	}
	else
	{
		MessageBox("已经是第一条记录!");
	}
}

//获取最后一条记录
void CADOMFCDlg::OnButtLast() 
{
	// TODO: Add your control notification handler code here
	//调用函数移动到最后一条记录
	m_pRs->MoveLast();
//	if (m_pRs->adoEOF == VARIANT_FALSE)
//	{
		DispFileds();
//	}
//	else
//	{
//		m_pRs->MovePrevious();
//		MessageBox("已经是最后一条记录!");
//	}
}

//获取第一条记录
void CADOMFCDlg::OnButtFirst() 
{
	// TODO: Add your control notification handler code here
	m_pRs->MoveFirst();
	DispFileds();
}

//删除当前记录
void CADOMFCDlg::OnButtDelete() 
{
	if (IDOK == MessageBox("是否删除当前数据?", "当前数据",MB_YESNO))
	{
		//获取当前删除数据
		HRESULT hr = m_pRs->Delete(adAffectCurrent);
		hr = m_pRs->Update();
	}
	//移动到下一条.
	OnButtNext();
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -