设为首页 加入收藏

TOP

3.8.6 数据库还原模块
2013-10-07 14:28:50 来源: 作者: 【 】 浏览:67
Tags:3.8.6 数据库 还原 模块

3.8.6  数据库还原模块

数据库还原模块的功能是对系统数据库完成还原处理。数据库备份和还原是一对孪生兄弟,当数据库备份后,需要还原操作才能够继续在项目中使用。具体实现流程如下。

(1) 创建一个对话框资源IDD_RESTORE_DLG,添加的控件如图3-17所示。

 
图3-17  还原数据库设计界面
(2) 双击对话框,创建一个新类CBackupDlg,对应代码如下:
  1. class CRestoreDlg : public CDialog  
  2. {  
  3. public:  
  4.     CRestoreDlg(CWnd* pParent = NULL);  

(3) 给"还原"按钮添加消息响应函数,该函数的功能是实现对指定数据库的数据还原处理。具体实现代码如下:

  1. void CRestoreDlg::OnRestore()   
  2. {  
  3.     UpdateData();                   //数据的更新  
  4.     if(m_strPath == "")  
  5.     {  
  6.         MessageBox("请选择路径!");  
  7.         return ;  
  8.     }  
  9.     _ConnectionPtr pConnection;  
  10.     try  
  11.     {  
  12.         HRESULT hr = pConnection.CreateInstance("ADODB.Connection");  
  13.         //创建Connection对象  
  14.         pConnection->Open("driver={SQL Server}
    ;
    Server=.;DATABASE=master;UID=sa;PWD=","","",adModeUnknown);  
  15.         CString sql;//定义字符串变量  
  16.         sql.Format("use master restore database 
    HMSys from 
    disk = '%s' with move 'HMSys_data' to 
    'c:\\HMSys.mdf', move 'HMSys_log' to 'c:\\HMSys.ldf'", m_strPath);  
  17.         pConnection->Execute((_bstr_t)sql, NULL, adCmdText);  
  18.         MessageBox("还原成功!");  
  19.         pConnection->Close();//关闭连接  
  20.     }  
  21.     CATCH_ERROR;  
  22. }  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++一个网络编程实例 下一篇3.8.5 数据库备份模块

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: