FastReportToDataBase
在网上找了很久,没有找到合适的代码,最后自己干脆做了一下
代码共享如下,大家研究一下,谁有更好的,能否给小弟一份,或是有什么指点的,
烦请给我发邮件,zjw_0916@126.com
------------创建储存数据表----------------
Create Table Table1
(
A001 INT IDENTITY(1,1) Primary Key,
A002 varchar(50) NOT NULL,---报表名称
A003 Image NOT NULL, --报表数据
A004 bit -- 默认打印
)
GO
Alter Table Table1 WITH NOCHECK
ADD CONSTRAINT UX_A002 UNIQUE NONCLUSTERED(A002)
Alter Table Table1 WITH NoCHECK
ADD CONSTRAINT DF_A004 DEFAULT 0 For A004
Create Trigger upd_Table1
ON Table1 For UPDATE
AS
begin
Declare @Flags bit,@ID int
IF UPDATE(A004)
begin
Select @Flags=A004,@ID=A001 From Inserted
IF @Flags=1
begin
UpDate Table1 Set A004=0
Update Table1 Set A004=1 where A001=@ID
end
end
end
//-------------报表存储相关函数-----------------
FFileName1,FFileName: string; //临时路径
ReportNo: Integer; //报表
procedure GetTempFileName; //获取临时路径
procedure LoadReport(ReportNo: Integer); //加载报表
procedure SetTemplet(Flags: Boolean); //设置模板
procedure SavetoDateBase(SaveAs: Boolean;
ReportNo: Integer); //保存模板到数据库
function LoadTemplet: Integer; //加载模板
//--------------报表存储相关函数----------------
procedure TFR_MakePool1.GetTempFileName;
var
Path: array[0..MAX_PATH - 1] of char;
begin
GetTempPath(MAX_PATH, Path);
FFileName := string(Path);
FFileName1 := FFileName;
FFileName:= FFileName + IntToStr(GetTickCount) + '.fr3';
end;
procedure TFR_MakePool1.LoadReport(ReportNo: Integer);
begin
if ReportNo <> 0 then
begin
ExecSql(FC_DM.qry_Com,'SELECT * FROM Table1 WHERE A001='+IntToStr(ReportNo));
if not FC_DM.qry_Com.IsEmpty then
begin
TBlobField(FC_DM.qry_Com.FieldByName('A003')).SaveToFile(FFileName1+FC_DM.qry_Com.FieldByName('A002').AsString+
'.fr3');
frxReport1.LoadFromFile(FFileName1+FC_DM.qry_Com.FieldByName('A002').AsString+ '.fr3');
end;
end;
end;
function TFR_MakePool1.LoadTemplet: Integer;
begin
Result := 0;
ExecSql(FC_DM.qry_Com,'SELECT Top 1 * FROM Table1 where A004 = 1');
if not FC_DM.qry_Com.IsEmpty then
Result := FC_DM.qry_Com.Fields[0].AsInteger
else
begin
ExecSql(FC_DM.qry_Com,'SELECT Top 1 * FROM Table1');
if not FC_DM.qry_Com.IsEmpty then
Result := FC_DM.qry_Com.Fields[0].AsInteger;
end;
end;
procedure TFR_MakePool1.SavetoDateBase(SaveAs: Boolean;
ReportNo: Integer);
var
SaveDialog : TSaveDialog;
UpDateFlags: Boolean;
begin
UpDateFlags := False;
frxReport1.SaveToFile(FFileName);
ExecSql(FC_DM.qry_Com1,' Select Top 1 * From Table1 Where A001='''+IntToStr(ReportNo)+'''');
if FC_DM.qry_Com1.IsEmpty or SaveAs then
try
SaveDialog := TSaveDialog.Create(nil);
SaveDialog.DefaultExt := 'fr3';
SaveDialog.Filter := frxResources.Get('dsRepFilter');
if frxCompressorClass <> nil then
SaveDialog.Filter := SaveDialog.Filter + '|' + frxResources.Get('dsComprRepFilter');
if SaveDialog.Execute then
begin
FC_DM.qry_Com1.Append;
FC_DM.qry_Com1.FieldByName('A002').AsString:=GetSingeName(SaveDialog.FileName);
UpDateFlags := True;
end;
finally
FreeAndNil(SaveDialog);
end
else
begin
FC_DM.qry_Com1.Edit;
UpDateFlags := True;
//FC_DM.qry_Com1.FieldByName('A002').AsString:=FileName;
end;
if UpDateFlags then
begin
TBlobField(FC_DM.qry_Com1.FieldByName('A003')).LoadFromFile(FFileName);
FC_DM.qry_Com1.Post;
ShowMsg(self, GetVariantCaption('gc_Prompt', '提示'), GetVariantCaption('gc_SaveSuccess', '保存成功!'), MB_OK,
MB_ICONASTERISK);
end;
end;
procedure TFR_MakePool1.SetTemplet(Flags: Boolean);
var
S : String;
begin
if Flags then
begin
S := Trim(dxLoadFromTemp.Text);
if S='' then
ReportNo:=0
else
ReportNo:=StrToInt(S);
end else
begin
ReportNo := LoadTemplet;
end;
LoadReport(ReportNo);
end;
--------使用方法-----------
function TFR_MakePool1.frxDesigner1SaveReport(Report: TfrxReport;
SaveAs: Boolean): Boolean;
begin
inherited;
Result :=false;
SavetoDateBase(SaveAs,ReportNo);
Result:=True;
if FileExists(FFileName) then
DeleteFile(FFileName);
end;