发布时间 : 星期一 文章用ado连接access更新完毕开始阅读d1e0d2eb19e8b8f67c1cb962
//////////////////////////////////////////////////////// /////////////////////////////////////////////// 11)-------------------
-////////////////////////////////////////////////////////////////////////////// //名称:OnQuery //功能:查询记录
///////////////////////////////////////////////////////////////////////////// void CAdoRWAccessDlg::OnQuery() {
UpdateData(TRUE);
//构造Sql语句 CString strSql;
if(m_Name != \
strSql.Format(\ m_Name,m_Age);
else if(m_Name != \
strSql.Format(\ else if(m_Name == \
strSql.Format(\ else
strSql = \ try {
//先关闭已经打开的记录集对象 m_pRecordset->Close();
//根据新的Sql查询语句,重新打开记录集对象
m_pRecordset->Open(strSql.AllocSysString(), theApp.m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText); }
catch(_com_error *e) {
AfxMessageBox(e->ErrorMessage()); }
//将查询后得到的新记录集显示出来 ListData(); }
12)关闭记录集和连接
BOOL CDemoadoDlg::DestroyWindow() {
m_pRecordset->Close (); m_pRecordset = NULL;
return CDialog::DestroyWindow(); }
BOOL CDemoadoDlg::DestroyWindow() {
//
m_pRecordset->Close (); m_pRecordset = NULL;
return CDialog::DestroyWindow(); }
//////////////////////////////////////////////////////////////////////////////////////////// //////
------------------------------------------------------------------------------------------- --
注意!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
CAdoRWAccessDlg::CAdoRWAccessDlg(CWnd* pParent /*=NULL*/) : CDialog(CAdoRWAccessDlg::IDD, pParent) {
//{{AFX_DATA_INIT(CAdoRWAccessDlg) m_Name = _T(\ m_Age = _T(\
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); }
void CAdoRWAccessDlg::DoDataExchange(CDataExchange* pDX) {
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAdoRWAccessDlg) DDX_Control(pDX, IDC_QUERY, m_Query);
DDX_Control(pDX, IDC_WRITEACCESS, m_Write); DDX_Control(pDX, IDC_READACCESS, m_Read); DDX_Control(pDX, IDC_MODIFY, m_Modify); DDX_Control(pDX, IDC_DELETE, m_Delete);
DDX_Control(pDX, IDC_LISTACCESS, m_AccessList); DDX_Text(pDX, IDC_NAME, m_Name); DDV_MaxChars(pDX, m_Name, 10); DDX_Text(pDX, IDC_AGE, m_Age); DDV_MaxChars(pDX, m_Age, 3); //}}AFX_DATA_MAP }
///////////////////////////////////////////////////////////////////////////// //名称:OnInitDialog //功能:初始化对话框
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////// BOOL CAdoRWAccessDlg::OnInitDialog() {
CDialog::OnInitDialog();
// 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
// 初始化XP风格按钮
m_Read.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30); m_Read.SetBkColor(RGB(162,189,252));
m_Read.SetTooltipText(_T(\读取全部记录\ m_Read.SetRounded(TRUE);
m_Write.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30); m_Write.SetBkColor(RGB(162,189,252)); m_Write.SetTooltipText(_T(\插入新记录\ m_Write.SetRounded(TRUE);
m_Modify.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30); m_Modify.SetBkColor(RGB(162,189,252));
m_Modify.SetTooltipText(_T(\修改当前记录\ m_Modify.SetRounded(TRUE);
m_Delete.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30); m_Delete.SetBkColor(RGB(162,189,252));
m_Delete.SetTooltipText(_T(\删除当前记录\ m_Delete.SetRounded(TRUE);
m_Query.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30); m_Query.SetBkColor(RGB(162,189,252)); m_Query.SetTooltipText(_T(\关于\
m_Query.SetRounded(TRUE);
//使用ADO创建数据库记录集
m_pRecordset.CreateInstance(__uuidof(Recordset));
// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息, // 因为它有时会经常出现一些想不到的错误
void CAdoRWAccessDlg::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 CAdoRWAccessDlg::OnQueryDragIcon() {
return (HCURSOR) m_hIcon; }
class CAdoRWAccessDlg : public CDialog {
// Construction
public:
CAdoRWAccessDlg(CWnd* pParent = NULL); // standard constructor
// 定义ADO连接、命令、记录集变量指针 _ConnectionPtr m_pConnection; _CommandPtr m_pCommand; _RecordsetPtr m_pRecordset;
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////// // Dialog Data
//{{AFX_DATA(CAdoRWAccessDlg)
enum { IDD = IDD_ADORWACCESS_DIALOG }; CWinXPButtonST m_Query; CWinXPButtonST m_Write; CWinXPButtonST m_Read; CWinXPButtonST m_Modify; CWinXPButtonST m_Delete; CListBox m_AccessList; CString m_Name; CString m_Age; //}}AFX_DATA
// ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAdoRWAccessDlg) public:
virtual BOOL DestroyWindow(); protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL
// Implementation protected:
HICON m_hIcon;
本文来自CSDN博客,转载请标明出http://blog.csdn.net/dan251906761/archive/2008/07/25/2709885.aspx
处: