遍历数据库表内容生成站点地图

看了MSDN上的例子和川哥的教程。自己动手根据自己的表结构写了一下根据表生成web.sitemap的东东。不过发现有几个问题
第一,发现这个一旦生成sitemap以后如果服务器不重启,那么即使你修改了表内容也无法实时的表现出结果来。原因是做个一个SMNode是否为空判断。
不知道这里的这个判断有什么实际的意义?我发现一旦生成这个sitemap后这个东西就一直随着服务器存在。
第二,如何和我自己的权限配合起来用,好像记得这个有个权限的功能。不过不知道是不是需要和membership配合使用?如果我根据自己的权限来对不同用户生成sitemap不知是否可行?会出什么问题?
 第三,数据库如果有个链接是直接链接到其他网站的,会出错。比如 http://www.sohu.com 但是有些时候确实需要这样的链接,这个怎么加进去?
第四,如果使用这种方式如何设置多个sitemap?看了一下msdn,视乎只有用 XmlSiteMapProvider 才能设置多个?
下面是我改的代码。^_^

None.gif namespace  LILELTP.Controls
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
using System;
InBlock.gif    
using System.Data;
InBlock.gif    
using System.Configuration;
InBlock.gif    
using System.Web;
InBlock.gif    
using System.Web.Security;
InBlock.gif    
using System.Web.UI;
InBlock.gif    
using System.Web.UI.WebControls;
InBlock.gif    
using System.Web.UI.WebControls.WebParts;
InBlock.gif    
using System.Web.UI.HtmlControls;
InBlock.gif    
//引入需要的命名空间
InBlock.gif
    using System.Collections.Specialized;
InBlock.gif    
using System.Collections;
InBlock.gif    
using System.Security.Permissions;
InBlock.gif    
using db=LILELTP.DataBase.Database;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// sqlsitemap 的摘要说明
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
InBlock.gif    
//权限说明
InBlock.gif
    public class sqlsitemap : StaticSiteMapProvider
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private SiteMapNode SMNode = null;
InBlock.gif        
private string time = null;
InBlock.gif
InBlock.gif        
public sqlsitemap()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private bool initialized = false;
InBlock.gif        
public virtual bool IsInitialized
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{//重写基类IsInitialized属性,在调用基类 Initialize 之后在该方法中执行自己的初始化
InBlock.gif
            get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gifreturn initialized; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected override SiteMapNode GetRootNodeCore()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{//重写基类GetRootNodeCore方法,使之返回RootNode这个属性
InBlock.gif
            return BuildSiteMap();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public override void Initialize(string name, NameValueCollection attributes)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{//重写基类Initialize方法,如果成功,那么将initialized设置为true
InBlock.gif
            if (IsInitialized)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Initialize(name, attributes);
InBlock.gif            
//调用基类Initialize方法来初始化
InBlock.gif
            initialized = true;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected override void Clear()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
lock (this)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (SMNode == null)
InBlock.gif                    
base.Clear();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override SiteMapNode BuildSiteMap()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
lock (this)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{//锁定
InBlock.gif
                if (!IsInitialized)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{//判断是否初始化了
InBlock.gif
                    throw new Exception("数据未初始化");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (SMNode == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Clear();
InBlock.gif                        
string strSQL = "SELECT ID,Name,ParentID,NavigateUrl,description from might_Menu WHERE Purview='Y' ORDER BY [ID],ParentID";
InBlock.gif                        DataSet dsnode 
= db.ExecuteDataset(db.ConnectionString, CommandType.Text, strSQL);
InBlock.gif                        SMNode 
= new SiteMapNode(this"-1""default.aspx""首页");
InBlock.gif                        BuildChildSite(
ref SMNode, 0, dsnode);
InBlock.gif                        time 
= DateTime.Now.ToString();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
string x = time;
InBlock.gif                
return SMNode;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 遍历生成子节点菜单
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="smn">父节点</param>
InBlock.gif        
/// <param name="cid">节点编号</param>
ExpandedSubBlockEnd.gif        
/// <param name="ds">DataSet</param>

InBlock.gif        private void BuildChildSite(ref SiteMapNode smn, int pid, DataSet ds)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
foreach (DataRow dr in ds.Tables[0].Select("ParentID   =   " + pid))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    SiteMapNode nsmn 
= new SiteMapNode(this, dr["id"].ToString(), dr["NavigateUrl"].ToString(), dr["Name"].ToString(), dr["description"].ToString());
InBlock.gif                    AddNode(nsmn, smn);
InBlock.gif                    BuildChildSite(
ref nsmn, (int)dr["id"], ds);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ex.ToString();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

None.gif if   exists  ( select   1
None.gif            
from   sysobjects
None.gif           
where   id  =   object_id ( ' dbo.Menu ' )
None.gif            
and    type  =   ' U ' )
None.gif   
drop   table  dbo.Menu
None.gif
go
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*==============================================================*/
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* Table: Menu                                            */
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*==============================================================*/
None.gif
create   table  dbo.Menu (
None.gif   ID                   
int                    not   null ,
None.gif   Name                 
varchar ( 20 )           null ,
None.gif   ParentID             
int                    null ,
None.gif   NavigateUrl          
varchar ( 100 )          null ,
None.gif   description          
varchar ( 200 )          null ,
None.gif   Taxis                
varchar ( 20 )           null ,
None.gif   Purview              
char ( 1 )               null ,
None.gif   DelSign              
int                    null ,
None.gif   
constraint  PK_MENU  primary   key   (ID)
None.gif)
None.gif
go
None.gif

None.gif

发现msdn上有这么一篇文章

http://msdn2.microsoft.com/en-us/library/aa479033.aspx

转载于:https://www.cnblogs.com/Lileltp/archive/2007/06/16/sqlwebsitemap.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值