// ReaderDlg.cpp : implementation file // #include "stdafx.h" #include "test.h" #include "ReaderDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CReaderDlg dialog CReaderDlg::CReaderDlg(CWnd* pParent /*=NULL*/) : CDialog(CReaderDlg::IDD, pParent) { //{{AFX_DATA_INIT(CReaderDlg) m_id = _T(""); m_msg = _T(""); m_name = _T(""); m_num = _T(""); m_password = _T(""); //}}AFX_DATA_INIT } void CReaderDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CReaderDlg) DDX_Control(pDX, IDC_COMBO2, m_idorname); DDX_Control(pDX, IDC_COMBO1, m_which); DDX_Text(pDX, IDC_ID, m_id); DDX_Text(pDX, IDC_MSG, m_msg); DDX_Text(pDX, IDC_NAME, m_name); DDX_Text(pDX, IDC_NUM, m_num); DDX_Text(pDX, IDC_PASSWORD, m_password); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CReaderDlg, CDialog) //{{AFX_MSG_MAP(CReaderDlg) ON_BN_CLICKED(IDC_ADD, OnAdd) ON_BN_CLICKED(IDC_SAVE1, OnSave) ON_BN_CLICKED(IDC_QRY, OnQry) ON_BN_CLICKED(IDC_EDIT1, OnEdit) ON_BN_CLICKED(IDC_DEL1, OnDel) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CReaderDlg message handlers void CReaderDlg::OnAdd() { // TODO: Add your control notification handler code here //清空编辑框内容 m_id=""; m_name=""; m_password=""; m_num="5"; m_which.SetCurSel(-1); UpdateData(false); //编辑框可用 GetDlgItem(IDC_ID)->EnableWindow(TRUE); GetDlgItem(IDC_NAME)->EnableWindow(TRUE); GetDlgItem(IDC_PASSWORD)->EnableWindow(TRUE); GetDlgItem(IDC_NUM)->EnableWindow(TRUE); GetDlgItem(IDC_COMBO1)->EnableWindow(TRUE); } void CReaderDlg::OnSave() { // TODO: Add your control notification handler code here GetDlgItem(IDC_DEL1)->EnableWindow(TRUE); //"删除"按钮可用 UpdateData(true); //数据更新 CString combo; int a=m_which.GetCurSel(); if(a!=-1) m_which.GetLBText(a,combo); else combo=""; if(combo==""||m_id==""||m_name==""||m_password==""||m_num=="") //输入内容不全面 { AfxMessageBox("必须填入全部信息"); return; } int num=atoi(m_num); //可借册数字符串转化为整数 if(num==0) //可借册数为0 { AfxMessageBox("可借册数为大于0的整数"); return; } CoInitialize(NULL); _ConnectionPtr pconnect(__uuidof(Connection)); _RecordsetPtr record(__uuidof(Recordset)); _CommandPtr comd(__uuidof(Command)); pconnect->ConnectionString="Provider=MSDASQL.1;Persist Security Info=False;User ID=Administrator;Data Source=student;Initial Catalog=Library"; pconnect->Open("","","",NULL); comd->put_ActiveConnection(_variant_t((IDispatch *)pconnect)); //查看数据库中是否已有ID为m_id的读者,如果有,那么操作失败 //标准SQL语句 CString cmd="select * from READER where ID='"; cmd+=m_id; cmd+="'"; comd->CommandText=_bstr_t(cmd); //得到SQL语句 record=comd->Execute(NULL,NULL,adCmdText); //执行SQL语句 if(!record->end) { AfxMessageBox("数据库已有此ID信息,重新输入ID"); //输入的ID已经存在 return; } cmd="select * from READER where NAME='"; cmd+=m_name; cmd+="'"; comd->CommandText=_bstr_t(cmd); //得到SQL语句 record=comd->Execute(NULL,NULL,adCmdText); //执行SQL语句 if(!record->end) { AfxMessageBox("数据库已有此姓名的信息,重新选择用户名"); //输入的读者姓名已经存在 return; } cmd="INSERT INTO READER VALUES('"; cmd+=m_id;cmd+="','"; cmd+=m_name;cmd+="','"; cmd+=m_password;cmd+="','"; cmd+=combo;cmd+="',"; cmd+=m_num; cmd+=")"; AfxMessageBox(cmd); comd->CommandText=_bstr_t(cmd); //得到SQL语句 record=comd->Execute(NULL,NULL,adCmdText); //插入记录 AfxMessageBox("保存到数据库成功"); //弹出消息框 pconnect->Close(); comd.Release(); record.Release(); pconnect.Release(); CoUninitialize(); //更新按钮状态 GetDlgItem(IDC_ID)->EnableWindow(FALSE); GetDlgItem(IDC_NAME)->EnableWindow(FALSE); GetDlgItem(IDC_PASSWORD)->EnableWindow(FALSE); GetDlgItem(IDC_NUM)->EnableWindow(FALSE); GetDlgItem(IDC_COMBO1)->EnableWindow(FALSE); } void CReaderDlg::OnQry() { // TODO: Add your control notification handler code here UpdateData(true); CString combo; int a=m_idorname.GetCurSel(); if(a!=-1) m_idorname.GetLBText(a,combo); //查询项 else combo=""; if(combo==""||m_msg=="") { AfxMessageBox("必须选择项目并输入查询信息"); //查询项或查询关键字为空 return; } CoInitialize(NULL); _ConnectionPtr pconnect(__uuidof(Connection)); _CommandPtr comd(__uuidof(Command)); _RecordsetPtr record(__uuidof(Recordset)); pconnect->ConnectionString="Provider=MSDASQL.1;Persist Security Info=False;User ID=Administrator;Data Source=student;Initial Catalog=Library"; pconnect->Open("","","",NULL); comd->put_ActiveConnection(_variant_t((IDispatch *)pconnect)); CString cmd; //标准SQL语句 if(combo=="ID号") cmd="select * from READER where ID='"; if(combo=="姓名") cmd="select * from READER where NAME='"; cmd+=m_msg; cmd+="'"; //执行查询语句 comd->CommandText=_bstr_t(cmd); //得到SQL语句 record=comd->Execute(NULL,NULL,adCmdText); //进行查询 if(!record->end) { m_id=(char*)(_bstr_t)record->GetCollect("ID"); //得到记录的ID项 m_name=(char*)(_bstr_t)record->GetCollect("NAME"); //得到记录的NAME项 m_password=(char*)(_bstr_t)record->GetCollect("PASSWORD"); //得到记录的PASSWORD项 CString str="管理员"; CString restr; restr=(char*)(_bstr_t)record->GetCollect("WHICH"); //得到登录者的权限 int length=str.GetLength(); if(restr.Left(length)==str) //登录者为管理员 m_which.SetCurSel(0); //列表框显示 else m_which.SetCurSel(1); UpdateData(false); m_num=(char*)(_bstr_t)record->GetCollect("NUM"); UpdateData(false); //更新数据 } else { //没有记录则显示空 m_id=""; m_name=""; m_password=""; m_num=""; m_which.SetCurSel(-1); UpdateData(false); //更新显示 } if(record->State) record->Close(); if(pconnect->State) pconnect->Close(); comd.Release(); record.Release(); pconnect.Release(); CoUninitialize(); //编辑框不可用 GetDlgItem(IDC_ID)->EnableWindow(FALSE); GetDlgItem(IDC_NAME)->EnableWindow(FALSE); GetDlgItem(IDC_PASSWORD)->EnableWindow(FALSE); GetDlgItem(IDC_NUM)->EnableWindow(FALSE); GetDlgItem(IDC_COMBO1)->EnableWindow(FALSE); } void CReaderDlg::OnEdit() { // TODO: Add your control notification handler code here OnDel(); GetDlgItem(IDC_ID)->EnableWindow(TRUE); GetDlgItem(IDC_NAME)->EnableWindow(TRUE); GetDlgItem(IDC_PASSWORD)->EnableWindow(TRUE); GetDlgItem(IDC_NUM)->EnableWindow(TRUE); GetDlgItem(IDC_COMBO1)->EnableWindow(TRUE); } void CReaderDlg::OnDel() { // TODO: Add your control notification handler code here UpdateData(true); CoInitialize(NULL); _ConnectionPtr pconnect(__uuidof(Connection)); _RecordsetPtr record(__uuidof(Recordset)); _CommandPtr comd(__uuidof(Command)); pconnect->ConnectionString="Provider=MSDASQL.1;Persist Security Info=False;User ID=Administrator;Data Source=student;Initial Catalog=Library"; pconnect->Open("","","",NULL); comd->put_ActiveConnection(_variant_t((IDispatch *)pconnect)); //标准SQL语句 CString cmd="delete from READER where ID='"; cmd+=m_id; cmd+="'"; comd->CommandText=_bstr_t(cmd); //得到SQL语句 record=comd->Execute(NULL,NULL,adCmdText); //执行SQL语句,删除记录 pconnect->Close(); comd.Release(); record.Release(); pconnect.Release(); CoUninitialize(); }