触摸屏租赁
Touch screen rental
广告机租赁
Advertising rental
机器人租赁
Robot leasing
 当前位置:首页 > > 常规数据库调用(列表框)

代码说明

(1)   stdafx.h 加入如下代码引入 ADO 库定义文件,

// stdafx.h : include file for standard system include files,

。。。。。。。。。。。。。。。。

#include <afxcmn.h>                   // MFC support for Windows Common Controls

#endif // _AFX_NO_AFXCMN_SUPPORT

#import "c:\program files\common files\system\ado\msado15.dll" \

                     no_namespace \

              rename ("EOF", "adoEOF")

。。。。。。。。。。。。。。

(2)初始化COM库,在BOOL CAdoAccessApp::InitInstance()中添加

BOOL CAdoAccessApp::InitInstance()

{

       AfxEnableControlContainer();

AfxOleInit();

。。。。。。。。。。。

(3)在类 CAdoAccessApp中添加一个用于打开数据库连接的变量;

_ConnectionPtr m_pConnection;

(4)在类 CgetFieldNameDlg 中添加两个用于打开数据库连接和打开记录集的变量和一个判断打开成功与否的变量;

_ConnectionPtr m_pConnection;

_RecordsetPtr m_pRecordset;

BOOL m_bSuccess;

 

(5) 在类CAdoAccessApp中添加函数ExitInstance()并始化代码;

int CAdoAccessApp::ExitInstance()

{

       if(m_pConnection->State)

       m_pConnection->Close(); ///如果已经打开了连接则关闭它

       return CWinApp::ExitInstance();

}

(6) 在文件AdoAccessDlg.cpp中加入相应代码;

// AdoAccessDlg.cpp : implementation file

//

#include "stdafx.h"

#include "AdoAccess.h"

#include "AdoAccessDlg.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

extern CAdoAccessApp theApp;

/////////////////////////////////////////////////////////////////////////////

// CAboutDlg dialog used for App About

.。。。。。。。。。。。。。。。。。。。。。。

 

(7) 在类 CAdoAccessApp中的InitInstance() 函数中加入相应代码;

BOOL CAdoAccessApp::InitInstance()

{

       AfxEnableControlContainer();

       AfxOleInit();

       ////////////连接数据库//////////////

       HRESULT hr;

       try

       {

       hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象

       if(SUCCEEDED(hr))

       {

       hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb","","",adModeUnknown);///连接数据库

       ///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51;  }

       }

       }

       catch(_com_error e)///捕捉异常

       {

              CString errormessage;

              errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());

              AfxMessageBox(errormessage);///显示错误信息

              return FALSE;

       }

(8) Listbox控件IDIDC_USERLIST添加控件变量m_userlist

(9) 在类 CgetFieldNameDlg 中的OnInitDialog()函数中加入相应代码;

BOOL CAdoAccessDlg::OnInitDialog()

{

       CDialog::OnInitDialog();

       ::SendMessage(m_userlist.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);

       //////////为列表控件添加列//////////

       m_userlist.InsertColumn(0,"用户ID",LVCFMT_LEFT,60);

       m_userlist.InsertColumn(1,"用户名",LVCFMT_LEFT,100);

       m_userlist.InsertColumn(2,"年龄",LVCFMT_LEFT,60);

       m_userlist.InsertColumn(3,"生日",LVCFMT_LEFT,100);

       //////////读取数据库中的信息添加到列表控件///////////

       int nItem;

       _variant_t vUsername,vBirthday,vID,vOld;

       try

       {

              m_pRecordset.CreateInstance("ADODB.Recordset");

              m_pRecordset->Open("SELECT * FROM users",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);

              m_bSuccess = TRUE;

              while(!m_pRecordset->adoEOF)

              {

                     vID = m_pRecordset->GetCollect("ID");

                     vUsername = m_pRecordset->GetCollect("username");

                     vOld = m_pRecordset->GetCollect("old");

                     vBirthday = m_pRecordset->GetCollect("birthday");

                     nItem=m_userlist.InsertItem(0xffff,(_bstr_t)vID);

                     m_userlist.SetItem(nItem,1,1,(_bstr_t)vUsername,NULL,0,0,0);

                     m_userlist.SetItem(nItem,2,1,(_bstr_t)vOld,NULL,0,0,0);

                     m_userlist.SetItem(nItem,3,1,(_bstr_t)vBirthday,NULL,0,0,0);

                     m_pRecordset->MoveNext();

              }

 

       }

       catch(_com_error e)///捕捉异常

       {

              AfxMessageBox("读取数据库失败!");///显示错误信息

       }

       // 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

      

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

}

 

void CAdoAccessDlg::OnSysCommand(UINT nID, LPARAM lParam)

{

       if ((nID & 0xFFF0) == IDM_ABOUTBOX)

       {

              CAboutDlg dlgAbout;

              dlgAbout.DoModal();

       }

       else

       {

              CDialog::OnSysCommand(nID, lParam);

       }

}

 

 


工作室地址:重庆石桥铺电脑城B座 | 微信:z35544216 | 网址:www.35544216.com