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

📄 testaccdbdlg.cpp

📁 论坛中有许多操作Access数据库的介绍、说明
💻 CPP
字号:
// TestAccDBDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestAccDB.h"
#include "TestAccDBDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CTestAccDBDlg dialog

CTestAccDBDlg::CTestAccDBDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestAccDBDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestAccDBDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestAccDBDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestAccDBDlg)
	DDX_Control(pDX, IDC_COMBO1, m_cCombox1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestAccDBDlg, CDialog)
	//{{AFX_MSG_MAP(CTestAccDBDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_CBN_SELENDOK(IDC_COMBO1, OnSelendokCombo1)
	ON_CBN_DROPDOWN(IDC_COMBO1, OnDropdownCombo1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestAccDBDlg message handlers

BOOL CTestAccDBDlg::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
	//连接Access数据库
	GetCurrentDirectory(255,m_DBConnInfo.DBPath);

	sprintf(m_DBConnInfo.DBName,"%s","notes.mdb");
	m_MyDB = new CAccessDB(m_DBConnInfo.DBPath,m_DBConnInfo.DBName,1);
	if(!m_MyDB->OpenDB(m_DBConnInfo.DBName))
	{
		AfxMessageBox("DB Open Error!");
		return FALSE;
	}

	CString szSql;
	szSql.Format("select * from friend");
	if(!m_MyDB->SetDataSet(NULL,szSql.GetBuffer(0)))
		return TRUE;
	int iRowCount=m_MyDB->GetDataSetRowCount(1);

	if(!iRowCount)
		return TRUE;

	char name[64],age[12],tel[15];
	for(int i=0;i<iRowCount;i++)
	{
		memset(name,0,sizeof(name));
		memset(age,0,sizeof(age));
		memset(tel,0,sizeof(tel));
		m_MyDB->GetDataSetFieldValue(i,0,name);
		m_MyDB->GetDataSetFieldValue(i,1,age);
		m_MyDB->GetDataSetFieldValue(i,2,tel);
		m_cCombox1.AddString(name);
	}

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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


//select 
void CTestAccDBDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CString szSql;
	szSql.Format("select count(*) from friend");
	if(!m_MyDB->SetDataSet(NULL,szSql.GetBuffer(0)))
		return ;
	char buf[10];
	int iRowCount=m_MyDB->GetDataSetFieldValue(0,buf);

	szSql.Format("当前记录条数[%s]",buf);
	AfxMessageBox(szSql);
	
}


//添加
void CTestAccDBDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	CString szSql,name,age,tel;
	GetDlgItemText(IDC_EDIT1,name);
	GetDlgItemText(IDC_EDIT2,age);
	GetDlgItemText(IDC_EDIT3,tel);

	szSql.Format("insert into friend ( name,age,tel) values ('%s',%s,'%s')",name,age,tel);
	if(!m_MyDB->ExecSQL((unsigned char *)szSql.GetBuffer(0)))
		AfxMessageBox("DB Error");
	else
		MessageBox("insert successful ...","AccessDB test",MB_OK|MB_ICONINFORMATION);
	
}

//更新
void CTestAccDBDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	CString szSql,name,age,tel;
	GetDlgItemText(IDC_EDIT1,name);
	GetDlgItemText(IDC_EDIT2,age);
	GetDlgItemText(IDC_EDIT3,tel);

	szSql.Format("update friend set name='%s',age=%s,tel='%s' where name='%s'",name,age,tel,m_CurName);
	if(!m_MyDB->ExecSQL((unsigned char *)szSql.GetBuffer(0)))
		AfxMessageBox("DB Error");
	else
		MessageBox("update successful ...","AccessDB test",MB_OK|MB_ICONINFORMATION);
	
}

//删除
void CTestAccDBDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	CString szSql;

	szSql.Format("delete * from friend where name='%s'",m_CurName);
	if(!m_MyDB->ExecSQL((unsigned char *)szSql.GetBuffer(0)))
		AfxMessageBox("DB Error");
	else
		MessageBox("delete successful ...","AccessDB test",MB_OK|MB_ICONINFORMATION);

	
}

void CTestAccDBDlg::OnSelendokCombo1() 
{
	// TODO: Add your control notification handler code here
	CString szSql,name;
	char age[10],tel[15];
	GetDlgItemText(IDC_COMBO1,name);

	m_CurName=name;

	szSql.Format("select age,tel from friend where name='%s'",m_CurName);
	if(!m_MyDB->SetDataSet(NULL,szSql.GetBuffer(0)))
		return ;

	m_MyDB->GetDataSetFieldValue(0,0,age);
	m_MyDB->GetDataSetFieldValue(0,1,tel);

	SetDlgItemText(IDC_EDIT1,name);
	SetDlgItemText(IDC_EDIT2,age);
	SetDlgItemText(IDC_EDIT3,tel);
	
}

void CTestAccDBDlg::OnDropdownCombo1() 
{
	// TODO: Add your control notification handler code here
	CString szSql;
	szSql.Format("select * from friend");
	if(!m_MyDB->SetDataSet(NULL,szSql.GetBuffer(0)))
		return ;
	int iRowCount=m_MyDB->GetDataSetRowCount(1);

	if(!iRowCount)
		return ;

	char name[64],age[12],tel[15];
	m_cCombox1.ResetContent();
	for(int i=0;i<iRowCount;i++)
	{
		memset(name,0,sizeof(name));
		memset(age,0,sizeof(age));
		memset(tel,0,sizeof(tel));
		m_MyDB->GetDataSetFieldValue(i,0,name);
		m_MyDB->GetDataSetFieldValue(i,1,age);
		m_MyDB->GetDataSetFieldValue(i,2,tel);
		m_cCombox1.AddString(name);
	}
	
}

⌨️ 快捷键说明

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