Blue
Red
Green
Orange
Voilet
Slate
Dark

多表多对多查询时遇到的怪问题。急救!(100分)

  • 主题发起人 zhang_j29
  • 开始时间
Z

zhang_j29

Unregistered / Unconfirmed
GUEST, unregistred user!
新人分不高,请大家多包含,谢谢!<br>我有5张表A、B、C、D、E。其中B是A的明细表,D是C的明细表。因为B与D之间存在多对多关系,所以A与C之间也存在多对多关系,故产生关系表E。E中只有4项A_ID,B_ID,C_ID,D_ID,分别存储的是ABCD表的关键字段,以表明其关系。<br><br>在程序中用4个DBgrid对应 ABCD 4张表,用5个ADOquery,datasource分别对应 ABCDE 5张表<br>(分别为adoquery1,datasource1对A表;adoquery2,datasource2对B表...adoquery5,datasource5对E表,方法比较笨,见笑。可帮我提下好方法)<br>我想通过选择查询方式(用checkbox判断)在窗体上实现任意以一张表为主,其它DBgrid中出现其相关记录。我的程序基本以实现这个功能,但出现个怪问题。以选B或D为主为例:<br>选B时查询一切正常,再换以D为主查询时程序出错(出错内容:adoquery4没找到B_ID);若重新进入程序,先以D为主查询则一切正常,再换以B为主查询则程序再出错(出错内容:adoquery2没找到D_ID)。我的程序如下,请各位帮忙分析一下。谢谢!!!<br>var<br>s1,s2,s3,s4,s5:string;<br>begin<br>adoquery1.close;<br>adoquery2.close;<br>adoquery3.close;<br>adoquery4.close;<br>adoquery5.close;<br>adoquery1.DataSource:=nil; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //避免循环套用<br>adoquery2.DataSource:=nil;<br>adoquery3.DataSource:=nil;<br>adoquery4.DataSource:=nil;<br>adoquery5.DataSource:=nil;<br><br>if checkbox1.check then &nbsp; &nbsp; &nbsp; &nbsp;//以B为主查询<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp;s1:='select * from A where A_ID=:A_ID'; &nbsp; &nbsp; &nbsp; &nbsp; //B表中有A_ID字段<br> &nbsp; &nbsp;s2:='select * from B ';<br> &nbsp; &nbsp;s3:='select * from C where C_ID=:C_ID'; &nbsp; &nbsp; &nbsp; &nbsp; //D表中有C_ID字段<br> &nbsp; &nbsp;s4:='select * from D where D_ID=:D_ID';<br> &nbsp; &nbsp;S5:='select * from E where B_ID=:B_ID'; &nbsp; &nbsp; &nbsp; &nbsp; //关系:C-&gt;D-&gt;E-&gt;B&lt;-A<br> &nbsp; &nbsp;adoquery1.datasource:=datasource2;<br> &nbsp; &nbsp;adoquery3.datasource:=datasource4;<br> &nbsp; &nbsp;adoquery4.datasource:=datasource5;<br> &nbsp; &nbsp;adoquery5.datasource:=datasource2;<br><br> &nbsp; &nbsp;adoquery2.sql.clear; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //执行SQL<br> &nbsp; &nbsp;adoquery2.sql.add(s2);<br> &nbsp; &nbsp;adoquery2.open;<br> &nbsp; &nbsp;adoquery1.sql.clear;<br> &nbsp; &nbsp;adoquery1.sql.add(s1);<br> &nbsp; &nbsp;adoquery1.open;<br> &nbsp; &nbsp;adoquery5.sql.clear;<br> &nbsp; &nbsp;adoquery5.sql.add(s5);<br> &nbsp; &nbsp;adoquery5.open;<br> &nbsp; &nbsp;adoquery4.sql.clear;<br> &nbsp; &nbsp;adoquery4.sql.add(s4);<br> &nbsp; &nbsp;adoquery4.open;<br> &nbsp; &nbsp;adoquery3.sql.clear;<br> &nbsp; &nbsp;adoquery3.sql.add(s3);<br> &nbsp; &nbsp;adoquery3.open;<br> &nbsp; &nbsp;end<br> &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //以D为主查询<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;s1:='select * from A where A_ID=:A_ID'; &nbsp; &nbsp; &nbsp; &nbsp; //B表中有A_ID字段<br> &nbsp; &nbsp; &nbsp;s2:='select * from B where B_ID=:B_ID';<br> &nbsp; &nbsp; &nbsp;s3:='select * from C where C_ID=:C_ID'; &nbsp; &nbsp; &nbsp; &nbsp; //D表中有C_ID字段<br> &nbsp; &nbsp; &nbsp;s4:='select * from D ';<br> &nbsp; &nbsp; &nbsp;S5:='select * from E where D_ID=:D_ID'; &nbsp; &nbsp; &nbsp; &nbsp; //关系:A-&gt;B-&gt;E-&gt;D&lt;-C<br> &nbsp; &nbsp;adoquery1.datasource:=datasource2;<br> &nbsp; &nbsp;adoquery2.datasource:=datasource5;<br> &nbsp; &nbsp;adoquery3.datasource:=datasource4;<br> &nbsp; &nbsp;adoquery5.datasource:=datasource4;<br><br> &nbsp; &nbsp;adoquery4.sql.clear; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //执行SQL<br> &nbsp; &nbsp;adoquery4.sql.add(s4);<br> &nbsp; &nbsp;adoquery4.open;<br> &nbsp; &nbsp;adoquery3.sql.clear;<br> &nbsp; &nbsp;adoquery3.sql.add(s3);<br> &nbsp; &nbsp;adoquery3.open;<br> &nbsp; &nbsp;adoquery5.sql.clear;<br> &nbsp; &nbsp;adoquery5.sql.add(s5);<br> &nbsp; &nbsp;adoquery5.open;<br> &nbsp; &nbsp;adoquery2.sql.clear;<br> &nbsp; &nbsp;adoquery2.sql.add(s2);<br> &nbsp; &nbsp;adoquery2.open;<br> &nbsp; &nbsp;adoquery1.sql.clear;<br> &nbsp; &nbsp;adoquery1.sql.add(s1);<br> &nbsp; &nbsp;adoquery1.open;<br> &nbsp; &nbsp;end;<br>END;
 

迷途的羔羊

Unregistered / Unconfirmed
GUEST, unregistred user!
先关闭再打开ado是试试
 
Z

zhang_j29

Unregistered / Unconfirmed
GUEST, unregistred user!
to &nbsp;迷途的羔羊<br>试过了。把开始的adoquery.close改成adoquery.active:=false;没用的。现在我实在搞不懂了[:(]
 

萧峒

Unregistered / Unconfirmed
GUEST, unregistred user!
看不太懂,似乎没有问题
 
Z

zhang_j29

Unregistered / Unconfirmed
GUEST, unregistred user!
上网查了几天,发现大家都说是DELPHI的BUG。完了[:(]
 
L

lgwen

Unregistered / Unconfirmed
GUEST, unregistred user!
在ADOQuery切换DateSource的时经常出现问题 <br>你最好还是换种处理方式
 
J

jenhon

Unregistered / Unconfirmed
GUEST, unregistred user!
如果你只是想解决问题,这个很好办啊。<br><br>就是用10个adoquery来完成,B用5个D用5个,应该没问题。<br><br>你用的数据库应该是ACCESS吗?我以前就碰过这种问题,CLOSE不行,CLEAR不行,连NEW一个都好像不行,不知道哪里出错,后来干脆同时建多个adoquery,各干各的工作就没问题了。
 
Z

zhang_j29

Unregistered / Unconfirmed
GUEST, unregistred user!
to:lgwen<br>还有其他什么方式?能不能指点一下。谢谢!
 
Z

zhang_j29

Unregistered / Unconfirmed
GUEST, unregistred user!
to:jenhon<br>这个办法我也想过,只不过我那4个DBgird还有其他的一些功能,比如双击1个DBgrid就要将对应的ADOquery进行操作等。若多个ADOquery,我就无法判断对那个ADOquery操作了。
 
D

dey-999

Unregistered / Unconfirmed
GUEST, unregistred user!
Tb: TADOQuery; aSQL: String; IsOpen: boolean=true<br> &nbsp;TB.Filtered &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; := false;<br> &nbsp;TB.Filter &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; := '';<br> &nbsp;Tb.Active &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; := false;<br> &nbsp;Tb.SQL.Clear ;<br> &nbsp;Tb.SQL.Add(aSQL);<br> &nbsp;try<br> &nbsp; &nbsp;if IsOpen then &nbsp;begin//打开数据库<br> &nbsp; &nbsp; &nbsp;tb.executeoptions &nbsp; &nbsp; := [];<br> &nbsp; &nbsp; &nbsp;Tb.Active &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; := true;<br> &nbsp; &nbsp;end else begin<br> &nbsp; &nbsp; &nbsp;tb.executeoptions &nbsp; &nbsp; := [eoExecuteNoRecords];<br> &nbsp; &nbsp; &nbsp;tb.Parameters.Clear;<br> &nbsp; &nbsp; &nbsp;Tb.ExecSQL;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;result &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= true;<br> &nbsp;except<br> &nbsp; &nbsp;on E: Exception do DoWriteLog(tb, timetostr(Time) + ': ' + aSQL+#13#10 + e.Message &nbsp;+#13#10 ,ViewMsg,IsOpen);<br> &nbsp;end;
 
Z

zhang_j29

Unregistered / Unconfirmed
GUEST, unregistred user!
为这问题我以耗了快一月时间了。郁闷。<br>天天都上网查,好象遇到这问题的还不少。但都没有一个正确解决了的。<br>网上的偏方也有几个,如楼上jenhon的多个ADOquery。还有先开附表,再开主表,之后再开附表然后再显示的。这两个的确行,但遇到一些比较麻烦的表对表关系时就显得力不从心了(如我的实际程序要想实现的功能,最上面的程序只是想说明问题举的例,实际情况比它复杂)。<br>不知那位大侠用比较规范的方法解决过这个问题没?跪求!!..5555....55...
 
L

lgwen

Unregistered / Unconfirmed
GUEST, unregistred user!
我也没有很好的方式,你可以试试jenhon的方法,我就是这么办的(感觉很笨)<br>我只有两个Query,你却有5个Query!
 
Z

zhang_j29

Unregistered / Unconfirmed
GUEST, unregistred user!
to:dey-999<br>能稍微解释一下吗?谢谢。
 
J

jenhon

Unregistered / Unconfirmed
GUEST, unregistred user!
来自:zhang_j29, 时间:2006-6-27 15:30:47, ID:3482852<br>to:jenhon<br>这个办法我也想过,只不过我那4个DBgird还有其他的一些功能,比如双击1个DBgrid就要将对应的ADOquery进行操作等。若多个ADOquery,我就无法判断对那个ADOquery操作了。 &nbsp;<br><br>只能建议了:<br>1、多个ADOQUERY只能是用数组进行判断了,麻烦,而且可能不是5+5的问题吧?可能是5*5个ADOQUERY....头大;<br>2、另外一种方法,不知道你尝试了没?<br>begin<br>adoquery1.close;<br>adoquery2.close;<br>adoquery3.close;<br>adoquery4.close;<br>adoquery5.close;<br>adoquery1.DataSource:=nil; //???你的ADOQUERY的DATASOURCE有什么啊?adoquery2.DataSource:=nil; //你不用TDATASOURCE关联?<br>adoquery3.DataSource:=nil;<br>adoquery4.DataSource:=nil;<br>adoquery5.DataSource:=nil;<br><br>ADOQUERY1.FREE;<br>ADOQUERY2.FREE;<br>ADOQUERY3.FREE;<br>ADOQUERY4.FREE;<br>ADOQUERY5.FREE;<br>DBGRID?.DATASOURCE:=nil; //要释放掉DBGRID的关联<br>ADOQUERY1:=TADOQUERY.Create(SELF);<br>ADOQUERY2:=TADOQUERY.Create(SELF);<br>ADOQUERY3:=TADOQUERY.Create(SELF);<br>ADOQUERY4:=TADOQUERY.Create(SELF);<br>ADOQUERY5:=TADOQUERY.Create(SELF);<br>.... //要补充定义好TDATASOURCE的关联哦<br>DBGRID?.DATASOURCE:=XXXX; //要关联回来<br><br><br>重新建ADOQUERY,试试。<br><br>另:别说是偏方了,都让我很不好意思,有时候只能那样做也是没办法的。
 
Z

zhang_j29

Unregistered / Unconfirmed
GUEST, unregistred user!
to:jenhon<br>谢谢!这办法管用,我试了!分给你了。<br>其实我说的偏方并不是贬义的意思。正所谓不管白猫、黑猫只要捉到耗子就是好猫。<br>希望我们以后能找到更好的办法,再次感谢!
 
顶部 底部