📄 数据库dlg.cpp
字号:
// 数据库Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "数据库.h"
#include "数据库Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
CDaoDatabase db;
CDaoRecordset RecSet(&db);
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()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
m_Name = _T("");
m_Age = _T("");
m_Number = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
DDX_Control(pDX, IDC_LIST1, m_List);
DDX_Text(pDX, IDC_EDIT1, m_Name);
DDX_Text(pDX, IDC_EDIT2, m_Age);
DDX_Text(pDX, IDC_EDIT3, m_Number);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_PRE, OnBtnPre)
ON_BN_CLICKED(IDC_BTN_NEXT, OnBtnNext)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers
BOOL CMyDlg::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
//链接数据库
::GetCurrentDirectory(MAX_PATH,m_Path);
this->ConnectDAO();
ListView_SetExtendedListViewStyle( m_List.m_hWnd,
LVS_EX_FULLROWSELECT|LVS_EX_ONECLICKACTIVATE|LVS_EX_UNDERLINEHOT );
m_List.InsertColumn(0, "姓名");
m_List.SetColumnWidth(0, 150);
m_List.InsertColumn(1, "年龄");
m_List.SetColumnWidth(1, 150);
m_List.InsertColumn(2, "学号");
m_List.SetColumnWidth(2,150);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyDlg::ConnectDAO()
{
CString db_file=m_Path;
db_file+="\\temp.mdb";
CFileFind fd;
BOOL success=fd.FindFile(db_file);
fd.Close();
if(!success)
{
db.Create(db_file);
CString sqlcmd="create table DemoTable(Name VARCHAR(20),Age VARCHAR(3),Num VARCHAR(20));";
db.Execute(sqlcmd); //用SQL语句加入一个记录
db.Execute("insert into DemoTable (Name,Age,Num) values ('李伟然','22','2003314118')");
db.Close();
}
}
BOOL CMyDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
//db.Close();
return CDialog::DestroyWindow();
}
void CMyDlg::OnBtnPre() //
{
// TODO: Add your control notification handler code here
this->UpdateData(TRUE);
if(m_Name.IsEmpty())
{
AfxMessageBox("姓名不能为空!");
return;
}
if(m_Age.IsEmpty())
{
AfxMessageBox("年龄不能为空!");
return;
}
if(m_Number.IsEmpty())
{
AfxMessageBox("学号不能为空!");
return;
}
CString db_file=m_Path;
db_file+="\\temp.mdb";
db.Open(db_file); // 打开已创建的demo数据库及DamoTable表
RecSet.Open(AFX_DAO_USE_DEFAULT_TYPE,"select * from DemoTable",NULL);
RecSet.AddNew(); //用DAO函数加入一个记录
RecSet.SetFieldValue("Name",m_Name.GetBuffer(0));
RecSet.SetFieldValue("Age",m_Age.GetBuffer(0));
RecSet.SetFieldValue("Num",m_Number.GetBuffer(0));
RecSet.Update();
RecSet.Close();
db.Close();
}
void CMyDlg::OnBtnNext()
{
// TODO: Add your control notification handler code here
COleVariant var; // 字段类型
var.ChangeType(VT_BSTR,NULL);
CString db_file=m_Path;
db_file+="\\temp.mdb";
db.Open(db_file); // 打开已创建的demo数据库及DamoTable表
RecSet.Open(AFX_DAO_USE_DEFAULT_TYPE,"select * from DemoTable",NULL);
while(!RecSet.IsEOF()) // 有没有到表结尾
{
RecSet.GetFieldValue("Name",var);
m_Name=(LPCSTR)var.pbstrVal;
RecSet.GetFieldValue("Age",var);
m_Age=(LPCSTR)var.pbstrVal;
RecSet.GetFieldValue("Num",var);
m_Number=(LPCSTR)var.pbstrVal;
RecSet.MoveNext();
}
RecSet.Close(); //关闭记录集
this->UpdateData(FALSE);
db.Close();
}
void CMyDlg::OnOK()
{
// TODO: Add extra validation here
//CDialog::OnOK();
int nItem;
COleVariant var; // 字段类型
var.ChangeType(VT_BSTR,NULL);
CString db_file=m_Path;
db_file+="\\temp.mdb";
db.Open(db_file); // 打开已创建的demo数据库及DamoTable表
RecSet.Open(AFX_DAO_USE_DEFAULT_TYPE,"select * from DemoTable",NULL);
while(!RecSet.IsEOF()) // 有没有到表结尾
{
nItem = m_List.InsertItem( m_List.GetItemCount()+1,"");
RecSet.GetFieldValue("Name",var);
m_Name=(LPCSTR)var.pbstrVal;
RecSet.GetFieldValue("Age",var);
m_Age=(LPCSTR)var.pbstrVal;
RecSet.GetFieldValue("Num",var);
m_Number=(LPCSTR)var.pbstrVal;
m_List.SetItemText(nItem,0,m_Name);
m_List.SetItemText(nItem,1,m_Age);
m_List.SetItemText(nItem,2,m_Number);
RecSet.MoveNext();
}
RecSet.Close(); //关闭记录集
this->UpdateData(FALSE);
db.Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -