DataRestore.cpp
上传用户:latoyin
上传日期:2017-10-19
资源大小:2882k
文件大小:6k
源码类别:

数据库系统

开发平台:

Visual C++

  1. // DataRestore.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Equipment.h"
  5. #include "DataRestore.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CDataRestore dialog
  13. extern CEquipmentApp theApp;
  14. CDataRestore::CDataRestore(CWnd* pParent /*=NULL*/)
  15. : CDialog(CDataRestore::IDD, pParent)
  16. {
  17. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINTITLE);
  18. backfilename = "";
  19. }
  20. void CDataRestore::DoDataExchange(CDataExchange* pDX)
  21. {
  22. CDialog::DoDataExchange(pDX);
  23. //{{AFX_DATA_MAP(CDataRestore)
  24. DDX_Control(pDX, IDC_LIST1, list);
  25. DDX_Control(pDX, IDC_EDIT1, datafile);
  26. //}}AFX_DATA_MAP
  27. }
  28. BEGIN_MESSAGE_MAP(CDataRestore, CDialog)
  29. //{{AFX_MSG_MAP(CDataRestore)
  30. ON_WM_CANCELMODE()
  31. ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  32. ON_COMMAND(ID_BUTTONREFRESH, OnButtonrefresh)
  33. ON_COMMAND(ID_BUTTONCLOSE, OnButtonclose)
  34. //}}AFX_MSG_MAP
  35. END_MESSAGE_MAP()
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CDataRestore message handlers
  38. void CDataRestore::OnCancel() 
  39. {
  40. // TODO: Add extra cleanup here
  41. CDialog::OnCancel();
  42. }
  43. void CDataRestore::OnOK() 
  44. {
  45. }
  46. BOOL CDataRestore::OnInitDialog() 
  47. {
  48. CDialog::OnInitDialog();
  49. SetIcon(m_hIcon,true);
  50. toolbar.Create(TBSTYLE_FLAT|CCS_TOP|WS_CHILD|WS_VISIBLE|WS_BORDER|CCS_ADJUSTABLE|TBSTYLE_WRAPABLE,CRect(0,0,0,0),this,IDR_TOOLBAR2);
  51. toolbar.SetBitmapSize(CSize(32,32));
  52. imagelist.Create(32,32,ILC_COLOR32|ILC_MASK,0,0);
  53. for (int n =0;n<5;n++)
  54. {
  55. imagelist.Add(theApp.LoadIcon(n+IDI_ICON1));
  56. }
  57. toolbar.SetImageList(&imagelist);
  58. TBBUTTON  buttons[3];
  59. for (int i =0; i<3;i++)
  60. {
  61. CString str;
  62. int strlength;
  63. CCHAR *temp;
  64. if (i<1)
  65. buttons[i].fsStyle =TBSTYLE_SEP;
  66. else
  67. {
  68. buttons[i].fsStyle =TBSTYLE_BUTTON;
  69. }
  70. buttons[i].fsState =TBSTATE_ENABLED;
  71. buttons[i].dwData =0;
  72. if (i == 1) 
  73. {
  74. buttons[i].idCommand = ID_BUTTONADD+3; //对应于刷新
  75. buttons[i].iBitmap = 3;
  76. str.LoadString(IDS_Restore);
  77. }
  78. else
  79. {
  80. buttons[i].idCommand = ID_BUTTONADD+4;//退出
  81. buttons[i].iBitmap = 4;
  82. str.LoadString(ID_BUTTONADD+4);
  83. }
  84. strlength = str.GetLength()+1;
  85. temp = str.GetBufferSetLength(strlength);
  86. temp[strlength]= '';
  87. temp[strlength-1]= '';
  88. buttons[i].iString = toolbar.AddStrings(temp);
  89. str.ReleaseBuffer();
  90. }
  91. toolbar.AutoSize();
  92. toolbar.AddButtons(3,buttons);
  93. toolbar.ShowWindow(SW_SHOW);
  94. list.SetExtendedStyle(LVS_REPORT|LVS_EX_FULLROWSELECT|LVS_OWNERDRAWFIXED |LVS_EX_FLATSB|LVS_SHOWSELALWAYS|LVS_EX_HEADERDRAGDROP);
  95. list.InsertColumn(100,"位置");
  96. list.InsertColumn(100,"备份时间");
  97. list.SetColumnWidth(0,100);
  98. list.SetColumnWidth(1,245);
  99. return TRUE;  // return TRUE unless you set the focus to a control
  100.               // EXCEPTION: OCX Property Pages should return FALSE
  101. }
  102. void CDataRestore::OnCancelMode() 
  103. {
  104. CDialog::OnCancelMode();
  105. // TODO: Add your message handler code here
  106. }
  107. void CDataRestore::OnButton1() 
  108. {
  109. CFileDialog* filedialog;
  110. filedialog = new CFileDialog(true,"bak");
  111. list.DeleteAllItems();
  112. if (filedialog->DoModal()==IDOK )
  113. {
  114. CString str,sql;
  115. str = filedialog->GetPathName();
  116. datafile.SetWindowText(str);
  117. //str = ExpandFileName(str);
  118. backfilename = str;
  119. sql.Format("restore headeronly from disk = '%s'",str);
  120. theApp.datamanage->Record1->raw_Close();
  121. try
  122. {
  123. theApp.datamanage->Record1->Open((_bstr_t)sql,theApp.datamanage->GetConn().GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
  124. }
  125. catch(...)
  126. {
  127. MessageBox("指定的文件不是合法的备份文件.","提示",64);
  128. datafile.SetWindowText("");
  129. backfilename = "";
  130. return;
  131. }
  132. int row = 0;
  133. while(! theApp.datamanage->Record1->ADOEOF)
  134. {
  135. list.InsertItem(row,"");
  136. str = (TCHAR *)(_bstr_t)theApp.datamanage->Record1->GetFields()->GetItem("Position")->Value;
  137. list.SetItemText(row,0,str);
  138. str = (TCHAR *)(_bstr_t)theApp.datamanage->Record1->GetFields()->GetItem("backupfinishdate")->Value;
  139. list.SetItemText(row,1,str);
  140. row += 1;
  141. theApp.datamanage->Record1->MoveNext();
  142. }
  143. }
  144. delete (filedialog);
  145. }
  146. CString CDataRestore::ExpandFileName(CString filename)
  147. {
  148. int pos;
  149. pos = filename.Find(".",0);
  150. if (pos != -1)
  151. {
  152. return filename.Left(pos);
  153. }
  154. return filename;
  155. }
  156. void CDataRestore::OnButtonrefresh() 
  157. {
  158. int row = list.GetSelectionMark();
  159. if (row != -1)
  160. {
  161. CString temp,sql;
  162. temp = (TCHAR*)(_bstr_t)theApp.datamanage->GetConn()->ConnectionString;
  163. try
  164. {
  165. theApp.datamanage->GetConn()->Close();
  166. theApp.datamanage->ConnectDatabase("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=master;Data Source=.");
  167. sql = "select spid from master..sysprocesses where dbid=db_id('EquipmentManage')";
  168. _RecordsetPtr recordset;
  169. recordset = theApp.datamanage->Record1;
  170. recordset->Open((_bstr_t)sql,theApp.datamanage->GetConn().GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
  171. while (!recordset->ADOEOF)
  172. {
  173. temp = (TCHAR *)(_bstr_t)recordset->GetFields()->GetItem("spid")->Value;
  174. sql.Format("kill %i",atoi(temp));
  175. theApp.datamanage->ExecSQL(sql);
  176. recordset->MoveNext();
  177. }
  178. sql.Format("  restore database equipmentmanage from disk ='%s' with file = %i",backfilename,atoi(list.GetItemText(row,0)));
  179. theApp.datamanage->ExecSQL(sql);
  180. MessageBox("还原成功.","提示",64);
  181. }
  182. catch(...)
  183. {
  184. MessageBox("还原失败.","提示",64);
  185. }
  186. theApp.datamanage->GetConn()->Close();
  187. theApp.datamanage->ConnectDatabase("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Equipmentmanage;Data Source=.");
  188. }
  189. else
  190. {
  191. MessageBox("请在列表中选择备份集.","提示",64);
  192. }
  193. }
  194. void CDataRestore::OnButtonclose() 
  195. {
  196. EndDialog(0);
  197. }