// ADOTest1Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "ADOTest1.h"
#include "ADOTest1Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CADOTest1App 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)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CADOTest1Dlg dialog
CADOTest1Dlg::CADOTest1Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CADOTest1Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CADOTest1Dlg)
m_nUserID = 0;
m_nOld = 0;
m_sUsername = _T("");
m_tBirthday = COleDateTime::GetCurrentTime();
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_nCurrentSel = -1;
m_bSuccess = FALSE;
m_bAutoSave = TRUE;
}
void CADOTest1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CADOTest1Dlg)
DDX_Control(pDX, IDC_DELITEM, m_cDelItem);
DDX_Control(pDX, IDC_ADDITEM, m_cAddItem);
DDX_Control(pDX, IDC_USERLIST, m_userlist);
DDX_Text(pDX, IDC_USERID, m_nUserID);
DDX_Text(pDX, IDC_OLD, m_nOld);
DDX_Text(pDX, IDC_USERNAME, m_sUsername);
DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER1, m_tBirthday);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CADOTest1Dlg, CDialog)
//{{AFX_MSG_MAP(CADOTest1Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ADDITEM, OnAdditem)
ON_BN_CLICKED(IDC_DELITEM, OnDelitem)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_USERLIST, OnItemchangedUserlist)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CADOTest1Dlg message handlers
BOOL CADOTest1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_cDelItem.EnableWindow(FALSE);
::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),ADODB::adOpenStatic,ADODB::adLockOptimistic,ADODB::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 CADOTest1Dlg::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 CADOTest1Dlg::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 CADOTest1Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CADOTest1Dlg::OnOK()
{
if(m_bSuccess)
{
m_pRecordset->Update();
m_pRecordset->Close();
}
CDialog::OnOK();
}
void CADOTest1Dlg::OnAdditem()
{
if(UpdateData())
if(m_sUsername.GetLength()>0)
{
m_pRecordset->AddNew();
m_nCurrentSel = m_userlist.InsertItem(0xffff,"");
SaveData();///保存数据
m_userlist.SetItemState(m_nCurrentSel,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
m_userlist.SetHotItem(m_nCurrentSel);
m_userlist.SetFocus();
}
else
AfxMessageBox("请输入用户名");
}
void CADOTest1Dlg::OnDelitem()
{
m_bAutoSave = FALSE;
if(m_nCurrentSel >= 0)
{
m_userlist.DeleteItem(m_nCurrentSel);
int count = m_userlist.GetItemCount();
if(count <= m_nCurrentSel)
m_nCurrentSel = count-1;
m_pRecordset->Delete(ADODB::adAffectCurrent);
m_pRecordset->MoveNext();
LoadData();
m_userlist.SetItemState(m_nCurrentSel,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
m_userlist.SetFocus();
}
m_bAutoSave = TRUE;
}
//////////在选择列表框的时候调用///////////////////
void CADOTest1Dlg::OnItemchangedUserlist(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(pNMListView->uNewState&LVIS_SELECTED)
{
UpdateData();
SaveData();///保存旧数据
m_nCurrentSel = pNMListView->iItem;
LoadData();///加载新数据
m_cDelItem.EnableWindow();
}
*pResult = 0;
}
//////将记录集中的数据加载到编辑框/////
void CADOTest1Dlg::LoadData()
{
m_pRecordset->Move(m_nCurrentSel,_variant_t((long)ADODB::adBookmarkFirst));
vUserID = m_pRecordset->GetCollect("ID");
vUsername = m_pRecordset->GetCollect("username");
vOld = m_pRecordset->GetCollect("old");
vBirthday = m_pRecordset->GetCollect("birthday");
m_nUserID = vUserID.lVal;
m_sUsername = (LPCTSTR)(_bstr_t)vUsername;
m_nOld = vOld.lVal;
m_tBirthday = vBirthday;
UpdateData(FALSE);
}
/////将编辑框的数据保存到记录集与列表框
void CADOTest1Dlg:
没有合适的资源?快使用搜索试试~ 我知道了~
ADO 数据库示例 初学者必备 教会你用ado连sql
共42个文件
tlh:4个
tli:4个
h:4个
5星 · 超过95%的资源 需积分: 9 14 下载量 159 浏览量
2010-05-21
16:42:39
上传
评论
收藏 4.58MB RAR 举报
温馨提示
ADO+数据库示例 ado 数据库 vc 初学者必备。ado精华
资源推荐
资源详情
资源评论
收起资源包目录
ADO+数据库示例adotest.rar (42个子文件)
ADO+数据库示例adotest
adotest
msadox.tli 30KB
ADOTest1.ncb 265KB
StdAfx.cpp 210B
ADOTest1.clw 2KB
ADOTest1.rc 6KB
ADOTest1.cpp 3KB
ADOTest1.plg 2KB
ADOTest1.aps 25KB
msado15.tlh 89KB
Debug
ADOTest1.pdb 457KB
msadox.tli 30KB
ADOTest1.pch 10.77MB
ADOTest1Dlg.obj 87KB
ADOTest1.res 6KB
vc60.pdb 620KB
ADOTest1.bsc 3.78MB
ADOTest1.sbr 0B
ADOTest1.exe 132KB
vc60.idb 241KB
msado15.tlh 89KB
msado15.tli 75KB
msadox.tlh 37KB
ADOTest1.obj 47KB
StdAfx.obj 138KB
ADOTest1.ilk 272KB
StdAfx.sbr 1.93MB
ADOTest1Dlg.sbr 0B
msado15.tli 75KB
StdAfx.h 1KB
ADOTest1.dsw 541B
msadox.tlh 37KB
ADOTest1.h 1KB
Resource.h 1KB
ADOTest1.dsp 4KB
ADOTest1.opt 54KB
ADOTest1Dlg.h 2KB
test.mdb 256KB
res
ADOTest1.ico 1KB
VCKBASE.bmp 3KB
Thumbs.db 5KB
ADOTest1.rc2 400B
ADOTest1Dlg.cpp 9KB
共 42 条
- 1
资源评论
- tonghua8892012-08-08初学,很有用的,
- 破晓春雨2012-07-30对初学者来说是很好的资料,浅显易懂
well_e
- 粉丝: 1
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功