四、数据显示
通过上述例子可以看出,IntraWeb用来设计Web数据库的应用非常简单,确实与我们开发普通的数据库应用方式基本一样。但是要注意,在所有的Web应用中,传递数据量过大将会造成网络负担过重和用户页面下载速度过慢,为了避免出现这种情况,就需要限制客户端下载的量。那么,在我们网站设计中又该如何结合IntraWeb的特点改善这些必要工作呢?实际上,只有部分数据对用户有用,不需要将所有数据一次全部在客户端下载,网络应用程序效率低下有一部分原因就是由于在数据库服务器上读取过多的数据而浪费了太多的时间。因此,应该采取相应的措施,尽可能提高Web应用程序的效率。依据经验我们可以从下列两个方面来改进和提高效率:
1.分页显示
很多的网络数据应用都是分页显示的,如Baidu或者Google的搜索引擎。在IntraWeb中,同样可以使用分页显示。方法是在使用TIWDgrid控件来显示数据库信息时,先将IWDGrid.fromstart设为Fasle,不允许每次从首记录读取数据;再将IWDBGrid.rowlimit设为每页显示的记录条数限制在一定的数目内,比如20条记录;最后在窗口添加“前一页”、“下一页”两个超链接按钮或是标签,并做相应处理。以下一按钮事件为例:
Procedure TIWform1.IWLinkpreClick(Sender: TObject);
Begin
If not UserSession.Tabel1.Bof then
UserSession.Table.MoveBy(-IWDBGrid1.RowLimit)
Else
WebApplication.Showmessage(‘已经到了第一页,不能再移动了…’);
End
Procedure TIWform1.IWLinkNxtClick(Sender: TObject);
Begin
If not UserSession.Tabel1.Eof then
UserSession.Table.MoveBy(IWDBGrid1.RowLimit)
Else
WebApplication.Showmessage(‘已经到了最后一页,不能再移动了…’);
End
2.条件显示
这个问题较好理解,与网络搜索引擎的手法一样,用户在页面输入读取数据的条件,然后系统根据条件从数据服务器上读取数据,再将结果下载到客户端。方法也很简单,在在线教学网站设计中对于Table查询采用Firter参数来实现用户的条件过滤,而Query查询则采用SQL 结构化查询语言的处理方式来完成,与普通应用的条件查询无异。
五、代码设计
1.用户登录部分
也许大家还有印象,在登录窗口中有一个“用户注册”按钮一直还没有添加代码,用户注册页面设计好后,下面就可以添加代码了。
“用户注册”按钮事件代码如下:
procedure TF_IWLogin.IWButton2Click(Sender: TObject);
begin
TF_IWRegistry.Create(WebApplication).Show; //调用用户注册
end;
2.用户注册页面代码
ADO数据库的初始化代码如下:
procedure TF_IWRegistry.IWAppFormCreate(Sender: TObject);
var Datapath:string;
begin
Datapath:=WebApplication.ApplicationPath+'data'; //数据库所在路径
ADOConnection1.ConnectionString :='Data Source='+datapath+'\mydatabase.mdb;Persist Security Info=False';
//指定数据库名称
ADOConnection1.Connected :=true; //数据库连接
IWLabel12.Caption :=''; //提示信息为空
end;
“提交”按钮事件代码如下:
procedure TF_IWRegistry.IWButton1Click(Sender: TObject);
begin
ADOtable1.Filter:='用户名='''+Iwedit1.text+'''';
ADOtable1.Filtered:=True;
ADOtable1.Open;
if ADOtable1.RecordCount = 1 then
begin
WebApplication.ShowMessage('该用户已经注册', smAlert);
exit;
end;
adotable1.Append;
adotable1.Edit;
if IwEdit1.Text<>'' then
adotable1['用户名']:=IwEdit1.Text else iwlabel12.Caption :='错误,姓名不能为空...';
if IwEdit10.Text<>'' then
adotable1['性别']:=IwEdit10.Text else iwlabel12.Caption :='错误,性别不能为空...';
if IWComboBox1.Items.Text<>'' then
adotable1['登录身份']:=IWComboBox1.Items.Text else iwlabel12.Caption :='错误,身份没有确认...';
if IwEdit2.Text<>'' then
adotable1['登录密码']:=IwEdit2.Text else iwlabel12.Caption :='错误,密码不能为空...';
if IwEdit3.Text<>'' then
adotable1['课程名称']:=IwEdit3.Text else iwlabel12.Caption :='错误,课程名称不能为空...';
if IwEdit4.Text<>'' then
adotable1['专业']:=IwEdit4.Text else iwlabel12.Caption :='错误,专业不能为空...';
if IwEdit5.Text<>'' then
adotable1['ID号']:=IwEdit5.Text else iwlabel12.Caption :='错误,学号不能为空...';
if IwEdit6.Text<>'' then
adotable1['入校时间']:=IwEdit6.Text else iwlabel12.Caption :='错误,年级不能为空...';
if IwEdit7.Text<>'' then
adotable1['学院']:=IwEdit7.Text else iwlabel12.Caption :='错误,学院不能为空...';
adotable1['联系电话']:=IwEdit8.Text;
adotable1['邮箱']:=IwEdit9.Text;
adotable1.Post;
end;
“注册名有效性检验”按钮代码如下:
procedure TF_IWRegistry.IWButton2Click(Sender: TObject);
begin
if Iwedit1.text<>'' then
begin
ADOtable1.Filter:='用户名='''+Iwedit1.text+''''; //查验用户是否存在
ADOtable1.Filtered:=True;
ADOtable1.Open;
end else exit;
if ADOtable1.RecordCount = 1 then
begin
WebApplication.ShowMessage('该用户已经存在...', smAlert);
exit;
end else WebApplication.ShowMessage('可以正常注册...', smAlert);
ADOtable1.close;
end;
“修改注册信息...”按钮代码如下:
procedure TF_IWRegistry.IWButton3Click(Sender: TObject);
begin
if iwedit1.text<>'' then
adotable1.Filter:='用户名='''+iwedit1.text+''' and 登录密码='''+iwedit2.text+'''' ;
//若用户名和密码一致则将相关信息显示出来
adotable1.filtered:=true;
adotable1.open;
if adoTable1.RecordCount = 1 then
begin
IWedit3.text := ADOtable1.FieldByName('课程名称').Asstring ;
IWComboBox1.ItemIndex :=-1;
IWComboBox1.Items.Text :=ADOtable1.FieldByName('登录身份').Asstring ;
IWedit4.Text:=ADOtable1.FieldByName('专业').Asstring ;
IWedit5.Text:=ADOtable1.FieldByName('ID号').Asstring ;
IWedit6.Text:=ADOtable1.FieldByName('入校时间').Asstring ;
IWedit7.Text:=ADOtable1.FieldByName('学院').Asstring ;
IWedit8.Text:=ADOtable1.FieldByName('联系电话').Asstring ;
IWedit9.Text:=ADOtable1.FieldByName('邮箱').Asstring ;
iwbutton4.Enabled :=true;
end;
end;
“确认修改”按钮代码如下:
procedure TF_IWRegistry.IWButton4Click(Sender: TObject);
begin
adotable1.Edit;
adotable1.Open;
WebApplication.ShowMessage('信息修改成功...', smAlert);
IWButton4.Enabled :=false;
end;
3.在线考勤页面
在线考勤页面的初始化代码如下:
procedure Tf_IWAttendance.IWAppFormCreate(Sender: TObject);
var AStrdate:string;
datapath:string;
begin
datapath:=WebApplication.ApplicationPath+'data';
ADOConnection1.ConnectionString :='Data Source='+datapath+'\mydatabase.mdb;Persist Security Info=False';
ADOConnection1.Connected :=true;
tblstudent.Filter :='登录身份='''+'学生'+''''; //从注册表筛选出学生
tblstudent.Filtered:=True;
tblstudent.Open;
tblattendance.Active :=true;
AStrDate := FormatDateTime('yyyy-mm-dd', date());
TIWDatePicker1.Date :=strtodate(AStrdate);
tblAttendance.Open;
tblAttendance.edit;
tblAttendance['日期']:=strtodate(ASTrdate);
if lboxFiles.ItemIndex > -1 then
begin
if lboxFiles.Text='环境信息导论' then
begin
tblStudent.Filter:='课程名称='''+lboxFiles.Text+'''';
tblStudent.Filtered:=True;
tblStudent.Open;
end;
end;
end;
“考勤日期”选择按钮代码如下:
procedure Tf_IWAttendance.TIWDatePicker1Click(Sender: TObject;
ADate: TDateTime);
var AStrDate:string;
begin
AStrDate := FormatDateTime('yyyy-mm-dd', TIWDatePicker1.Date);
tblAttendance.Open;
tblAttendance.edit;
tblAttendance['日期']:=strtodate(ASTrdate);
end;
在IntraWeb的IWDBGrid中,记录定位不能直接通过点击相应的行来实现,需要一个专门函数代码,与LinkField字段一起使用。Gostudent即是实现该过程代码,它将数据库中的编号与用户名(学生)结合,从而实现记录的准确定位效果。
procedure Tf_IWAttendance.GoStudent(const AStudentNo: integer);
begin
tblstudent.Locate('编号', AStudentNo, []);
stdname:=tblstudent['用户名'];
tblattendance.Filtered:=False;
// tblattendance.tablename:='attendance.db';
{用课程名称、学生姓名和上课时间来过滤考勤库,在考勤库中添加或考勤学生}
tblAttendance.Filter:='课程名称='''+lboxFiles.Text+''' and 学生姓名='''+stdname+''' and 日期='''+IWDBEdit7.Text +'''';
tblAttendance.Filtered:=True;
tblAttendance.Open;
if tblAttendance.RecordCount <=0 Then
begin
tblAttendance.Edit;
tblAttendance.insert;
tblAttendance['学生姓名'] :=stdname;
tblAttendance['日期'] :=TIWDatePicker1.Date;
//if IWDBEdit7.Text='' then
IWDBEdit7.Text:=datetostr(TIWDatePicker1.Date);
tblAttendance.Filtered:=false;
end;
end;
下面测试一下代码的运行效果,在Delphi中直接按下F9,登录后在主页面菜单中点击“教学档案”→“在线考勤”菜单项,浏览器中出现页面如图4所示。通过点击课程选择框,考勤日期选择、记录定位、记录操作和结果提交等进行相应测试,一切正常。
另外,数据库后台管理方面内容一则因为相似功能已有介绍,二则限于篇幅,所以文字上也就不详细描述了,但是实例程序部分已经有页面和代码,网站运行后可通过菜单“后台管理”→“注册信息表”进入,有兴趣的读者可以打开以便了解,实例中页面窗口文件为F_iwmanage,单元文件为u_BKMange.pas。

图 4 在线考勤的浏览效果
|