用的是delphi开发工具;
我是写在过程中:
procedure opendata1(ado:tadoquery;str:string);
begin
try
ado.Close;
ado.SQL.Clear;
ado.SQL.Text:=str;
ado.Open;
except
application.MessageBox('打开数据错误!','系统提示',$30) ;
abort;
end;
end;
然后再具体程序界面里执行opendata1(form.adoquery1,'select * from table');第一次没有问题,但是第二次执行这个界面的时候就报错了,单步执行检查是执行sql.clear后直接就到打开数据库错误?奇怪?不知道是什么原因?
报错内容是:access violation at address 00000000....
我是写在过程中:
procedure opendata1(ado:tadoquery;str:string);
begin
try
ado.Close;
ado.SQL.Clear;
ado.SQL.Text:=str;
ado.Open;
except
application.MessageBox('打开数据错误!','系统提示',$30) ;
abort;
end;
end;
然后再具体程序界面里执行opendata1(form.adoquery1,'select * from table');第一次没有问题,但是第二次执行这个界面的时候就报错了,单步执行检查是执行sql.clear后直接就到打开数据库错误?奇怪?不知道是什么原因?
报错内容是:access violation at address 00000000....
建议楼主转delphi版.
估计会是"没有当前SQL 指令”这样的提示了。。
你在open前,一定要先检查str是否是空或str是否是合法的SQL 批处理指令.如果sql指令不合法,你用了try except end容错处理,这样的话,只要SQL指令出错,那你就会看到
你自己预定义的错误信息了。
procedure opendata1(ado:tadoquery;str:string);
begin
if str='' then Exit;
//如果sql批处理指令不合法,只接退出,这里只检查是否是空,但如果有str,但有错的话,是无法检查到的..
try
ado.Close;
ado.SQL.Clear;
ado.SQL.Text:=str;
ado.Open;
except
application.MessageBox('打开数据错误!','系统提示',$30) ;
//abort; 这一句可以不要了....
end;
end;
procedure opendata1(ado:TDataSet;str:string);
begin
try
with ado begin
close;
sql.text:=str;
open;
end;
except
application.MessageBox('打开数据错误!','系统提示',$30) ;
end;
end;
procedure opendata1(ado:tadoquery;str:string);
begin
try
ado.Close;
ado.SQL.Clear;
ado.SQL.Text:=str;
ado.Open;
except
application.MessageBox('打开数据错误!','系统提示',$30) ;
abort;
end;
end;
这段我在整个软件里经常调用都没有问题,只是在一个界面里执行两次就有问题,很奇怪。