📄 conaccessdlg.cpp
字号:
// ConAccessDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ConAccess.h"
#include "ConAccessDlg.h"
#include "ADOConn.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CConAccessDlg dialog
CConAccessDlg::CConAccessDlg(CWnd* pParent /*=NULL*/)
: CDialog(CConAccessDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CConAccessDlg)
m_oid = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//m_index=0;
//m_name= 字符串怎么初始化?
}
void CConAccessDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CConAccessDlg)
DDX_Control(pDX, IDC_COMBO2, m_comgrade);
DDX_Control(pDX, IDC_COMBO1, m_combo);
DDX_Control(pDX, IDC_LIST2, m_Grid);
DDX_Text(pDX, IDC_EDIT3, m_oid);
//}}AFX_DATA_MAP
DDX_Text(pDX,IDC_EDIT1,m_index);
DDX_Text(pDX,IDC_EDIT2,m_name);
}
BEGIN_MESSAGE_MAP(CConAccessDlg, CDialog)
//{{AFX_MSG_MAP(CConAccessDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_COMMAND(IDC_BUTTON1,OnAdd)
ON_COMMAND(IDC_BUTTON2,OnModifyAndSave)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CConAccessDlg message handlers
BOOL CConAccessDlg::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
//初始化COM环境
//::CoInitialize(NULL);
//AfxEnableControlContainer();
// TODO: Add extra initialization here
m_Grid.SetExtendedStyle(LVS_EX_FLATSB
|LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP
|LVS_EX_ONECLICKACTIVATE|LVS_EX_GRIDLINES);
m_Grid.InsertColumn(0,"编号",LVCFMT_LEFT,110,0);
m_Grid.InsertColumn(1,"姓名",LVCFMT_LEFT,110,1);
m_Grid.InsertColumn(2,"性别",LVCFMT_LEFT,110,2);
m_Grid.InsertColumn(3,"学历",LVCFMT_LEFT,110,3);
AddToGrid();
m_combo.InsertString(0,"男");
m_combo.InsertString(1,"女");
m_combo.SetCurSel(0);
m_comgrade.InsertString(0,"本科");
m_comgrade.InsertString(1,"硕士");
m_comgrade.InsertString(2,"博士");
m_comgrade.SetCurSel(0);
//return TRUE;
return TRUE; // return TRUE unless you set the focus to a control
}
void CConAccessDlg::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 CConAccessDlg::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 CConAccessDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CConAccessDlg::OnInitADOConn()
{
/*try
{
//创建连接对象实例
m_pConnection.CreateInstance("ADODB.Connection");
//设置连接字符串
//Set conn = Server.CreateObject("ADODB.Connection")
CString strConnect="DRIVER={Microsoft Access Driver (*.mdb)};\
uid=;pwd=;DBQ=shujuku.mdb;";
//CString strConnect="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=shujuku.mdb;";
//CString strConnect="DBQ=" & C:\Program Files\Microsoft Visual Studio\MyProjects\ConAccess & \shujuku.mdb" & ";Driver={Microsoft Access Driver (*.mdb)};"
// uid=;pwd=;DBQ=C:\Program Files\Microsoft Visual Studio\MyProjects\ConAccess\shujuku.mdb;";
//使用open方法连接数据库
m_pConnection->Open((_bstr_t)strConnect,"","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}*/
}
void CConAccessDlg::ExitConnect()
{
/*//关闭记录集和连接
if(m_pRecordset!=NULL)
m_pRecordset->Close();
m_pConnection->Close();*/
}
void CConAccessDlg::AddToGrid()
{
//连接数据库
ADOConn m_adocon;
m_adocon.OnInitADOConn();
//设置查询字符串
_bstr_t bstrSQL="select * from employees order by 编号 desc";
//创建记录集对象实例
//ADOConn m_adocon;
m_adocon.m_pRecordset.CreateInstance(__uuidof(Recordset));
//打开记录集
m_adocon.m_pRecordset->Open(bstrSQL,m_adocon.m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
while(!m_adocon.m_pRecordset->adoEOF)
{
m_Grid.InsertItem(0,"");
m_Grid.SetItemText(0,0,(char*)(_bstr_t)m_adocon.m_pRecordset->GetCollect("编号"));
m_Grid.SetItemText(0,1,(char*)(_bstr_t)m_adocon.m_pRecordset->GetCollect("姓名"));
m_Grid.SetItemText(0,2,(char*)(_bstr_t)m_adocon.m_pRecordset->GetCollect("性别"));
m_Grid.SetItemText(0,3,(char*)(_bstr_t)m_adocon.m_pRecordset->GetCollect("学历"));
//将记录集指向下一个记录
m_adocon.m_pRecordset->MoveNext();
}
//断开数据库的连接
ExitConnect();
//::CoUninitialize();//释放COM环境*/
}
void CConAccessDlg::OnAdd()
{
UpdateData(true);
if(m_index.IsEmpty())
{
MessageBox("编号不能为空!");
return ;
}
if(m_name.IsEmpty())
{
MessageBox("姓名不能为空!");
return;
}
ADOConn m_adocon;
m_adocon.OnInitADOConn();
_bstr_t sql;
sql="select * from employees";
_RecordsetPtr m_pRecordset;
m_pRecordset=m_adocon.GetRecordSet(sql);
CString sex;
CString grade;
m_combo.GetLBText(m_combo.GetCurSel(),sex);
m_comgrade.GetLBText(m_comgrade.GetCurSel(),grade);
try
{
m_pRecordset->AddNew();//添加新行
m_pRecordset->PutCollect("编号",(_bstr_t)m_index);
m_pRecordset->PutCollect("姓名",(_bstr_t)m_name);
m_pRecordset->PutCollect("性别",(_bstr_t)sex);
m_pRecordset->PutCollect("学历",(_bstr_t)grade);
m_pRecordset->Update();
m_adocon.ExitConnect();
}
catch(_com_error e)
{ CString str;
str.Format("%s",e.ErrorMessage());
MessageBox(str);
//MessageBox("操作失败");
return;
}
MessageBox("保存成功");
//删除list control控件中的数据
m_Grid.DeleteAllItems();
AddToGrid();//重新遍历记录集
}
void CConAccessDlg::OnModifyAndSave()
{
UpdateData(true);
if(m_name.IsEmpty())
{
MessageBox("姓名不能为空!");
return ;
}
ADOConn m_adocon;
m_adocon.OnInitADOConn();
_bstr_t sql;
sql="select * from employees";
_RecordsetPtr m_pRecordset;
m_pRecordset=m_adocon.GetRecordSet(sql);
CString sex;
CString grade;
try
{
m_pRecordset->Move((long)pos,vtMissing);
m_combo.GetLBText(m_combo.GetCurSel(),sex);
m_comgrade.GetLBText(m_comgrade.GetCurSel(),grade);
m_pRecordset->PutCollect("编号",(_bstr_t)m_index);
m_pRecordset->PutCollect("姓名",(_bstr_t)m_name);
m_pRecordset->PutCollect("性别",(_bstr_t)sex);
m_pRecordset->PutCollect("学历",(_bstr_t)grade);
m_pRecordset->Update();
m_adocon.ExitConnect();
}
catch(...)
{
MessageBox("操作失败!");
return;
}
MessageBox("保存成功!");
m_Grid.DeleteAllItems();
AddToGrid();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -