📄 adoadodlg.cpp
字号:
// ADoADoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ADoADo.h"
#include "ADoADoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include"Mmsystem.h"
extern CADoADoApp theApp;
/////////////////////////////////////////////////////////////////////////////
// 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)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CADoADoDlg dialog
CADoADoDlg::CADoADoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CADoADoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CADoADoDlg)
m_Age = _T("");
m_Name = _T("");
m_Num = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CADoADoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CADoADoDlg)
DDX_Control(pDX, IDC_Listaccess, m_AccessList);
DDX_Text(pDX, IDC_AGE, m_Age);
DDX_Text(pDX, IDC_NAME, m_Name);
DDX_Text(pDX, IDC_NUM, m_Num);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CADoADoDlg, CDialog)
//{{AFX_MSG_MAP(CADoADoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ReadAccess, OnReadAccess)
ON_LBN_SELCHANGE(IDC_Listaccess, OnSelchangeListaccess)
ON_BN_CLICKED(IDC_WriteAccess, OnWriteAccess)
ON_BN_CLICKED(IDC_Modify, OnModify)
ON_BN_CLICKED(IDC_Delete, OnDelete)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_tuichu, Ontuichu)
ON_BN_CLICKED(IDC_music, Onmusic)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CADoADoDlg message handlers
BOOL CADoADoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_pRecordset.CreateInstance(__uuidof(Recordset));//使用ADO创建数据库记录集
// Add "About..." menu item to system menu.
try
{
m_pRecordset->Open("SELECT * FROM 数表", // 查询DemoTable表中所有字段
theApp.m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,////注意声明全局变量CNewadoApp theApp为外部变量即extern CNewadoApp theApp;因为app是一个全局类对象。所以要声明
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
// 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
return TRUE; // return TRUE unless you set the focus to a control
}
void CADoADoDlg::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 CADoADoDlg::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 CADoADoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CADoADoDlg::OnReadAccess()
{
//先关闭已经打开的记录集对象
m_pRecordset->Close();
try
{
m_pRecordset->Open("SELECT * FROM 数表 order by CreateTime", // 查询DemoTable表中所有字段
theApp.m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
ListData();////注意要对这个类进行声明
////做法:在CNewadoDlg右键点击 add menber function-》再定义类型为void的函数
}
void CADoADoDlg::ListData()
{
_variant_t var;
CString strName,strAge,strNum;
//效清空列表框
// m_AccessList.ResetContent();//////注意m_AccessList需被定义声明
m_AccessList.ResetContent();
//先建立一个列表框Listaccess,-》改名-》声明一个列表框的成员变量m_AccessList
strName=strAge=strNum = "";
//在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
//因为它有时会经常出现一些想不到的错误。
try
{
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();
else
{
AfxMessageBox("表内数据为空");
return;
}
// 读入库中各字段并加入列表框中
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);
var = m_pRecordset->GetCollect("Num");
if(var.vt != VT_NULL)
strNum = (LPCSTR)_bstr_t(var);
m_AccessList.AddString( strName + " ---> "+strAge +"--->"+strNum );
// m_AccessList.AddString (strid+"-->"+strName+"-->"+strAge+"-->"+strnum);
// m_AccessList.AddString(strid+"-->"strName+" -->"+strAge+"-->"+strnum);
m_pRecordset->MoveNext();
}
// m_pRecordset->MoveNext();
// 默认列表指向第一项,同时移动记录指针并显示
m_AccessList.SetCurSel(0);
OnSelchangeListaccess();///前一步已经对这个列表框命名 现在要建立一个类向导;
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
void CADoADoDlg::OnSelchangeListaccess()
{
int curSel = m_AccessList.GetCurSel();
_variant_t var,varIndex;
if(curSel < 0)
return;
try
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -