accessdlg.cpp
来自「DAO操作Access数据库类」· C++ 代码 · 共 338 行
CPP
338 行
// BY: QQ:316118740
//博客 http://hi.baidu.com/alalmn
//希望能结识更多数据库开发的朋友
// ACCESSDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ACCESS.h"
#include "ACCESSDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CACCESSDlg dialog
CACCESSDlg::CACCESSDlg(CWnd* pParent /*=NULL*/)
: CDialog(CACCESSDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CACCESSDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CACCESSDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CACCESSDlg)
DDX_Control(pDX, IDC_LIST1, m_LineList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CACCESSDlg, CDialog)
//{{AFX_MSG_MAP(CACCESSDlg)
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_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CACCESSDlg message handlers
void CACCESSDlg::abc()
{
/* CString szSqlq;
szSqlq.Format("select * from friend");
//查询所有列名 查询friend表单
m_MyDB->SetDataSet(NULL,szSqlq.GetBuffer(0));
int iRowCount1=m_MyDB->GetDataSetRowCount(1); //获取记录数
char name[64],age[12],tel[15];
for(int i=0;i<iRowCount1;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); //列名
int n=m_LineList.GetItemCount();
m_LineList.InsertItem(n,name); //姓名
m_LineList.SetItemText(n,1,age); //年龄
m_LineList.SetItemText(n,2,tel); //电话
} */
}
BOOL CACCESSDlg::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);
}
}
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//上线列表初始化
LONG lStyle = m_LineList.SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
lStyle |= LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES;
m_LineList.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0,(LPARAM)lStyle);
m_LineList.InsertColumn(0,"姓名",NULL,150,-1);
m_LineList.InsertColumn(1,"年龄",NULL,110,-1);
m_LineList.InsertColumn(2,"电话",NULL,140,-1);
///////////////////////////
//连接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 数据库打开错误!");
return FALSE;
}
CString szSql;
szSql.Format("select * from friend");
//查询所有列名 查询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); //列名
int n=m_LineList.GetItemCount();
m_LineList.InsertItem(n,name); //姓名
m_LineList.SetItemText(n,1,age); //年龄
m_LineList.SetItemText(n,2,tel); //电话
}
//abc(); //连接数据库
return TRUE; // return TRUE unless you set the focus to a control
}
void CACCESSDlg::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 CACCESSDlg::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();
}
}
HCURSOR CACCESSDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CACCESSDlg::OnButton1() //添加
{
// 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("写入信息 失败!!!");
else
MessageBox("插入成功 ...","AccessDB测试",MB_OK|MB_ICONINFORMATION);
abc(); //连接数据库
}
void CACCESSDlg::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("update friend set name='%s',age=%s,tel='%s' where name='%s'",name,age,tel,name);
if(!m_MyDB->ExecSQL((unsigned char *)szSql.GetBuffer(0)))
AfxMessageBox("修改信息 失败!!!");
else
MessageBox("更新成功… ","AccessDB测试",MB_OK|MB_ICONINFORMATION);
abc(); //连接数据库
}
void CACCESSDlg::OnButton3() //统计当前记录数
{
// 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 CACCESSDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
CString s1,s2,s3,s4;
// TODO: Add your control notification handler code here
int nSelRows = m_LineList.GetSelectedCount(); //被选择的项的数目
if(!nSelRows) //-- If there are no rows selected,jump out here
return;
POSITION pos =m_LineList.GetFirstSelectedItemPosition(); //返回一个用于迭代或获取对象指针的POSITION值;如果没有选择任何项,那么将返回NULL。
int i = m_LineList.GetNextSelectedItem(pos); //返回列表视图控件中被选择的下一项的索引值
TRACE("First Item %d\n",i);
if (i != -1) //-- Execute this loop as long as GetNextSelectedItem() returns -1
s1= m_LineList.GetItemText(i, 0); //获取信息
s2= m_LineList.GetItemText(i, 1); //获取信息
s3= m_LineList.GetItemText(i, 2); //获取信息
SetDlgItemText(IDC_EDIT1, s1);
SetDlgItemText(IDC_EDIT4, s1);
SetDlgItemText(IDC_EDIT2, s2);
SetDlgItemText(IDC_EDIT3, s3);
UpdateData(FALSE);
return;
*pResult = 0;
}
void CACCESSDlg::OnButton4()
{
// TODO: Add your control notification handler code here
CString name1;
GetDlgItemText(IDC_EDIT4,name1);
CString szSql;
szSql.Format("delete * from friend where name='%s'",name1);
if(!m_MyDB->ExecSQL((unsigned char *)szSql.GetBuffer(0)))
AfxMessageBox("删除信息 失败!!!");
else
MessageBox("删除成功… ","AccessDB测试 ",MB_OK|MB_ICONINFORMATION);
abc(); //连接数据库
}
void CACCESSDlg::OnButton5()
{
// TODO: Add your control notification handler code here
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?