procedure TmainFrm.createmenu;
var aquery1,aquery2,aquery3,aquery4:TADOQuery; //自己生成ADOQUERY控件
mymenu1,mymenu2,mymenu3,mymenu4:TMenuItem; //定义菜单级别
mysql1,mysql2,mysql3,mysql4:string;
begin
MainMenu1.Items.Clear ;
aquery1:=TADOQuery.Create(application);
aquery2:=TADOQuery.Create(application);
aquery3:=TADOQuery.Create(application);
aquery4:=TADOQuery.Create(application);
try
aquery1.Connection:=dm1.ADOConnection1; //数据库连接
aquery2.Connection:=dm1.ADOConnection1;
aquery3.Connection:=dm1.ADOConnection1;
aquery4.Connection:=dm1.ADOConnection1;
//一级菜单动态加载
mysql1:='';
mysql1:='select ccxlh,gnbh,substring(gnmch,1,len(gnmch)) as gnmch from menu where len(ccxlh)=2 order by ccxlh' ;
aquery1.Close;
aquery1.SQL.Clear;
aquery1.SQL.Add(mysql1);
aquery1.Prepared;
aquery1.Open ;
if aquery1.RecordCount<>0 then
begin
aquery1.First ;
while not aquery1.Eof do
begin
//创建一级菜单
mymenu1:=TMenuItem.Create(self);
mymenu1:=NewItem(aquery1.fields[2].value,0,false,true,MainFrm.Click,0,trim(aquery1.fields[1].value));
mainfrm.MainMenu1.Items.Add(mymenu1);
//判断是否有二级菜单,如有创建二级菜单
mysql2:='';
mysql2:='select ccxlh,gnbh,substring(gnmch,1,len(gnmch)) as gnmch from menu where len(ccxlh)=4 '+
' and substring(ccxlh,1,2)=:jibie order by ccxlh';
aquery2.Close;
aquery2.SQL.Clear ;
aquery2.SQL.Add(mysql2);
aquery2.Parameters.ParamByName('jibie').value:=aquery1.fields[0].value;
aquery2.Prepared ;
aquery2.ExecSQL ;
aquery2.Open ;
if aquery2.RecordCount <>0 then
begin
aquery2.First ;
while not aquery2.Eof do
begin
//创建二级菜单
mymenu2:=TMenuItem.Create(self);
mymenu2:=NewItem(aquery2.fields[2].value,0,false,true,MainFrm.Click,0,trim(aquery2.fields[1].value));
mymenu1.Add(mymenu2);
mysql3:='';
mysql3:='select ccxlh,gnbh,substring(gnmch,1,len(gnmch)) as gnmch from menu where len(ccxlh)=6 '+
' and substring(ccxlh,1,4)=:jb order by ccxlh';
aquery3.Close;
aquery3.SQL.Clear ;
aquery3.SQL.Add(mysql3);
aquery3.Parameters.ParamByName('jb').value:=aquery2.fields[0].value;
aquery3.Prepared ;
aquery3.ExecSQL ;
aquery3.Open ;
if aquery3.RecordCount <>0 then
begin
aquery3.First ;
while not aquery3.Eof do
begin
//创建三级菜单
mymenu3:=TMenuItem.Create(self);
mymenu3:=NewItem(aquery3.fields[2].value,0,false,true,MainFrm.Click,0,trim(aquery3.fields[1].value));
mymenu2.Add(mymenu3);
//四层循环开始
mysql4:='';
mysql4:='select ccxlh,gnbh,substring(gnmch,1,len(gnmch)) as gnmch from menu where len(ccxlh)=8 '+
' and substring(ccxlh,1,6)=:jb4 order by ccxlh';
aquery4.Close;
aquery4.SQL.Clear ;
aquery4.SQL.Add(mysql4);
aquery4.Parameters.ParamByName('jb4').value:=aquery3.fields[0].value;
aquery4.Prepared ;
aquery4.ExecSQL ;
aquery4.Open ;
if aquery4.RecordCount <>0 then
begin
aquery4.First ;
while not aquery4.Eof do
begin
//创建三级菜单