2,497
社区成员
发帖
与我相关
我的任务
分享
with TFrm_BackDB.Create(nil) do
ShowModal;
调用后 每次在执行 ExecSQL后都不对 并报错 对象名‘users’无效
unit UFrm_BackDB;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TFrm_BackDB = class(TForm)
edtPath: TEdit;
Label1: TLabel;
btnBrowse: TButton;
btnStart: TButton;
Button3: TButton;
dlgSaveDB: TSaveDialog;
procedure btnBrowseClick(Sender: TObject);
procedure btnStartClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Frm_BackDB: TFrm_BackDB;
implementation
uses UConfig, UMainDM;
{$R *.dfm}
procedure TFrm_BackDB.btnBrowseClick(Sender: TObject);
var
fName: string;
begin
fName := '智能照明控制系统_' + FormatDateTime('yymmddhhmmss', Now);
dlgSaveDB.FileName := fName;
dlgSaveDB.Filter := '所有文件(*.*)|*.*|数据库文件(*.bak)|*.bak|数据库文件(*.trn)|*.trn';
if dlgSaveDB.Execute then
begin
if ExtractFileExt(dlgSaveDB.FileName) <> '.bak' then
dlgSaveDB.FileName := ChangeFileExt(dlgSaveDB.FileName, '.bak');
edtPath.Text := dlgSaveDB.FileName;
end;
end;
procedure TFrm_BackDB.btnStartClick(Sender: TObject);
var
SQLText: string;
begin
try
MainDM.conMain.Connected := False; // 断开连接
// 切换数据库
MainDM.conMain.ConnectionString :=
'Provider=SQLOLEDB.1;Persist Security Info=False;' +
'User ID=sa;Password=1;' + 'Initial Catalog = master' + ';Data Source=' +
TConfiger.GetKeyValue('ServerConfig', 'ServerName')+
';Use Procedure for Prepare=1;Auto Translate=True;' +
'Packet Size=4096; Workstation ID=' + TConfiger.GetKeyValue
('ServerConfig', 'ServerName') +
';Use Encryption for Data=False;' +
'Tag with column collation when possible=False ';
MainDM.conMain.Connected := True; // 连接数据库
with MainDM.qryPrivate do { 执行备份 }
begin
Close;
SQLText := Format('Backup DataBase %s to disk = %s',
[TConfiger.GetKeyValue('ServerConfig', 'DBName'),
Quotedstr(edtPath.Text)]);
SQL.Add(SQLText);
try
ExecSQL;
Application.MessageBox('备份数据库成功!', '系统提示', MB_OK + MB_ICONINFORMATION);
except
Application.MessageBox('备份数据库失败,请联系管理员!', '系统错误', MB_OK + MB_ICONERROR);
end;
end;
finally
// 切换回主数据库
MainDM.conMain.Connected := False;
MainDM.conMain.ConnectionString :=
'Provider=SQLOLEDB.1;Persist Security Info=False;' +
'User ID=sa;Password=1;' + 'Initial Catalog =' + TConfiger.GetKeyValue
('ServerConfig', 'DBName') + ';Data Source=' + TConfiger.GetKeyValue
('ServerConfig', 'ServerName');
MainDM.conMain.Connected := True;
end;
end;
end.
procedure TFrm_BackDB.btnStartClick(Sender: TObject);
begin
if edtPath.Text = '' then
begin
Application.MessageBox('请按浏览按钮选择保存的路径!', '系统提示',
MB_OK + MB_ICONINFORMATION);
Exit;
end;
pbEnd.Position := 0; { 进度条 }
pbEnd.Visible := True;
btnStart.Enabled := False;
pbEnd.StepIt;
try
MainDM.conMain.Close; {关闭主连接}
pbEnd.StepIt;
cmdBackDB.Connection := MainDM.conMain;
cmdBackDB.CommandText := 'Use Master'; {连接master数据库}
cmdBackDB.Execute;
pbEnd.StepIt;
{备份数据库}
cmdBackDB.CommandText := 'Backup DataBase ' + TConfiger.GetKeyValue
('ServerConfig', 'DBName') + ' to disk = ' + Quotedstr(edtPath.Text)
+ ' with init';
cmdBackDB.Execute;
pbEnd.StepIt;
{重连主数据库}
cmdBackDB.CommandText := 'Use ' + TConfiger.GetKeyValue('ServerConfig',
'DBName');
cmdBackDB.Execute;
pbEnd.StepIt;
Application.MessageBox('备份数据库成功!', '系统提示', MB_OK + MB_ICONINFORMATION);
except
Application.MessageBox('备份数据库失败!', '系统提示', MB_OK + MB_ICONINFORMATION);
end;
try
MainDM.conMain.Connected := True;
except
Application.MessageBox('无法连接到数据库!', '系统提示', MB_OK + MB_ICONINFORMATION);
Application.Terminate;
end;
pbEnd.Visible := False;
btnStart.Enabled := True;
end;
procedure TFrm_RestoreDB.btnRestoreDBClick(Sender: TObject);
var
SQLText: string;
begin
if edtPath.Text = '' then
begin
Application.MessageBox('请按浏览选择要恢复的数据库文件!', '系统提示',
MB_OK + MB_ICONINFORMATION);
Exit;
end;
if Application.MessageBox(PChar('确定要恢复数据库么?这将覆盖原有数据,建议先备份再执行此操作!'), '系统提示',
MB_OKCANCEL + MB_ICONINFORMATION) = mrOk then
begin
pbEnd.Position := 0; { 进度条 }
pbEnd.Visible := True;
btnRestoreDB.Enabled := False;
pbEnd.StepIt;
try
MainDM.conMain.Close; {关闭主连接}
pbEnd.StepIt;
cmdRestoreDB.Connection := MainDM.conMain;
cmdRestoreDB.CommandText := 'Use Master'; {连接到master数据库}
cmdRestoreDB.Execute;
pbEnd.StepIt;
{ 获取排他访问权 }
SQLText := Format(
'select spid from master.dbo.sysprocesses where dbid = DB_ID(%s)',
[QuotedStr(TConfiger.GetKeyValue('ServerConfig', 'DBName'))]);
with MainDM.FindRecords(SQLText) do
try
First;
while not Eof do
begin
cmdRestoreDB.CommandText := 'Kill ' + FieldByName('spid').AsString;
cmdRestoreDB.Execute;
Next;
end;
except
end;
pbEnd.StepIt;
{恢复数据库}
cmdRestoreDB.CommandText := 'Restore DataBase ' + TConfiger.GetKeyValue
('ServerConfig', 'DBName') + ' from disk = ' + QuotedStr(edtPath.Text)
+ ' with replace';
cmdRestoreDB.Execute;
pbEnd.StepIt;
{重连主数据库}
cmdRestoreDB.CommandText := 'Use ' + TConfiger.GetKeyValue
('ServerConfig', 'DBName');
cmdRestoreDB.Execute;
Application.MessageBox('恢复数据库成功!', '系统提示', MB_OK + MB_ICONINFORMATION);
except
Application.MessageBox('恢复数据库失败,请检查文件是否正确!', '系统提示',
MB_OK + MB_ICONINFORMATION);
end;
try
MainDM.conMain.Connected := True;
except
Application.MessageBox('无法连接到数据库!', '系统提示', MB_OK + MB_ICONINFORMATION);
Application.Terminate;
end;
pbEnd.Visible := False;
btnRestoreDB.Enabled := True;
end;
end;
MainDM.conMain.Connected := True; // 连接数据库
with MainDM.qryPrivate do { 执行备份 }
begin
Close;
SQL.Clear; //增加这句
SQLText := Format('Backup DataBase %s to disk = %s',
[TConfiger.GetKeyValue('ServerConfig', 'DBName'),
Quotedstr(edtPath.Text)]);
SQL.Add(SQLText);
try
ExecSQL;
Application.MessageBox('备份数据库成功!', '系统提示', MB_OK + MB_ICONINFORMATION);
except
Application.MessageBox('备份数据库失败,请联系管理员!', '系统错误', MB_OK + MB_ICONERROR);
end;
end;
qryBackDB.Connection := MainDM.conMain;
with qryBackDB do { 执行备份 }
begin
Close;
SQLText := Format('Backup DataBase %s to disk = %s',
[TConfiger.GetKeyValue('ServerConfig', 'DBName'),
Quotedstr(edtPath.Text)]);
SQL.Add(SQLText);
try
ExecSQL;
Application.MessageBox('备份数据库成功!', '系统提示', MB_OK + MB_ICONINFORMATION);
except
Application.MessageBox('备份数据库失败,请联系管理员!', '系统错误', MB_OK + MB_ICONERROR);
end;
end;