下面的代码如何修改可以提升它的效率,也就是消耗内存减少!!!(0分)

  • 主题发起人 stonehuang
  • 开始时间
S

stonehuang

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.bsSkinButton1Click(Sender: TObject);
begin
try
dm.ADOConnection1.Connected:=true;
dm.adodataset_userpass.Close;
dm.ADODataSet_userpass.CommandText:='select * from user_pass';
dm.ADODataSet_userpass.Filtered:=false;
dm.ADODataSet_userpass.Filter:='ID='+quotedstr(bsSkinEdit1.Text);
dm.ADODataSet_userpass.Filtered:=true;
dm.ADODataSet_userpass.Open;
except
application.MessageBox('请正确设置数据库','数据库连接错误',mb_ok);
application.Terminate;
end;
if dm.ADODataSet_userpass.RecordCount=1 then
begin
if trim(dm.ADODataSet_userpass.FieldValues['passwd'])=trim(form1.bsSkinEdit2.Text) then
case form1.bsSkinComboBox1.ItemIndex of
0:
begin
if dm.ADODataSet_userpass.FieldValues['authority']=1 then
begin
form1.Hide;
form3.Show;
end
else
application.MessageBox('请选择合适的登陆系统','系统选择不正确',mb_ok);
end;
1:
begin
if dm.ADODataSet_userpass.FieldValues['authority']=2 then
begin
form1.Hide;
form4.Show;
end
else
application.MessageBox('请选择合适的登陆系统','系统选择不正确',mb_ok);
end;
2:
begin
if dm.ADODataSet_userpass.FieldValues['authority']=3 then
begin
form1.Hide;
form5.Show;
end
else
application.MessageBox('请选择合适的登陆系统','系统选择不正确',mb_ok);
end;
3:
begin
if dm.ADODataSet_userpass.FieldValues['authority']=4 then
begin
form1.Hide;
form6.Show;
end
else
application.MessageBox('请选择合适的登陆系统','系统选择不正确',mb_ok);
end
else
application.MessageBox('请选择合适的登陆系统','系统选择不正确',mb_ok);
end
else
application.MessageBox('请确认密码是否正确','密码不匹配',mb_ok);
end
else
application.MessageBox('请确认用户名称','无此用户',mb_ok);
end;

procedure TForm1.bsSkinButton2Click(Sender: TObject);
begin
application.Terminate;
end;

end.
 
你的很不错。。
 
这段代码确是需要改进
1、本来从DataSet中只需要返加一行数据,你先返回记录集中的所有记录,
然后,又过滤,所以自己增加程序的负担。
改成如下: dm.ADODataSet_userpass.Filtered:=false;(不用Filtered属性)

dm.ADOConnection1.Connected:=true;
dm.adodataset_userpass.Close;
dm.ADODataSet_userpass.CommandText:='select * from user_pass where ID=:ID';
dm.ADODataSet_userpass.ParamByName('ID').AsString:=quotedstr(bsSkinEdit1.Text);
dm.ADODataSet_userpass.Open;
第二你下面的 Else 太多了,不好读,应该优化一下.
 
else 可以用类似下面的方法优化一下:
bo:=false;
for i:=0 to 3 do
if dm.ADODataSet_userpass.FieldValues['authority']=i+1 then
begin
form1.Hide;
form6.Show;
bo:=true;
break;

end;
if not bo then
application.MessageBox('请选择合适的登陆系统','系统选择不正确',mb_ok);
 
同时你的 form3 ,form6最好弄成数组 myform:array[0..3] of tform
 
有人可以把完整代码写出来么?
 
procedure TForm1.bsSkinButton1Click(Sender: TObject);
var readdata : boolean;
begin
try
readdata := true;
dm.ADOConnection1.Connected:=true;
dm.adodataset_userpass.Close;
dm.ADODataSet_userpass.CommandText:='select * from user_pass where ID=:id';
dm.ADODataSet_userpass.Parameters.ParambyName('id').value := bsSkinEdit1.Text;
dm.ADODataSet_userpass.Open;
except
application.MessageBox('请正确设置数据库','数据库连接错误',mb_ok);
readdata := false;
//这里我觉得不用退出,完全可以让用户自己退出程序。在界面上多加一个退出按钮。
end;

if readdata then
begin
if dm.ADODataSet_userpass.FieldValues['authority'].asstring <>'' then
//使用ADO的时候,用RecordCount容易出问题。
begin
if trim(dm.ADODataSet_userpass.FieldValues['passwd'])=trim(form1.bsSkinEdit2.Text) then
begin
if dm.ADODataSet_userpass.FieldValues['authority'].AsInteger -1 <>
form1.bsSkinComboBox1.ItemIndex then
begin
application.MessageBox('请选择合适的登陆系统','系统选择不正确',mb_ok);
readdata := false;
end;
end else
begin
application.MessageBox('请确认用户名称','无此用户',mb_ok);
readdata := false;
end;
end else
begin
application.MessageBox('请确认用户名称','无此用户',mb_ok);
readdata := false;
end;
end;

if readdata then
begin
Form1.hide;
case form1.bsSkinComboBox1.ItemIndex of
0: Form3.show;
1: Form2.Show;
end;
end;

procedure TForm1.bsSkinButton2Click(Sender: TObject);
begin
Close;
end;

end.
 
大家认为这样耗费的内存资源最少么?
 
我觉得肯定不是,但是不知道他的窗体究竟是如何做的。

如果窗体动态创建肯定可以更小一点。不过应该还有可以优化的地方
 
各位再研究研究如何?谢谢娃娃!
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
844
SUNSTONE的Delphi笔记
S
顶部