温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:VS2005典型实例源码大全(C#)
当前文件:
VS2005Helper/Web/App_Code/SqlSiteMapProvider.cs,打开代码结构图
VS2005Helper/Web/App_Code/SqlSiteMapProvider.cs,打开代码结构图1using System; 2
using System.Web; 3
using System.Data.SqlClient; 4
using System.Collections.Specialized; 5
using System.Configuration; 6
using System.Web.Configuration; 7
using System.Collections.Generic; 8
using System.Configuration.Provider; 9
using System.Security.Permissions; 10
using System.Data.Common; 11
using System.Data; 12
13
/// <summary> 14
/// SqlSiteMapProvider 15
/// </summary> 16
public class SqlSiteMapProvider : StaticSiteMapProvider 17
{ 18
private string _strCon; 19
private int _indexID, _indexTitle, _indexUrl, _indexDesc, _indexParent; 20
21
// 节点 22
private SiteMapNode _node; 23
24
// 节点字典表 25
private Dictionary<int, SiteMapNode> _nodes = new Dictionary<int, SiteMapNode>(); 26
27
// 用于单例模式 28
private readonly object _lock = new object(); 29
30
/// <summary> 31
/// 初始化 32
/// </summary> 33
/// <param name="name">name</param> 34
/// <param name="config">config</param> 35
public override void Initialize(string name, NameValueCollection config) 36
{ 37
// 验证是否有config 38
if (config == null) 39
throw new ArgumentNullException("config不能是null"); 40
41
// 没有provider则设置为默认的 42
if (String.IsNullOrEmpty(name)) 43
name = "SqlSiteMapProvider"; 44
45
// 没有描述就增加一个描述 46
if (string.IsNullOrEmpty(config["description"])) 47
{ 48
config.Remove("description"); 49
config.Add("description", "SqlSiteMapProvider"); 50
} 51
52
// 调用基类的初始化方法 53
base.Initialize(name, config); 54
55
// 初始化连接字符串 56
string conStringName = config["connectionStringName"]; 57
58
if (String.IsNullOrEmpty(conStringName)) 59
throw new ProviderException("没找到connectionStringName"); 60
61
config.Remove("connectionStringName"); 62
63
if (WebConfigurationManager.ConnectionStrings[conStringName] == null) 64
throw new ProviderException("根据connectionStringName没找到连接字符串"); 65
66
// 获得连接字符串 67
_strCon = WebConfigurationManager.ConnectionStrings[conStringName].ConnectionString; 68
69
if (String.IsNullOrEmpty(_strCon)) 70
throw new ProviderException("连接字符串是空的"); 71
} 72
73
/// <summary> 74
/// 从持久性存储区加载站点地图信息,并在内存中构建它 75
/// </summary> 76
/// <returns></returns> 77
public override SiteMapNode BuildSiteMap() 78
{ 79
lock (_lock) 80
{ 81
// 单例模式的实现 82
if (_node != null) 83
return _node; 84
85
SqlConnection connection = new SqlConnection(_strCon); 86
87
try 88
{ 89
SqlCommand command = new SqlCommand("sp_GetSiteMap", connection); 90
command.CommandType = CommandType.StoredProcedure; 91
92
connection.Open(); 93
SqlDataReader reader = command.ExecuteReader(); 94
95
// 获得各个字段的索引 96
_indexID = reader.GetOrdinal("ID"); 97
_indexUrl = reader.GetOrdinal("Url"); 98
_indexTitle = reader.GetOrdinal("Title"); 99
_indexDesc = reader.GetOrdinal("Description"); 100
_indexParent = reader.GetOrdinal("Parent"); 101
102
if (reader.Read()) 103
{ 104
// 把第一条记录作为根节点添加 105
_node = CreateSiteMapNodeFromDataReader(reader); 106
AddNode(_node, null); 107
108
// 构造节点树 109
while (reader.Read()) 110
{ 111
// 在站点地图中增加一个节点 112
SiteMapNode node = CreateSiteMapNodeFromDataReader(reader); 113
AddNode(node, GetParentNodeFromDataReader(reader)); 114
} 115
116
} 117
118
reader.Close(); 119
} 120
catch (Exception ex) 121
{ 122
throw new Exception(ex.ToString()); 123
} 124
finally 125
{ 126
connection.Close(); 127
} 128
129
// 返回SiteMapNode 130
return _node; 131
} 132
} 133
134
/// <summary> 135
/// 将检索目前由当前提供程序管理的所有节点的根节点 136
/// </summary> 137
/// <returns></returns> 138
protected override SiteMapNode GetRootNodeCore() 139
{ 140
lock (_lock) 141
{ 142
return BuildSiteMap(); 143
} 144
} 145
146
/// <summary> 147
/// 根据DataReader读出来的数据返回SiteMapNode 148
/// </summary> 149
/// <param name="reader">DbDataReader</param> 150
/// <returns></returns> 151
private SiteMapNode CreateSiteMapNodeFromDataReader(DbDataReader reader) 152
{ 153
if (reader.IsDBNull(_indexID)) 154
throw new ProviderException("没找到ID"); 155
156
int id = reader.GetInt32(_indexID); 157
158
if (_nodes.ContainsKey(id)) 159
throw new ProviderException("不能有重复ID"); 160
161
// 根据字段索引获得相应字段的值 162
string title = reader.IsDBNull(_indexTitle) ? null : reader.GetString(_indexTitle).Trim(); 163
string url = reader.IsDBNull(_indexUrl) ? null : reader.GetString(_indexUrl).Trim(); 164
string description = reader.IsDBNull(_indexDesc) ? null : reader.GetString(_indexDesc).Trim(); 165
166
// 新建一个SiteMapNode 167
SiteMapNode node = new SiteMapNode(this, id.ToString(), url, title, description); 168
169
// 把这个SiteMapNode添加进节点字典表里 170
_nodes.Add(id, node); 171
172
// 返回这个SiteMapNode 173
return node; 174
} 175
176
/// <summary> 177
/// 得到父节点的SiteMapNode 178
/// </summary> 179
/// <param name="reader"></param> 180
/// <returns></returns> 181
private SiteMapNode GetParentNodeFromDataReader(DbDataReader reader) 182
{ 183
if (reader.IsDBNull(_indexParent)) 184
throw new ProviderException("父节点不能是空"); 185
186
int pid = reader.GetInt32(_indexParent); 187
188
if (!_nodes.ContainsKey(pid)) 189
throw new ProviderException("有重复节点ID"); 190
191
// 返回父节点的SiteMapNode 192
return _nodes[pid]; 193
} 194
195
196
}







