熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> SQL Server >> 正文

VC在SQL Server7中動態增加/刪除用戶

2013-11-15 14:33:32  來源: SQL Server 

  本文主要通過調用SQL Server的系統存儲過程和系統表在程序中動態增加和刪除用戶
  
  一首先用系統管理員帳號登錄系統程序省略請看源程序
   
  二 進入用戶管理界面
  
   讀取數據庫從mastersysdatabse中讀取所有數據庫
  
  void CUser::AddDataBase()
  {
  _RecordsetPtr rs;
  _bstr_t bt;
  HRESULT hr;
  CString str;
  
  bt=(_bstr_t)select * from mastersysdatabases;
  try{
  hr=rsCreateInstance (__uuidof(Recordset));
  ASSERT(SUCCEEDED(hr));
  
  //打開數據源
  hr=rs>Open (btcnGetInterfacePtr ()adOpenDynamicadLockOptimisticadCmdText);
  ASSERT(SUCCEEDED(hr));
  rs>MoveFirst();
  while(!rs>EndOfFile)
  {
  str=(char*)(_bstr_t)rs>Fields>GetItem(name)>Value;
  m_databaseAddString(str);
  rs>MoveNext();
  }
  rs>Close();
  m_databaseSetCurSel();
  }
  catch(_com_error)
  {}
  }
  
   讀取用戶從mastersyslogins視圖中讀取所有非NT用戶void CUser::AddUser()
  {
  _RecordsetPtr rs;
  _bstr_t bt;
  HRESULT hr;
  CString str;
  bt=(_bstr_t)select * from mastersyslogins where isntname=;
  try{
  hr=rsCreateInstance (__uuidof(Recordset));
  ASSERT(SUCCEEDED(hr));
  //打開數據源
  hr=rs>Open (btcnGetInterfacePtr ()adOpenDynamicadLockOptimisticadCmdText);
  ASSERT(SUCCEEDED(hr));
  rs>MoveFirst();
  while(!rs>EndOfFile)
  {
  str=(char*)(_bstr_t)rs>Fields>GetItem(name)>Value;
  m_user_listAddString(str);
  rs>MoveNext();
  }
  rs>Close();
  m_user_listSetCurSel ();
  }
  catch(_com_error)
  {}
  }
  
   讀取當前用戶有權限的數據庫每個數據庫有權限的用戶在每個數據的sysusers表中void CUser::OnSelchangeUserList()
  {
  CString strUserstrDB;
  m_db_listResetContent ();
  m_user_listGetText (m_user_listGetCurSel ()strUser);
  for(int i=;i<m_databaseGetCount ();i++)
  {
  m_databaseGetLBText (istrDB);
  if(HasRight(strUserstrDB))
  {
  m_db_listAddString (strDB);
  }
  }
  if(m_db_listGetCount ()>)
  {
  m_db_listSetCurSel ();
  }
  }
  //查看用戶user有沒有數據庫database的權限
  BOOL CUser::HasRight(CString user CString database)
  {
  _RecordsetPtr rs;
  _bstr_t bt;
  HRESULT hr;
  BOOL bResult=FALSE;
  
  bt=(_bstr_t)select * from +(_bstr_t)database+(_bstr_t)sysusers where name=+(_bstr_t)user+(_bstr_t);
  try{
  hr=rsCreateInstance (__uuidof(Recordset));
  ASSERT(SUCCEEDED(hr));
  //打開數據源
  hr=rs>Open (btcnGetInterfacePtr ()adOpenDynamicadLockOptimisticadCmdText);
  ASSERT(SUCCEEDED(hr));
  
  rs>MoveFirst();
  rs>Close();
  bResult=TRUE;
  }
  catch(_com_error)
  {
  }
  return bResult;
  }
  
   增加用戶及授予權限增加用戶調用存儲過程sp_addlogin授予權限調用存儲過程sp_grantdbaccess
  基本用法為:sp_addlogin usernamepassword sp_grantdbaccess usernamevoid CUser::OnOK()
  {
  // TODO: Add extra validation here
  UpdateData();
  
  CString strDB;
  m_databaseGetLBText (m_databaseGetCurSel()strDB);
  if(m_userIsEmpty () || m_user==sa)
  return;
  try{
  if(m_user_listFindString (m_user)<)
  {
  cn>Execute (sp_addlogin +(_bstr_t)m_user++(_bstr_t)m_user+NULLadExecuteNoRecords);
  m_user_listAddString (m_user);
  }
  else
  {
  cn>PutDefaultDatabase ((_bstr_t)strDB);
  cn>Execute (sp_grantdbaccess +(_bstr_t)m_user+(_bstr_t)NULLadExecuteNoRecords);
  m_db_listAddString (strDB);
  }
  }
  catch(_com_error)
  {
  AfxMessageBox(發生錯誤!);
  }
  //CDialog::OnOK();
  }
  
   刪除用戶及權限: 刪除用戶調用存儲過程sp_droplogin授予權限調用存儲過程sp_revokedbaccess
  基本用法為:sp_droplogin username
  sp_revokedbaccess usernamevoid CUser::OnBnClickedDel()
  {
  UpdateData();
  m_userTrimRight ();
  if(m_userIsEmpty () || m_user==sa || m_user_listFindString (m_user)<)
  {
  AfxMessageBox(不能刪除!);
  return;
  }
  
  CString strDB;
  m_databaseGetLBText (m_databaseGetCurSel ()strDB);
  try{
  if(m_db_listGetCount ()<)
  {//刪除用戶
  cn>Execute (exec sp_droplogin +(_bstr_t)m_user+NULLadExecuteNoRecords);
  m_user_listDeleteString (m_user_listFindString (m_user));
  
  }
  else
  {//刪除權限
  if(m_db_listFindString (strDB)>=)
  {
  cn>PutDefaultDatabase ((_bstr_t)strDB);
  cn>Execute (exec sp_revokedbaccess +(_bstr_t)m_user+NULLadExecuteNoRecords);
  m_db_listDeleteString (m_db_listFindString (strDB));
  }
  }
  }
  catch(_com_error)
  {
  AfxMessageBox(發生錯誤!);
  }
  }
From:http://tw.wingwit.com/Article/program/SQLServer/201311/21990.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.