📄 adotestdlg.cpp
字号:
// AdoTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AdoTest.h"
#include "AdoTestDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CAdoTestDlg dialog
CAdoTestDlg::CAdoTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAdoTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAdoTestDlg)
m_name = _T("");
m_show = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CAdoTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAdoTestDlg)
DDX_Text(pDX, IDC_name, m_name);
DDX_Text(pDX, IDC_show, m_show);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAdoTestDlg, CDialog)
//{{AFX_MSG_MAP(CAdoTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDchange, Onchange)
ON_BN_CLICKED(IDdelet, Ondelet)
ON_BN_CLICKED(IDfind, Onfind)
ON_BN_CLICKED(IDC_Showed, OnShowed)
ON_BN_CLICKED(IDC_first, Onfirst)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAdoTestDlg message handlers
BOOL CAdoTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
try
{
HRESULT hr=m_pConnection.CreateInstance("ADODB.Connection");//创建Connection对象
m_pRecordset.CreateInstance("ADODB.Recordset");
if(SUCCEEDED(hr))
{
hr=m_pConnection->Open("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=wynnew;Data Source=3093FA2FA8CC444","","",adConnectUnspecified);//连接数据库
//上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51; }
_variant_t ra;
try
{
m_pRecordset->Open("SELECT * FROM 姓名",_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
}
catch (_com_error e)
{
m_pConnection->Execute("CREATE TABLE 姓名(name TEXT)",&ra,adCmdText);
m_pRecordset->Open("SELECT * FROM 姓名",_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
}
}
}
catch (_com_error e) //COM错误取得,当执行COM功能的时候,如果出错,可以捕捉到_com_error的异常
{
CString strComError;
strComError.Format("错误编号: %08lx\n错误信息: %s\n错误源: %s\n错误描述: %s",
e.Error(), // 错误编号
e.ErrorMessage(), // 错误信息
(LPCSTR) e.Source(), // 错误源
(LPCSTR) e.Description()); // 错误描述
::MessageBox(NULL,strComError,"错误",MB_ICONEXCLAMATION);
}
// 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 CAdoTestDlg::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 CAdoTestDlg::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 CAdoTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CAdoTestDlg::OnOK() //向表中增加一条记录
{
// TODO: Add extra validation here
UpdateData(true);
if (m_name=="")
{
MessageBox("输入不能为空!");
}
else
{
_variant_t vname;
m_pRecordset->AddNew(); //添加新记录
m_pRecordset->PutCollect("name",_variant_t((m_name)));
m_pRecordset->Update();
//m_pRecordset->Close();
MessageBox("增加记录成功!");
}
//CDialog::OnOK();
}
void CAdoTestDlg::Onchange() //在表中找到符合要求的字段并替换一条记录
{
// TODO: Add your control notification handler code here
UpdateData(true);
if (m_name==""||m_show=="")
{
MessageBox("输入不能为空!");
}
else
{
_variant_t vname;
CString changname;
m_pRecordset->MoveFirst();
while(!m_pRecordset->adoEOF)
{
changname=m_pRecordset->GetCollect("name").bstrVal;
if (m_name==changname)
{
m_pRecordset->Delete(adAffectCurrent); //删除当前记录
m_pRecordset->Update();
m_pRecordset->AddNew(); //添加新记录
m_pRecordset->PutCollect("name",_variant_t((m_show)));
m_pRecordset->Update();
}
m_pRecordset->MoveNext();
}
//m_pRecordset->Close();
MessageBox("替换成功!");
}
}
void CAdoTestDlg::Ondelet() //删除表中的一条记录
{
// TODO: Add your control notification handler code here
UpdateData();
CString changname;
if (m_name=="")
{
MessageBox("输入不能为空!");
}
else
{
m_pRecordset->MoveFirst(); //移到首条记录
while(!m_pRecordset->adoEOF)
{
changname=m_pRecordset->GetCollect("name").bstrVal;
if (m_name==changname)
{
m_pRecordset->Delete(adAffectCurrent); //删除当前记录
}
m_pRecordset->MoveNext();
}
//m_pRecordset->Close();
//m_pRecordset->Delete(adAffectCurrent); //删除当前记录
//m_pRecordset->Update();
//m_pRecordset->Close();
MessageBox("删除成功!");
}
}
void CAdoTestDlg::Onfind() //查找表中的记录
{
// TODO: Add your control notification handler code here
}
void CAdoTestDlg::OnShowed() //显示表中对应字段的记录值
{
// TODO: Add your control notification handler code here
_variant_t vshowname;
//vID = m_pRecordset->GetCollect(_variant_t((long)0)); //取得第1列的值,从0开始计数,你也可以直接给出列的名称,如下一行
while (!m_pRecordset->adoEOF)
{
m_show=m_pRecordset->GetCollect("name").bstrVal; //取得姓名字段的值
//m_pRecordset->Update();
break;
}
if (m_pRecordset->adoEOF)
{
MessageBox("显示结束!");
}
if (!m_pRecordset->adoEOF)
{
m_pRecordset->MoveNext();
}
UpdateData(false);
}
void CAdoTestDlg::Onfirst()
{
// TODO: Add your control notification handler code here
m_pRecordset->MoveFirst();
m_show=m_pRecordset->GetCollect("name").bstrVal;
UpdateData(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -