📄 quanshu.cpp
字号:
// Quanshu.cpp : implementation file
//
#include "stdafx.h"
#include "yhgl.h"
#include "Quanshu.h"
#include "Quanxian.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CYhglApp theApp;
CString quantemp=_T("");
/////////////////////////////////////////////////////////////////////////////
// CQuanshu dialog
CQuanshu::CQuanshu(CWnd* pParent /*=NULL*/)
: CDialog(CQuanshu::IDD, pParent)
{
//{{AFX_DATA_INIT(CQuanshu)
m_qid = 0;
m_Shudai = _T("");
m_Dshudai = _T("");
m_Dquanxian = _T("");
//}}AFX_DATA_INIT
flag=0;
}
void CQuanshu::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CQuanshu)
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Text(pDX, IDC_EDITQID, m_qid);
DDX_Text(pDX, IDC_EDITADD, m_Shudai);
DDX_Text(pDX, IDC_EDITNOT, m_Dshudai);
DDX_Text(pDX, IDC_EDITYES, m_Dquanxian);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CQuanshu, CDialog)
//{{AFX_MSG_MAP(CQuanshu)
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CQuanshu message handlers
void CQuanshu::OnOK()
{
m_pRecordset->Close();
CDialog::OnOK();
}
BOOL CQuanshu::OnInitDialog()
{
CDialog::OnInitDialog();
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open("SELECT * FROM Privilege",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
}
catch(_com_error e)///捕捉异常
{
AfxMessageBox("读取数据库失败!");///显示错误信息
}
OnReadAccess();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CQuanshu::OnRadio1()
{
flag=1;
a="1";
}
void CQuanshu::OnRadio2()
{
flag=1;
a="2";
}
void CQuanshu::OnRadio3()
{
flag=2;
}
void CQuanshu::OnButton1()
{
quantemp=m_Dquanxian;
UpdateData();
if(strcmp(m_Dquanxian,quantemp)!=0)
{
flag=3;
}
if(flag==1)
{
UpdateData();
if(m_Shudai == "")
{
AfxMessageBox("数据集代码不能为空!");
}
try
{
// 写入各字段值
m_pRecordset->AddNew();
m_pRecordset->PutCollect("RoleGroupID", (long)m_qid);
m_pRecordset->PutCollect("DSCODE", _variant_t(m_Shudai));
m_pRecordset->PutCollect("Privilege", _variant_t(a));
m_pRecordset->Update();
AfxMessageBox("插入成功!");
// 更新显示其库内容
int nCurSel = m_list.GetCurSel();
OnReadAccess();
m_list.SetCurSel(nCurSel);
// 移动记录指针到新的位置
OnSelchangeList1();
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
if(flag==2)
{
if(m_list.GetCount() == 0)
return;
else if(m_list.GetCurSel() < 0 || m_list.GetCurSel() > m_list.GetCount())
m_list.SetCurSel(0);
try
{
// 删除当前行记录
m_pRecordset->Delete(adAffectCurrent);
m_pRecordset->Update();
// 删除列表中当前值
int nCurSel = m_list.GetCurSel();
m_list.DeleteString(nCurSel);
if(nCurSel == 0 && (m_list.GetCount() != 0))
m_list.SetCurSel(nCurSel);
else if(m_list.GetCount() != 0)
m_list.SetCurSel(nCurSel-1);
// 移动记录指针到新的位置
OnSelchangeList1();
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
if(flag==3)
{
UpdateData(); // 更新对话框数据
if(m_list.GetCurSel() < 0 || m_list.GetCurSel() > m_list.GetCount())
m_list.SetCurSel(0);
// 修改当前记录的字段值
try
{
m_pRecordset->PutCollect("RoleGroupID", (long)m_qid);
m_pRecordset->PutCollect("DSCODE", _variant_t(m_Dshudai));
m_pRecordset->PutCollect("Privilege", _variant_t(m_Dquanxian));
m_pRecordset->Update();
// 重新读入库记录更新显示
int nCurSel = m_list.GetCurSel();
OnReadAccess();
m_list.SetCurSel(nCurSel);
// 移动记录指针到新的位置
OnSelchangeList1();
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
if(flag==0)
{
AfxMessageBox("请选择操作的类型或是修改权限设置!");
}
}
//当每次选择新的记录时,都会移动指针到新的记录位置并将值显示出来
void CQuanshu::OnSelchangeList1()
{
int curSel = m_list.GetCurSel();
_variant_t var;
int i=0;
int id=0;
if(curSel < 0)
return;
try
{
// 先将指针移向第一条记录,然后就可以相对第一条记录来随意移动记录指针
m_pRecordset->MoveFirst();
while(i<curSel)
{
var = m_pRecordset->GetCollect("RoleGroupID");
if(var.vt != VT_NULL)
id = var.iVal;
if(id==m_qid)
i++;
m_pRecordset->MoveNext();
}
while(!m_pRecordset->adoEOF)
{
var = m_pRecordset->GetCollect("RoleGroupID");
if(var.vt != VT_NULL)
id = var.iVal;
if(id==m_qid)
{
var = m_pRecordset->GetCollect("DSCODE");
if(var.vt != VT_NULL)
m_Dshudai = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Privilege");
if(var.vt != VT_NULL)
m_Dquanxian = (LPCSTR)_bstr_t(var);
else m_Dquanxian=_T("");
break;
}
m_pRecordset->MoveNext();
}
UpdateData(false);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
void CQuanshu::OnReadAccess()
{
_variant_t var;
int qid=0;
CString dscode,pre;
// 清空列表框
m_list.ResetContent();
dscode=pre="";
try
{
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();
else
{
AfxMessageBox("表内数据为空");
}
// 读入库中各字段并加入列表框中
while(!m_pRecordset->adoEOF)
{
var = m_pRecordset->GetCollect("RoleGroupID");
if(var.vt != VT_NULL)
qid = var.iVal;
var = m_pRecordset->GetCollect("DSCODE");
if(var.vt != VT_NULL)
dscode = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Privilege");
if(var.vt != VT_NULL)
pre = (LPCSTR)_bstr_t(var);
else
pre=_T("");
if(qid==m_qid)
{
if(strcmp(pre,"1")==0)
{
m_list.AddString(" "+dscode + " -------> "+"可读");
}
else if(strcmp(pre,_T(""))==0)
{
m_list.AddString(" "+dscode + " -------> "+"没有设置权限");
}
else
m_list.AddString(" "+dscode + " -------> "+"可读写");
}
m_pRecordset->MoveNext();
}
// 默认列表指向第一项,同时移动记录指针并显示
m_list.SetCurSel(0);
OnSelchangeList1();
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -