📄 upgrade.pas.svn-base
字号:
unit Upgrade;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, ComCtrls, DB, ADODB;
type
TUpgradeForm = class(TForm)
pnlStep1: TPanel;
lblSystem: TLabel;
lblDatabase: TLabel;
sbSystem: TSpeedButton;
sbDatabase: TSpeedButton;
edtSystem: TEdit;
edtDatabase: TEdit;
cbBackup: TCheckBox;
pnlStep2: TPanel;
lblProcess1: TLabel;
ProgressBar1: TProgressBar;
lblProcess2: TLabel;
ProgressBar2: TProgressBar;
bbtnOk: TBitBtn;
bbtnExit: TBitBtn;
OpenDialog1: TOpenDialog;
DDMADOC: TADOConnection;
qryDDM: TADOQuery;
qryDatabase: TADOQuery;
qryQuery: TADOQuery;
SYSADOC: TADOConnection;
adsDatabase: TADODataSet;
edtBackup: TEdit;
ACCTADOC: TADOConnection;
sbBackup: TSpeedButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure bbtnOkClick(Sender: TObject);
procedure bbtnExitClick(Sender: TObject);
procedure sbSystemClick(Sender: TObject);
procedure sbDatabaseClick(Sender: TObject);
procedure cbBackupClick(Sender: TObject);
procedure sbBackupClick(Sender: TObject);
private
procedure SetInterface;
{ Private declarations }
public
{ Public declarations }
end;
var
UpgradeForm: TUpgradeForm;
implementation
uses CommFun;
{$R *.dfm}
procedure TUpgradeForm.SetInterface;
var
APath:String;
begin
Caption:='数据库升级';
pnlStep1.Visible:=True;
pnlStep2.Visible:=False;
APath:=ExtractFilePath(Application.ExeName);
if APath[Length(APath)]<>'\' then APath:=APath+'\';
edtDatabase.Text:=APath+'HwERP.ddm';
APath:=ReadReg('\Software\HwERP5','InstallPath',1,'');
if APath='' then
begin
APath:=ExtractFilePath(Application.ExeName);
ForceDirectories(APath);
ChDir('..\');
APath:=GetCurrentDir;
end;
if APath[Length(APath)]<>'\' then APath:=APath+'\';
edtSystem.Text:=APath+'HwSYS.hws';
end;
procedure TUpgradeForm.FormCreate(Sender: TObject);
begin
SetInterface;
end;
procedure TUpgradeForm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
//
end;
procedure TUpgradeForm.bbtnOkClick(Sender: TObject);
var
APath,AOldVer,ANewVer,AHost,AFile,AUser,APass,S,AFromVer,AToVer:String;
AType,I:Integer;
AStringList,ASource,ADest:TStringList;
begin
//确定(&O)
if not FileExists(edtSystem.Text) then
begin
ShowMsg('无效的系统数据库文件,请重新输入',1); //
edtSystem.SetFocus;
Abort;
end;
if not FileExists(edtDatabase.Text) then
begin
ShowMsg('无效的数据库设计文件,请重新输入',1); //
edtDatabase.SetFocus;
Abort;
end;
if (cbBackup.Checked) and (not DirectoryExists(edtBackup.Text)) then
begin
ShowMsg('无效的备份路径,请重新输入',1); //
edtBackup.SetFocus;
Abort;
end;
pnlStep1.Visible:=False;
pnlStep2.Visible:=True;
bbtnOk.Enabled:=False;
lblProcess1.Caption:='升级中...';
lblProcess2.Caption:='升级中...';
Update;
ProgressBar1.Position:=0;
ProgressBar1.Max:=100;
ProgressBar2.Position:=0;
ProgressBar2.Max:=4;
try
//1.备份所有程序,包含数据库 backup
APath:=ExtractFilePath(edtSystem.Text);
if APath[Length(APath)]<>'\' then APath:=APath+'\';
if cbBackup.Checked then
begin
ProgressBar2.Max:=ProgressBar2.Max+1;
lblProcess1.Caption:='备份文件...';
lblProcess2.Caption:='备份文件...';
Update;
Application.ProcessMessages;
ASource:=TStringList.Create;
ADest:=TStringList.Create;
//取得所有待拷贝的文件
GetCopyFiles(APath,edtBackup.Text,ASource,ADest);
ProgressBar1.Position:=ProgressBar1.Max;
ProgressBar1.Max:=ASource.Count-1;
for I:=0 to ASource.Count-1 do
begin
Application.ProcessMessages;
ProgressBar1.Position:=ProgressBar1.Position+1;
lblProcess1.Caption:='备份文件...['+ASource[I]+']';
Update;
if not CopyFile(PChar(ASource[I]),PChar(ADest[I]),False) then
begin
ShowMsg('备份文件失败',1); //
Abort;
end;
end;
ASource.Free;
ADest.Free;
ProgressBar1.Position:=ProgressBar1.Max;
ProgressBar1.Position:=0;
ProgressBar2.Position:=ProgressBar2.Position+1;
end;
//2.生成升级脚本
lblProcess1.Caption:='生成升级脚本...';
lblProcess2.Caption:='生成升级脚本...';
Update;
ConnectDB(DDMADOC,0,'.',edtDatabase.Text,'Admin','HowWell19740507');
//取得原有文件的版本信息
AOldVer:=ReadIniFile(APath+'HwSYS.ini','General','Version',1);
//取得新文件的版本信息
ANewVer:=ReadIniFile(ExtractFilePath(Application.ExeName)+'HwSYS.ini','General','Version',1);
if AOldVer=ANewVer then
begin
ShowMsg('已经是最新版本,不用再升级',1);
Abort;
end;
//取得DDM中的升级脚本,并执行
S:='';
qryDDM.Close;
qryDDM.SQL.Clear;
qryDDM.SQL.Add('select * from HwVersion where FVersion>'+''''+AOldVer+''''+' and FVersion<='+''''+ANewVer+'''');
qryDDM.Open;
ProgressBar1.Max:=qryDDM.RecordCount;
if ProgressBar1.Max=0 then ProgressBar1.Max:=100;
qryDDM.First;
while not qryDDM.Eof do
begin
Application.ProcessMessages;
ProgressBar1.Position:=ProgressBar1.Position+1;
S:=S+qryDDM.FieldByName('FScript').Value+#13;
qryDDM.Next;
end;
qryDDM.Close;
AStringList:=TStringList.Create;
AStringList.Text:=S;
DDMADOC.Connected:=False;
ProgressBar1.Position:=ProgressBar1.Max;
ProgressBar1.Position:=0;
ProgressBar2.Position:=ProgressBar2.Position+1;
//3.取得旧程序的HwSYS.hws中的HwDatabase表的所有数据
lblProcess1.Caption:='升级系统数据库...';
lblProcess2.Caption:='升级系统数据库...';
Update;
ConnectDB(SYSADOC,0,'.',edtSystem.Text,'Admin','HowWell19740507');
adsDatabase.Close;
adsDatabase.CreateDataSet;
qryDatabase.Close;
qryDatabase.Open;
//取得现有帐套信息,并保存在adsDatabase
ProgressBar1.Max:=qryDatabase.RecordCount;
if ProgressBar1.Max=0 then ProgressBar1.Max:=100;
qryDatabase.First;
while not qryDatabase.Eof do
begin
Application.ProcessMessages;
ProgressBar1.Position:=ProgressBar1.Position+1;
adsDatabase.Append;
adsDatabase.FieldByName('FName').Value:=qryDatabase.FieldByName('FName').Value; //帐套名称
adsDatabase.FieldByName('FCompany').Value:=qryDatabase.FieldByName('FCompany').Value; //公司名称
adsDatabase.FieldByName('FDate').Value:=qryDatabase.FieldByName('FDate').Value; //建帐日期
adsDatabase.FieldByName('FPath').Value:=qryDatabase.FieldByName('FPath').Value; //帐套路径(帐套所在的路径及文件名)
adsDatabase.FieldByName('FType').Value:=qryDatabase.FieldByName('FType').Value; //帐套类型(0=Access, 1=SQL Server)
adsDatabase.FieldByName('FHost').Value:=qryDatabase.FieldByName('FHost').Value; //服务器名(数据库所在的主机名称)
adsDatabase.FieldByName('FUser').Value:=qryDatabase.FieldByName('FUser').Value; //用户名称(连接数据库的用户名称)
adsDatabase.FieldByName('FPass').Value:=qryDatabase.FieldByName('FPass').Value; //用户密码(连接数据库的用户密码)
adsDatabase.FieldByName('FVersion').Value:=qryDatabase.FieldByName('FVersion').Value; //版本路径
adsDatabase.Post;
qryDatabase.Next;
end;
qryDatabase.Close;
SYSADOC.Connected:=False;
ProgressBar1.Position:=ProgressBar1.Max;
ProgressBar1.Position:=0;
ProgressBar2.Position:=ProgressBar2.Position+1;
//4.复制所有的程序
lblProcess1.Caption:='升级程序文件...';
lblProcess2.Caption:='升级程序文件...';
ASource:=TStringList.Create;
ADest:=TStringList.Create;
Update;
//取得所有待拷贝的文件
GetCopyFiles(ExtractFilePath(Application.ExeName),APath,ASource,ADest);
ProgressBar1.Position:=ProgressBar1.Max;
ProgressBar1.Max:=ASource.Count-1;
for I:=0 to ASource.Count-1 do
begin
Application.ProcessMessages;
ProgressBar1.Position:=ProgressBar1.Position+1;
if ExtractFileName(ASource[I])<>'HwSYS.ini' then
begin
lblProcess1.Caption:='升级程序文件...['+ASource[I]+']';
Update;
if not CopyFile(PChar(ASource[I]),PChar(ADest[I]),False) then
begin
ShowMsg('升级程序文件失败',1); //
Abort;
end;
end else
begin
AFromVer:=ASource[I];
AToVer:=ADest[I];
end;
end;
ASource.Free;
ADest.Free;
ProgressBar1.Position:=ProgressBar1.Max;
ConnectDB(SYSADOC,0,'.',edtSystem.Text,'Admin','HowWell19740507');
qryDatabase.Close;
qryDatabase.Open;
ProgressBar1.Position:=0;
ProgressBar2.Position:=ProgressBar2.Position+1;
//5.循环系统数据库并升级所有帐套数据库
lblProcess1.Caption:='升级帐套数据库...';
lblProcess2.Caption:='升级帐套数据库...';
Update;
ProgressBar1.Max:=adsDatabase.RecordCount;
if ProgressBar1.Max=0 then ProgressBar1.Max:=100;
adsDatabase.First;
while not adsDatabase.Eof do
begin
ProgressBar1.Position:=ProgressBar1.Position+1;
//5A.插入到新的系统数据库文件中
AType:=adsDatabase.FieldByName('FType').AsInteger;
AHost:=adsDatabase.FieldByName('FHost').AsString;
AFile:=adsDatabase.FieldByName('FPath').AsString;
AUser:=adsDatabase.FieldByName('FUser').AsString;
APass:=adsDatabase.FieldByName('FPass').AsString;
if copy(AFile,1,2)='.\' then AFile:=APath+copy(AFile,3,Length(AFile));
qryDatabase.Append;
qryDatabase.FieldByName('FName').Value:=adsDatabase.FieldByName('FName').Value; //帐套名称
qryDatabase.FieldByName('FCompany').Value:=adsDatabase.FieldByName('FCompany').Value; //公司名称
qryDatabase.FieldByName('FDate').Value:=adsDatabase.FieldByName('FDate').Value; //建帐日期
qryDatabase.FieldByName('FPath').Value:=AFile; //帐套路径(帐套所在的路径及文件名)
qryDatabase.FieldByName('FType').Value:=AType; //帐套类型(0=Access, 1=SQL Server)
qryDatabase.FieldByName('FHost').Value:=AHost; //服务器名(数据库所在的主机名称)
qryDatabase.FieldByName('FUser').Value:=AUser; //用户名称(连接数据库的用户名称)
qryDatabase.FieldByName('FPass').Value:=APass; //用户密码(连接数据库的用户密码)
qryDatabase.FieldByName('FVersion').Value:=adsDatabase.FieldByName('FVersion').Value; //版本路径
qryDatabase.Post;
//5B.执行升级脚本,更新所有帐套数据库
if AType=0 then APass:='HowWell19740507';
ConnectDB(ACCTADOC,AType,AHost,AFile,AUser,APass);
ExecScript(AStringList,qryQuery,ProgressBar1,lblProcess1);
adsDatabase.Next;
end;
qryDatabase.Close;
SYSADOC.Connected:=False;
qryQuery.Close;
ACCTADOC.Connected:=False;
qryDDM.Close;
DDMADOC.Connected:=False;
AStringList.Free;
ProgressBar1.Position:=ProgressBar1.Max;
ProgressBar2.Position:=ProgressBar2.Position+1;
CopyFile(PChar(AFromVer),PChar(AToVer),False);
ShowMsg('升级成功!',1); //
ModalResult:=1;
finally
pnlStep1.Visible:=True;
pnlStep2.Visible:=False;
bbtnOk.Enabled:=True;
end;
Close;
end;
procedure TUpgradeForm.bbtnExitClick(Sender: TObject);
begin
//退出(&X)
Close;
end;
procedure TUpgradeForm.sbSystemClick(Sender: TObject);
begin
//系统数据库文件
OpenDialog1.DefaultExt:='hws';
OpenDialog1.Filter:='系统数据库文件(*.hws)|*.hws';
OpenDialog1.Title:='系统数据库文件';
OpenDialog1.InitialDir:=ExtractFilePath(Application.ExeName);
OpenDialog1.FileName:=edtSystem.Text;
if OpenDialog1.Execute then
begin
edtSystem.Text:=OpenDialog1.FileName;
end;
end;
procedure TUpgradeForm.sbDatabaseClick(Sender: TObject);
begin
//数据库设计文件
OpenDialog1.DefaultExt:='ddm';
OpenDialog1.Filter:='数据库设计文件(*.ddm)|*.ddm';
OpenDialog1.Title:='数据库设计文件';
OpenDialog1.InitialDir:=ExtractFilePath(Application.ExeName);
OpenDialog1.FileName:=edtDatabase.Text;
if OpenDialog1.Execute then
begin
edtDatabase.Text:=OpenDialog1.FileName;
end;
end;
procedure TUpgradeForm.cbBackupClick(Sender: TObject);
begin
//升级备份
edtBackup.Enabled:=cbBackup.Checked;
sbBackup.Enabled:=cbBackup.Checked;
if edtBackup.Enabled then edtBackup.Color:=clWindow else edtBackup.Color:=clBtnFace;
end;
procedure TUpgradeForm.sbBackupClick(Sender: TObject);
var
APath:string;
begin
APath:=BrowseDialog('选择路径',0);
if APath<>'' then
begin
if APath[Length(APath)]<>'\' then APath:=APath+'\';
edtBackup.Text:=APath;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -