在Delphi
中使用RAVE报表(四)中讲解了和数据库连接的报表
有朋友提出了问题
所以在用一篇文章讲解使用Query动态查询和存储过程连接数据库的报表
因为要使用到存储过程我们使用SQL_Server数据库建立数据库Infotest建立数据表InfoTable 字段为: {[name][sex][age][province]} 添加数据
数据库的部分不进行过多地讲解在窗体上放置DatabaseQueryDataSourceRvQueryConnectionDBGrid组件连接到数据库{查询所有[陕西]的}按钮的事件为
procedure TFormButtonClick(Sender: TObject);
begin
QuerySQLClear ;
QuerySQLAdd(SELECT * FROM InfoTable WHERE (province =:pro) );
QueryParamByName(pro)AsString :=陕西;
QueryExecSQL ;
QueryActive :=True;
end;
运行点击后可以查看到DBGrid显示了查看的结果这样完成了第一步动态查询的过程
将程序运行[查询]然后打开Rave记住不要关掉查询的程序
[File]=〉New Data Object=〉Direct Data View=〉选择RvQueryConnection=〉 [Finish]=〉看到报表设计导航区的Data View Dictionary增加了DataView扩展后可以看到数据字段
选[Tools]=〉Report Wizards=〉Single Table=〉选DataView选择数据库字段=〉Report Title改为个人情况报表好了之后可以看到在page中生成了报表
然后保存*rav文件关闭程序添加[报表预览]按钮事件以及RvQueryConnection的GetCols和GetRow事件
procedure TFormButtonClick(Sender: TObject);
begin
With RvProjectProjMan do
begin
RvProjectOpen ;
RvQueryConnectionExecGetCols ; //得到列名
RvQueryConnectionExecGetRow ; //得到记录
RvProjectExecuteReport(Report);
Close ;
end;
end;
procedure TFormRvQueryConnectionGetCols(
Connection: TRvCustomConnection);
begin
ConnectionWriteField(namedtString);
ConnectionWriteField(sexdtString);
ConnectionWriteField(agedtInteger);
ConnectionWriteField(provincedtString);
end;
procedure TFormRvQueryConnectionGetRow(Connection: TRvCustomConnection);
begin
ConnectionWriteStrdata(DBGridFields[]value);
ConnectionWriteStrdata(DBGridFields[]value);
ConnectionWriteIntdata(DBGridFields[]value);
ConnectionWriteStrdata(DBGridFields[]value);
end;
在运行程序这样就完成了一个根据动态查询生成的报表
使用存储过程的报表方法如下:
首先你要建立你的存储过程建立存储如下虽然这样的简单的查询用存储过程没有必要这里也只是简单的示例:
ALTER procedure pr_test
as
DECLARE @chrnSQL nvarchar()
SELECT @chrnSQL=select * FROM InfoTable where age>
EXEC sp_ExecuteSql @chrnSQL
在上例的程序中增加DBGrid StoredProcDataSourceRvDataSetConnection[运行存储过程]按钮和[报表预览]按钮DataSource的dataset属性设置为StoredProcDBGrid的DataSource设置为DataSourceStoredProc连接数据库StoredProcName := pr_test; RvDataSetConnection的dataset属性设置为StoredProc[运行存储过程]按钮的click事件为
with StoredProc do begin
prepare;
StoredProcActive :=True;
end;
运行程序看到DBGrid显示了存储过程查询的结果
将程序运行[运行存储过程]然后打开Rave记住不要关掉查询的程序然后用和上例相同的方法添加如下代码
[File]=〉New Data Object=〉Direct Data View=〉 选择RvDataSetConnection=〉 [Finish]=〉
看到报表设计导航区的Data View Dictionary增加了DataView扩展后可以看到数据字段
选[Tools]àReport WizardsàSingle Tableà 选DataView选择数据库字段àReport Title改为个人情况报表好了之后可以看到在page中生成了报表
保存文件关闭程序添加[报表预览]按钮事件以及RvDataSetConnection的GetCols和GetRow事件
procedure TFormButtonClick(Sender: TObject);
begin
With RvProjectProjMan do
begin
RvProjectOpen ;
RvDataSetConnectionExecGetCols ; //得到列名
RvDataSetConnectionExecGetRow ; //得到记录
RvProjectExecuteReport(Report);
Close ;
end;
end;
procedure TFormRvDataSetConnectionGetCols(
Connection: TRvCustomConnection);
begin
ConnectionWriteField(namedtString); {列名}
ConnectionWriteField(sexdtString);
ConnectionWriteField(agedtInteger);
ConnectionWriteField(provincedtString);
end;
procedure TFormRvDataSetConnectionGetRow(
Connection: TRvCustomConnection);
begin
ConnectionWriteStrdata(DBGridFields[]value);
ConnectionWriteStrdata(DBGridFields[]value);
ConnectionWriteIntdata(DBGridFields[]value);
ConnectionWriteStrdata(DBGridFields[]value);
end;
好了这篇的讲解应该大家都明白和数据库有关的报表怎么设计了总结一下通过DBGrid数据感应组件得到查询的结果通过和rave的连接组件(RvDataSetConnectionRvQueryConnection等)的GetCols和GetRow事件往报表当中添加数据