此处代码只是测试代码,仅仅是测试

//环境: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

Delphi数据库备份的更多相关文章

  1. backup1:开始数据库备份

    数据库备份分为数据文件备份和日志文件备份,数据文件的备份分为:完整备份和差异备份.在SQL Server 2012中,能够将数据分布式备份到不同的存储设备上,一般情况,只将数据备份到一个备份文件(.b ...

  2. 数据库日常维护-CheckList_02有关数据库备份检查

    数据库备份是DB日常运维中最基本的也是最重要的工作,很多情况下都是做成作业形式实现自动化周期性的做全备.差异以及日志备份.那么,如果作业出现问题没有完成工作,我们可以设置自动报警如email被动提醒我 ...

  3. [django]手动数据库备份

    基本原理是按钮点击后,系统查询出数据表中信息,然后在网页中导出相关表格! 若有自动数据库备份的兄弟,指点一下! 模板代码: <a href="{% url 'work_backup' ...

  4. Xtrabackup原理及使用innobackupex进行MySQL数据库备份恢复

    Xtrabackup是由percona提供的mysql数据库备份工具,据官方介绍,这也是世界上惟一一款开源的能够对innodb和xtradb数据库进行热备的工具. Xtrabackup中主要包含两个工 ...

  5. SQL Server数据库备份的镜像

    SQL Server数据库备份的镜像 一个完整备份可以分开镜像 USE master GO BACKUP DATABASE [testdatabase] TO DISK = N'C:\testdata ...

  6. SQL Server 数据库备份还原和数据恢复

      认识数据库备份和事务日志备份 数据库备份与日志备份是数据库维护的日常工作,备份的目的是在于当数据库出现故障或者遭到破坏时可以根据备份的数据库及事务日志文件还原到最近的时间点将损失降到最低点. 数据 ...

  7. Sql Server数据库备份和恢复:原理篇

    本文与您探讨为什么Sql Server有完整备份.差异备份和事务日志备份三种备份方式,以及为什么数据库又有简单模式.完整模式和大容量日志模式这三种恢复模式.本文内容适用于2005以上所有版本的Sql ...

  8. TODO:MongoDB MySQL数据库备份

    TODO:MongoDB MySQL数据库备份 1. MongoDB使用命令备份 mongodump进行整个数据库备份,主要用到的命令参数: -d 要备份的数据库 -o 输出的路径 ./mongodu ...

  9. SQL Server 维护计划实现数据库备份(策略实战)

    一.背景 之前写过一篇关于备份的文章:SQL Server 维护计划实现数据库备份,上面文章使用完整备份和差异备份基本上能解决数据库备份的问题,但是为了保障数据更加安全,我们需要再次完善我们的备份计划 ...

  10. Oracle数据库备份、恢复及常见问题

    通常我们都是使用PL/SQL Developer来对Oracle中的数据对象和数据进行管理.如果我们想方便快速还原或者部署Oracle到新机器上,怎么进行数据库的备份.导入恢复呢? 这里我们必须要考虑 ...

随机推荐

  1. UltiSnips安装及设置

    2022-10-05 10:56:50 星期三 安装了UltiSnips插件,然后开始学习 第一个命令 UltiSnipsEdit 不好使,创建了~/.vim/UltiSnips 还是 can not ...

  2. 微信h5支付报错 商家参数格式有误,请联系商家解决

    商家参数格式有误,请联系商家解决 当前调起H5支付的referer为空导致,一般是因为直接访问页面调起H5支付,请按正常流程进行页面跳转后发起支付,或自行抓包确认referer值是否为空 解决:就把 ...

  3. fftw安装

    1. 下载fftw 2.tar -zxvf fftw.tar.gz 3. ./configure --prefix=path --enable-sse2 --enable-avx --enable-f ...

  4. 在datagridview中首列添加复选框

    //为dgv增加复选框列 DataGridViewCheckBoxColumn checkbox = new DataGridViewCheckBoxColumn(); //列显示名称 checkbo ...

  5. 33. Java对异常处理的两种方式

    声明异常 throw: 关键字,用于抛出一个指定的异常对象 必须写在方法内部.必须是Exception或Exception的子类对象 throws: 用于方法声明上,表示当前方法不处理该异常,提醒调用 ...

  6. C#使用SharpZipLib解压多文件的zip压缩文件数据流,保存到本地

    代码: public async Task<ReturnModel<List<string>>> UploadModel() { var task = new Ta ...

  7. asp.net mvc api swagger 配置

    1.安装插件 Swashbuckle(版本5.6.0) 和 Swagger.Net.UI(版本1.1.0) 2.SwaggerNet类中注释掉一下代码: using System; using Sys ...

  8. 老生常谈:String s1 = new String("abc") 创建了几个字符串对象及8 种基本类型的包装类和常量池

    将创建 1 或 2 个字符串.如果池中已存在字符串常量"abc",则只会在堆空间创建一个字符串常量"abc".如果池中没有字符串常量"abc" ...

  9. kubernetes中 flannel网络组件

    Flannel 软件包地址:https://github.com/coreos/flannel Flannel是CoreOS开源的CNI网络插件,下图flannel官网提供的一个数据包经过封包.传输以 ...

  10. How to find WWN and WWPN of HBA card in Linux

    There are several ways to detect the WWN of a Fibre Channel (FC) HBA and their details in Linux/Unix ...