动态得到ADOQUERY连接的表的名称(100分)

  • 主题发起人 主题发起人 huysnet
  • 开始时间 开始时间
H

huysnet

Unregistered / Unconfirmed
GUEST, unregistred user!
代码如下,我想要得到SNAME,他的名称是数据库表名称,现在我没有办法,只有用ADOQUERY的名字和表名称一致才能够动态得到表的名字,请高手指点<br>procedure Tspfrm.dbckup(Sender: TObject);<br>var<br>&nbsp; sid,sname:string;<br>// &nbsp;tmado:tadoquery;<br>begin<br>// &nbsp;Session-&gt;GetTableNames<br>&nbsp;with sdata.tmp do<br>&nbsp;begin<br>&nbsp; &nbsp;close;<br>&nbsp; &nbsp;sql.Clear;<br>&nbsp; &nbsp;sid:=dbgrid1.DataSource.DataSet.fieldbyname('typeid').AsString;<br>&nbsp; &nbsp;sname:= dbgrid1.DataSource.DataSet.Name;<br>&nbsp; &nbsp;sql.Add('select * from '+sname+' where parid = '+sid);<br>&nbsp; &nbsp;open;<br>&nbsp; &nbsp;if not isempty then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;//加filter重新书写,并且在lable1.CAPTION 更改<br>&nbsp; &nbsp; &nbsp;dbgrid1.DataSource.DataSet.Filtered:=false;<br>&nbsp; &nbsp; &nbsp;dbgrid1.Tag:=dbgrid1.Tag+1;<br>&nbsp; &nbsp; &nbsp;dbgrid1.DataSource.DataSet.Filter:='parid = '+sid + ' AND &nbsp;leveal = '+inttostr(dbgrid1.Tag);<br>&nbsp; &nbsp; &nbsp;dbgrid1.DataSource.DataSet.Filtered:=true;<br>// &nbsp; &nbsp; dbgrid1.Columns[0].Visible:=false;<br>&nbsp; &nbsp; &nbsp;label1.Caption:=label1.Caption+'/'+Fields[2].Asstring;<br>&nbsp; &nbsp; &nbsp;listbox2.Items.Add(label1.Caption);<br>&nbsp; &nbsp; &nbsp;listbox2.Items.Add(sid);<br>&nbsp; &nbsp; &nbsp;listbox2.Tag:=listbox2.Tag+2;<br>// &nbsp; &nbsp; application.MessageBox(pchar('上一级的名称是:'+Fields[2].Asstring+' !'),pchar(application.title),mb_ok)<br>&nbsp; &nbsp;end &nbsp;else<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;//修改菜单弹出<br>&nbsp; &nbsp; &nbsp;application.MessageBox('是最末端的数据','aaa',mb_ok)<br>&nbsp; &nbsp;end;<br>&nbsp;end;<br>end;
 
通过adoconnection的一个gettablesname方法就可以取得所有表名
 
GetTableNames这个方法是取的所有的数据库表名称,而不是ADOQUEY连接的表的名称
 
我提供一个笨办法:分析ADOQUERY中的SQL<br>var<br>&nbsp; sSQL,sTableName:string;<br>begin<br>&nbsp; sSQL := Trim(UPPERCASE(ADOQUERY.SQL.TEXT));<br>&nbsp; sTableName := Copy(sSQL,Pos('FROM ',sSQL)+5,100000);<br>&nbsp; if Pos(' ',sTableName)&gt;0 then<br>&nbsp; &nbsp; sTableName := Copy(sSQL,1,Pos(' ',sTableName-1);<br>end;
 
接受答案了.<br>谢谢lanyaoshi
 
后退
顶部