?? 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 + -