此处代码只是测试代码,仅仅是测试
//环境:D7+SQL Server 2008
1 unit Unit1; 2 3 interface 4 5 uses 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, StdCtrls, ADODB, DB, FolderDialog; 8 9 type 10 TForm1 = class(TForm) 11 Button1: TButton; 12 OpenDialog1: TOpenDialog; 13 SaveDialog1: TSaveDialog; 14 Button2: TButton; 15 Edit1: TEdit; 16 ADOQuery1: TADOQuery; 17 ADOConnection1: TADOConnection; 18 ADOStoredProc1: TADOStoredProc; 19 edtDBPlacePosition: TEdit; 20 btnDBRestore: TButton; 21 FolderDialog1: TFolderDialog; 22 labDBPlacePosition: TLabel; 23 labDBBackSource: TLabel; 24 btnDBBackSource: TButton; 25 edtDBBackSource: TEdit; 26 btnDBPlacePosition: TButton; 27 Edit2: TEdit; 28 Memo1: TMemo; 29 Button3: TButton; 30 procedure Button1Click(Sender: TObject); 31 procedure Button2Click(Sender: TObject); 32 procedure btnDBRestoreClick(Sender: TObject); 33 procedure btnDBBackSourceClick(Sender: TObject); 34 procedure btnDBPlacePositionClick(Sender: TObject); 35 procedure Button3Click(Sender: TObject); 36 private 37 procedure DropDB(vDB:string); 38 procedure CreateDB(vDB:string); 39 procedure RestoreFailDeal(vDB:string); 40 { Private declarations } 41 public 42 { Public declarations } 43 end; 44 45 var 46 Form1: TForm1; 47 48 implementation 49 50 const 51 uConStr:string='Provider=SQLOLEDB.1;Password=sa123456;Persist Security Info=True;User ID=sa;Initial Catalog=MyDB;Data Source=192.168.1.50\SQLExpress'; 52 uConStrMaster:string='Provider=SQLOLEDB.1;Password=sa123456;Persist Security Info=True;User ID=sa;Initial Catalog=master;Data Source=192.168.1.50\SQLExpress'; 53 54 55 //CntSQLStr = 'Provider=SQLOLEDB.1;Data Source=%s;User ID=%s;Password=%s;Persist Security Info=True;Initial Catalog=%s'; 56 //CntSQLStrWin='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Data Source=%s;Initial Catalog=%s'; 57 {$R *.dfm} 58 procedure TForm1.DropDB(vDB: string); 59 var 60 SQLStr:string; 61 begin 62 {SQLStr:='IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'''+ 63 vDB+''')'+ #13#10+ 64 ' DROP DATABASE ['+vDB+']'; } 65 SQLStr:=Format('IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N''%s'') '+#13#10 66 +' begin '+#13#10 67 +' ALTER DATABASE %s SET SINGLE_USER WITH ROLLBACK IMMEDIATE ;'+#13#10 68 +' DROP DATABASE %s ;'+#13#10 69 //+' ALTER DATABASE %S SET MULTI_USER '+#13#10 70 +' end ;',[vDB,'['+vDB+']','['+vDB+']']); 71 ADOQuery1.SQL.Text:=SQLStr; 72 ADOQuery1.ExecSQL; 73 end; 74 75 procedure TForm1.Button1Click(Sender: TObject); 76 var 77 vDataBackStr:String; 78 vPath:string; 79 80 SQLStr:String; 81 begin 82 83 vDataBackStr:='MyDB_'+FormatDateTime('yyyymmddhhmmss',Now)+'.bak'; 84 vPath:=Edit1.Text+'\'+vDataBackStr; 85 SaveDialog1.FileName:=vPath; 86 if SaveDialog1.Execute then 87 begin 88 // 89 end 90 else 91 Exit; 92 ADOConnection1.Connected:=False; 93 ADOConnection1.ConnectionString:=uConStr; 94 ADOConnection1.Connected:=True; 95 vPath:=SaveDialog1.FileName; 96 SQLStr:='BACKUP DATABASE "' + 'MyDB' + '" TO DISK = ''' + vPath + ''''; 97 ADOQuery1.SQL.Text:=SQLStr; 98 ADOQuery1.ExecSQL; 99 ShowMessage('备份成功!'); 100 end; 101 102 procedure TForm1.Button2Click(Sender: TObject); 103 var 104 DBFile,CntStr,SQLStr,DataFile,LogFile,vDBName:string; 105 begin 106 {DBFile:=Edit2.Text; 107 ADOConnection1.Connected:=False; 108 ADOConnection1.ConnectionString:=uConStrMaster; 109 ADOConnection1.Connected:=True; 110 //新建一个空数据库 111 vDBName:='MyDB'; 112 DropSourceDB(vDBName); 113 SQLStr:='Create database ['+vDBName+']'; 114 115 ADOQuery1.SQL.Text:=SQLStr; 116 ADOQuery1.ExecSQL; 117 sleep(1000); 118 119 //还原数据库到一个新建的数据库中 120 SQLStr:='RESTORE DATABASE ['+vDBName+'] FROM DISK = N'''+DBFile 121 +''' WITH FILE = 1, NOUNLOAD , STATS = 10, RECOVERY , REPLACE , '+ 122 ' MOVE N'''+vDBName+'_Data'' TO N'''+edit1.text+'\'+vDBName+'.mdf'+''','+ 123 ' MOVE N'''+vDBName+'_Log'' TO N'''+edit1.text+'\'+vDBName+'_log.ldf'+''''; 124 try 125 ADOQuery1.SQL.Text:=SQLStr; 126 ADOQuery1.ExecSQL; 127 except 128 DropSourceDB(vDBName); 129 end; } 130 end; 131 132 procedure TForm1.btnDBRestoreClick(Sender: TObject); 133 var 134 DBFile,CntStr,SQLStr,DataFile,LogFile,vDBName:string; 135 begin 136 DBFile:=edtDBBackSource.Text; 137 if Length(edtDBBackSource.Text)<2 then 138 begin 139 ShowMessage('数据库备份文件的文件错误!'); 140 exit; 141 end 142 else if Length(edtDBPlacePosition.Text)<2 then 143 begin 144 ShowMessage('数据库放置的位置路径错误!'); 145 exit; 146 end; 147 if Application.MessageBox(Pchar('确定恢复(还原)数据库吗?'+#13#10'此动作一旦执行就无法撤回'+#13#10'请慎重操作!'), 148 Pchar(Application.Title), MB_OKCANCEL + MB_ICONQUESTION) = mrOk then 149 begin 150 151 end 152 else 153 Exit; 154 155 ADOConnection1.Connected:=False; 156 ADOConnection1.ConnectionString:=uConStrMaster; 157 ADOConnection1.Connected:=True; 158 //新建一个空数据库 159 vDBName:='MyDB'; 160 CreateDB(vDBName); 161 //SQLStr:='Create database ['+vDBName+']'; 162 163 //ADOQuery1.SQL.Text:=SQLStr; 164 //ADOQuery1.ExecSQL; 165 //sleep(1000); 166 167 //还原数据库到一个新建的数据库中 168 SQLStr:='RESTORE DATABASE ['+vDBName+'] FROM DISK = N'''+DBFile 169 +''' WITH FILE = 1, NOUNLOAD , STATS = 10, RECOVERY , REPLACE , '+ 170 ' MOVE N'''+vDBName+''' TO N'''+edtDBPlacePosition.Text+'\'+vDBName+'.mdf'+''','+ 171 ' MOVE N'''+vDBName+'_Log'' TO N'''+edtDBPlacePosition.Text+'\'+vDBName+'_log.ldf'+''''; 172 try 173 ADOQuery1.SQL.Text:=SQLStr; 174 Memo1.Text:=ADOQuery1.SQL.Text; 175 ADOQuery1.ExecSQL; 176 ShowMessage('恢复成功!'); 177 except 178 RestoreFailDeal(vDBName); 179 ShowMessage('还原失败!'); 180 end; 181 182 end; 183 184 procedure TForm1.btnDBBackSourceClick(Sender: TObject); 185 begin 186 if SaveDialog1.Execute then 187 begin 188 edtDBBackSource.Text:=SaveDialog1.FileName; 189 end 190 end; 191 192 procedure TForm1.btnDBPlacePositionClick(Sender: TObject); 193 begin 194 FolderDialog1.Directory:=ExtractFilePath(Application.ExeName); 195 if FolderDialog1.Execute then 196 begin 197 edtDBPlacePosition.Text:=FolderDialog1.Directory 198 end 199 end; 200 201 procedure TForm1.CreateDB(vDB: string); 202 var 203 SQLStr:string; 204 begin 205 SQLStr:=Format('IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N''%s'') '+#13#10 206 +' begin '+#13#10 207 +' ALTER DATABASE %s SET SINGLE_USER WITH ROLLBACK IMMEDIATE ;'+#13#10 208 +' DROP DATABASE %s ;'+#13#10 209 +' end ;'+#13#10 210 +' Create database %s ;'+#13#10 211 ,[vDB,'['+vDB+']','['+vDB+']','['+vDB+']']); 212 213 ADOQuery1.SQL.Text:=SQLStr; 214 ADOQuery1.ExecSQL; 215 end; 216 217 procedure TForm1.RestoreFailDeal(vDB: string); 218 var 219 SQLStr:string; 220 begin 221 SQLStr:=Format('IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N''%s'') '+#13#10 222 +' begin '+#13#10 223 +' ALTER DATABASE %s SET MULTI_USER ;'+#13#10 224 +' end ;',[vDB,'['+vDB+']']); 225 ADOQuery1.SQL.Text:=SQLStr; 226 ADOQuery1.ExecSQL; 227 end; 228 229 procedure TForm1.Button3Click(Sender: TObject); 230 begin 231 232 ShowMessage(ExtractFileDir(ParamStr(0))); 233 end; 234 235 end.
1 object Form1: TForm1 2 Left = 588 3 Top = 390 4 Width = 755 5 Height = 413 6 Caption = 'Form1' 7 Color = clBtnFace 8 Font.Charset = DEFAULT_CHARSET 9 Font.Color = clWindowText 10 Font.Height = -11 11 Font.Name = 'MS Sans Serif' 12 Font.Style = [] 13 OldCreateOrder = False 14 PixelsPerInch = 96 15 TextHeight = 13 16 object labDBPlacePosition: TLabel 17 Left = 240 18 Top = 115 19 Width = 139 20 Height = 13 21 Alignment = taRightJustify 22 Caption = '数据库放置的位置:' 23 end 24 object labDBBackSource: TLabel 25 Left = 251 26 Top = 80 27 Width = 128 28 Height = 13 29 Alignment = taRightJustify 30 Caption = '数据库备份文件:' 31 end 32 object Button1: TButton 33 Left = 24 34 Top = 128 35 Width = 75 36 Height = 25 37 Caption = '备份' 38 TabOrder = 0 39 OnClick = Button1Click 40 end 41 object Button2: TButton 42 Left = 96 43 Top = 8 44 Width = 75 45 Height = 25 46 Caption = '恢复' 47 TabOrder = 1 48 OnClick = Button2Click 49 end 50 object Edit1: TEdit 51 Left = 8 52 Top = 96 53 Width = 217 54 Height = 21 55 ImeName = '中文(简体) - 搜狗拼音输入法' 56 TabOrder = 2 57 Text = 'D:\program files\TXHR_BACK' 58 end 59 object edtDBPlacePosition: TEdit 60 Left = 384 61 Top = 112 62 Width = 249 63 Height = 21 64 ImeName = '中文(简体) - 搜狗拼音输入法' 65 TabOrder = 3 66 end 67 object btnDBRestore: TButton 68 Left = 480 69 Top = 141 70 Width = 81 71 Height = 25 72 Caption = '还原(恢复)' 73 TabOrder = 4 74 OnClick = btnDBRestoreClick 75 end 76 object btnDBBackSource: TButton 77 Left = 632 78 Top = 77 79 Width = 25 80 Height = 25 81 Caption = '...' 82 TabOrder = 5 83 OnClick = btnDBBackSourceClick 84 end 85 object edtDBBackSource: TEdit 86 Left = 384 87 Top = 80 88 Width = 249 89 Height = 21 90 ImeName = '中文(简体) - 搜狗拼音输入法' 91 TabOrder = 6 92 end 93 object btnDBPlacePosition: TButton 94 Left = 632 95 Top = 112 96 Width = 25 97 Height = 25 98 Caption = '...' 99 TabOrder = 7 100 OnClick = btnDBPlacePositionClick 101 end 102 object Edit2: TEdit 103 Left = 8 104 Top = 72 105 Width = 217 106 Height = 21 107 ImeName = '中文(简体) - 搜狗拼音输入法' 108 TabOrder = 8 109 Text = 'Edit2' 110 end 111 object Memo1: TMemo 112 Left = 264 113 Top = 176 114 Width = 425 115 Height = 185 116 ImeName = '中文(简体) - 搜狗拼音输入法' 117 Lines.Strings = ( 118 'Memo1') 119 ReadOnly = True 120 ScrollBars = ssBoth 121 TabOrder = 9 122 end 123 object Button3: TButton 124 Left = 80 125 Top = 256 126 Width = 75 127 Height = 25 128 Caption = 'Button3' 129 TabOrder = 10 130 OnClick = Button3Click 131 end 132 object OpenDialog1: TOpenDialog 133 Filter = 'MS SQL Server (*.bak)|*.bak' 134 Left = 664 135 Top = 64 136 end 137 object SaveDialog1: TSaveDialog 138 Filter = 'MS SQL Server (*.bak)|*.bak' 139 Left = 48 140 Top = 32 141 end 142 object ADOQuery1: TADOQuery 143 Connection = ADOConnection1 144 Parameters = <> 145 Left = 128 146 Top = 144 147 end 148 object ADOConnection1: TADOConnection 149 ConnectionString = 150 'Provider=SQLOLEDB.1;Password=sa123456;Persist Security Info=True' + 151 ';User ID=sa;Initial Catalog=TXHR;Data Source=192.168.1.50\SQLExp' + 152 'ress' 153 Provider = 'SQLOLEDB.1' 154 Left = 168 155 Top = 144 156 end 157 object ADOStoredProc1: TADOStoredProc 158 Parameters = <> 159 Left = 200 160 Top = 128 161 end 162 object FolderDialog1: TFolderDialog 163 DialogX = 0 164 DialogY = 0 165 Version = '1.1.0.1' 166 Left = 664 167 Top = 112 168 end 169 end