1.建立基本对话框
在类中新建两个变量CString m_strName; CString m_strPassword;
Cfinancial_sysDlg::Cfinancial_sysDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(Cfinancial_sysDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_strName = _T("娱乐");
m_strPassword = _T("123");
CoInitialize(NULL);
}
void Cfinancial_sysDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_name1, m_strName);
DDV_MaxChars(pDX,m_strName,8);
DDX_Text(pDX, IDC_password1, m_strPassword);
DDV_MaxChars(pDX,m_strPassword,8);
}
2.ado方法连接数据库
在stdafx.h中添加
#import "msado15.dll" no_namespace rename("EOF","adoEOF")
在financial_sysDlg.h 中添加变量_ConnectionPtr pConn; 用来连接数据库的对象
class Cfinancial_sysDlg : public CDialogEx
{
// 构造
public:
Cfinancial_sysDlg(CWnd* pParent = NULL);// 标准构造函数
.......
_ConnectionPtr pConn;
.........
}
在Cfinancial_sysDlg.cpp中 的构造函数中添加如下代码:
Cfinancial_sysDlg::Cfinancial_sysDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(Cfinancial_sysDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_strName = _T("娱乐");
m_strPassword = _T("123");
CoInitialize(NULL);
}
并在析构函数中添加
Cfinancial_sysDlg::~Cfinancial_sysDlg()
{
pConn->Close();
pConn.Release();
};
在financial_sysDlg.h 中定义析构函数
class Cfinancial_sysDlg : public CDialogEx
{
// 构造
public:
Cfinancial_sysDlg(CWnd* pParent = NULL);// 标准构造函数
.......
~Cfinancial_sysDlg();
.........
}
3.登录按钮与登录界面中输入的用户名和密码进行对比,以保证正常登录
UpdateData(TRUE);
CString strConnect;
strConnect =_T("Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+m_strName+".mdb;persist security info=false;Jet OLEDB:Database Password="+m_strPassword+"");
HRESULT hr;
hr = pConn.CreateInstance("ADODB.Connection");
try
{
if(SUCCEEDED(hr))
{
pConn->ConnectionTimeout = 15; //设置连接时间
pConn->Open(_bstr_t(strConnect),"","",adModeUnknown); //连接SQL SERVER
//char *pMessage;
//pMessage="连接数据库成功";
CString Message;
Message.Format(_T("连接数据库成功"));
AfxMessageBox(Message);
}
else
{
CString Message;
Message.Format(_T("ADO对象实例化失败"));
AfxMessageBox(Message);
}
}
catch(_com_error e) //捕捉异常
{
CString errMessage;
errMessage.Format(_T("连接数据库失败!\r\n错误信息:%s"),
(LPCTSTR)e.Description(),
(LPCTSTR)e.ErrorMessage());
AfxMessageBox(errMessage);
}
4.access数据库操作
定义
_variant_t aacs;
CString acs;
操作
//--------插入数据
acs="INSERT INTO 表1(ID,name,age) VALUES (10,'liala',25)";
pConn->Execute((_bstr_t)acs, &aacs, adCmdUnknown);
AfxMessageBox("数据插入成功");
//--------删除数据
acs="delete from 表1 where ID=10";
pConn->Execute((_bstr_t)acs, &aacs, adCmdUnknown);
AfxMessageBox("数据删除成功");
//--------更新数据
acs="update 表1 set name='李四' where ID=3";
pConn->Execute((_bstr_t)acs, &aacs, adCmdUnknown);
AfxMessageBox("数据更新成功");
//--------建表
acs="create table 表3(age SmallInt,hight Float,birthday DateTime)";
pConn->Execute((_bstr_t)acs, &aacs, adCmdUnknown);
AfxMessageBox("表3建立成功");
//--------表加字段
acs="alter table 表3 add column name1 Text";
pConn->Execute((_bstr_t)acs, &aacs, adCmdUnknown);
AfxMessageBox("表3加字段成功");
//--------表删字段
acs="alter table 表3 drop column name1 ";
pConn->Execute((_bstr_t)acs, &aacs, adCmdUnknown);
AfxMessageBox("表3删除字段成功");
//--------删表
acs="drop table 表3";
pConn->Execute((_bstr_t)acs, &aacs, adCmdUnknown);
AfxMessageBox("表3删除成功");