(急)好奇怪的问题:cannot perform this operation on a closed dataset?(200分)

  • 主题发起人 主题发起人 冰冷的雨
  • 开始时间 开始时间

冰冷的雨

Unregistered / Unconfirmed
GUEST, unregistred user!
请指教,谢谢!<br>代码如下,adoqBCDef和adoqBaseCode两个ADOQuery共用一个ADOConnection。<br>把红色部分注释掉能正常运行。<br>请注意,不管把adoqBCDef改成什么名字,错误信息中指出的对象实例都是“adoqBaseCode”<br>[black]procedure TForm1.FormCreate(Sender: TObject);<br>var<br>&nbsp; strSQL: string;<br>begin<br>&nbsp; strSQL := 'SELECT * FROM BCDefTable';[/black]<br><br>[red] &nbsp;with adoqBCDef do<br>&nbsp; begin<br>&nbsp; &nbsp; Close;<br>&nbsp; &nbsp; SQL.Clear;<br>&nbsp; &nbsp; SQL.Append(strSQL);<br>&nbsp; &nbsp; Open; &nbsp; &nbsp; //运行到此句出错<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //错误信息是:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // &nbsp;adoqBaseCode:cannot perform this operation on a closed dataset<br>&nbsp; &nbsp; ExecSQL;<br>&nbsp; end;[/red]<br>[black]<br>&nbsp; strSQL := 'SELECT * FROM BaseCodeTable';<br>&nbsp; with adoqBaseCode do<br>&nbsp; begin<br>&nbsp; &nbsp; Close;<br>&nbsp; &nbsp; SQL.Clear;<br>&nbsp; &nbsp; SQL.Append(strSQL);<br>&nbsp; &nbsp; Open;<br>&nbsp; &nbsp; ExecSQL;<br>&nbsp; end;<br>end;[/black]
 
晕死,怎么又open 又 ExecSQL 的?<br>去掉 ExecSQL吧
 
谢谢gxw,可去掉OPEN还是一样,关键是对adoqBCDef操作,但adoqBaseCode出错
 
刚才又试了试,把红色部分从FormCreate事件移到FormShow就正常了。而在FormCreate中对adoqBaseCode的类似操作就没问题
 
应该去掉execsql而不是去掉open,因为语句是select...,需要返回数据集的。
 
另外ADOConnection.connected:=true最好放在所有语句之前。
 
刚才又试了试,把红色部分从FormCreate事件移到FormShow就正常了。而在FormCreate中对adoqBaseCode的类似操作就没问题
 
呵呵,gxw真是热心。<br>ADOConnection.connected:=true是在控件里直接设的,应该在初始化的时候就执行了。<br>最开始我是用的OPEN,出错后改为EXECSQL,还是出错,干脆两个都用了。因为OPEN已经试过,所以就删除它了。<br>问题通过把那段代码移到FromShow事件里,并在FROM1第一次SHOW的时候设置一个标志,已经变通解决了,但错误原因还是没找出来。<br>为什么对adoqBCDef进行正常操作,会报adoqBaseCode出错呢?
 
呵呵,gxw真是热心。<br>ADOConnection.connected:=true是在控件里直接设的,应该在初始化的时候就执行了。<br>最开始我是用的OPEN,出错后改为EXECSQL,还是出错,干脆两个都用了。因为OPEN已经试过,所以就删除它了。<br>问题通过把那段代码移到FromShow事件里,并在FROM1第一次SHOW的时候设置一个标志,已经变通解决了,但错误原因还是没找出来。<br>为什么对adoqBCDef进行正常操作,会报adoqBaseCode出错呢?
 
应该在FORM创建时判断一下连接状态,这样确确CONNECTED都为TRUE;
 
原因是:你没加try...Except语句,所以第一次发生异常没及时退出FormCreate函数,接着往下执行到了第二个select语句,所以出这个错,这么改看看:<br><br>[black]procedure TForm1.FormCreate(Sender: TObject);<br>var<br>&nbsp; strSQL: string;<br>begin<br>&nbsp; strSQL := 'SELECT * FROM BCDefTable';[/black]<br><br>[red] &nbsp;with adoqBCDef do<br>&nbsp; begin<br>&nbsp; &nbsp; Close;<br>&nbsp; &nbsp; SQL.Clear;<br>&nbsp; &nbsp; SQL.Append(strSQL);<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; Open; &nbsp; &nbsp; //运行到此句出错<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; MessageDlg('adoqBCDef出误!',mtError, [mbOK], 0);<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //错误信息是:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // &nbsp;adoqBaseCode:cannot perform this operation on a closed dataset<br>&nbsp; &nbsp; //ExecSQL; 需要返回结果集(Select语句)就不要用ExecSQL,要用Open<br>&nbsp; end;[/red]<br>[black]<br>&nbsp; strSQL := 'SELECT * FROM BaseCodeTable';<br>&nbsp; with adoqBaseCode do<br>&nbsp; begin<br>&nbsp; &nbsp; Close;<br>&nbsp; &nbsp; SQL.Clear;<br>&nbsp; &nbsp; SQL.Append(strSQL);<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; Open;<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; MessageDlg('adoqBaseCode出误!',mtError, [mbOK], 0);<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; //ExecSQL; 需要返回结果集(Select语句)就不要用ExecSQL,要用Open<br>&nbsp; end;<br>end;[/black]
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
1K
import
I
I
回复
0
查看
2K
import
I
I
回复
0
查看
652
import
I
后退
顶部