19.5创建分层的SQL数据源控件

本文介绍了一种自定义的SQLHierarchicalDataSource控件,该控件扩展了标准的SqlDataSource,支持从数据库中获取分层数据。通过设置DataKeyName和DataParentKeyName属性,可以指定数据表中的主键和父键字段,从而实现对多表数据的层次结构展示。
创建分层的SQL数据源控件

SqlHierarchicaldataSource

SqlHierarchicalDataSource.cs
[code]
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace AspNetUnleashed
{

/// <summary>
/// Extends SqlDataSource control to support hierarchical database data
/// </summary>
public class SqlHierarchicalDataSource : SqlDataSource, IHierarchicalDataSource
{
private string _dataKeyName;
private string _dataParentKeyName;


public event EventHandler DataSourceChanged;


/// <summary>
/// The database table primary key
/// </summary>
public string DataKeyName
{
get { return _dataKeyName; }
set { _dataKeyName = value; }
}

/// <summary>
/// The database table parent id
/// </summary>
public string DataParentKeyName
{
get { return _dataParentKeyName; }
set { _dataParentKeyName = value; }
}

/// <summary>
/// Return hierarchical data
/// </summary>
public HierarchicalDataSourceView GetHierarchicalView(string viewPath)
{
return new SqlHierarchicalDataSourceView(this, viewPath);
}

}
}

[/code]

SqlHierarchicalDataSourceView.cs
[code]
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace AspNetUnleashed
{

/// <summary>
/// Represents the data returned from database
/// </summary>
public class SqlHierarchicalDataSourceView : HierarchicalDataSourceView
{
private SqlHierarchicalDataSource _owner;
private string _viewPath;
private DataTable _data;

public SqlHierarchicalDataSourceView(SqlHierarchicalDataSource owner, string viewPath)
{
_owner = owner;
_viewPath = viewPath;
}

/// <summary>
/// The DataTable which contains all rows from
/// underlying database table
/// </summary>
public DataTable Data
{
get { return _data; }
}

/// <summary>
/// We need to expose this for the SqlNodes
/// </summary>
public string DataKeyName
{
get { return _owner.DataKeyName; }
}

/// <summary>
/// We need to expose this for the SqlNodes
/// </summary>
public string DataParentKeyName
{
get { return _owner.DataParentKeyName; }
}

/// <summary>
/// Get the top-level rows (rows without parent rows)
/// </summary>
/// <returns></returns>
public override IHierarchicalEnumerable Select()
{
// Verify DataKeyName and DataParentKeyName properties
if (String.IsNullOrEmpty(DataKeyName))
throw new Exception("You must set the DataKeyName property");
if (String.IsNullOrEmpty(DataParentKeyName))
throw new Exception("You must set the DataParentKeyName property");

// Return DataView from SqlDataSource
if (_owner.DataSourceMode != SqlDataSourceMode.DataSet)
throw new Exception("DataSourceMode must be set to DataSet");
DataView view = (DataView)_owner.Select(DataSourceSelectArguments.Empty);
_data = view.Table;

// Get the root rows
string filter = string.Format("{0} IS NULL", this.DataParentKeyName);
DataRow[] rootRows = _data.Select(filter);

// Build up the hierarchical collection
SqlHierarchicalEnumerable en = new SqlHierarchicalEnumerable();
foreach (DataRow row in rootRows)
en.Add(new SqlNode(this, row));
return en;
}


}
}

[/code]

SqlHierarchicalEnumerable.cs
[code]
using System;
using System.Web.UI;
using System.Collections;

namespace AspNetUnleashed
{

/// <summary>
/// Represents a collection of SqlNodes
/// </summary>
public class SqlHierarchicalEnumerable : ArrayList, IHierarchicalEnumerable
{

public SqlHierarchicalEnumerable() : base() { }

public IHierarchyData GetHierarchyData(object enumeratedItem)
{
return enumeratedItem as IHierarchyData;
}


}
}
[/code]

SqlNode.cs
[code]
using System;
using System.Collections.Generic;
using System.Data;
using System.ComponentModel;
using System.Web.UI;

namespace AspNetUnleashed
{

/// <summary>
/// Represents a node (row) from the database
/// </summary>
public class SqlNode : IHierarchyData, ICustomTypeDescriptor
{

private SqlHierarchicalDataSourceView _owner;
private DataRow _row;

public SqlNode(SqlHierarchicalDataSourceView owner, DataRow row)
{
_owner = owner;
_row = row;
}

/// <summary>
/// Does the current database row have child rows?
/// </summary>
public bool HasChildren
{
get
{
string filter = String.Format("{0}={1}", _owner.DataParentKeyName, _row[_owner.DataKeyName]);
DataRow[] childRows = _owner.Data.Select(filter);

return childRows.Length > 0;
}
}


/// <summary>
/// Returns the DataRow
/// </summary>
public object Item
{

get { return _row; }

}

/// <summary>
/// A unique identifier for the row
/// </summary>
public string Path
{

get { return _row[_owner.DataKeyName].ToString(); }

}


/// <summary>
/// The Type is used in switching logic
/// </summary>
public string Type
{

get { return "SqlNode"; }

}

/// <summary>
/// The ToString() method is called to show
/// the value of a row (we default to showing
/// the value of the first column)
/// </summary>
public override string ToString()
{
return _row[0].ToString();
}

/// <summary>
/// Get child rows of current row
/// </summary>
public IHierarchicalEnumerable GetChildren()
{

string filter = string.Format("{0}={1}", _owner.DataParentKeyName, _row[_owner.DataKeyName]);
DataRow[] childRows = _owner.Data.Select(filter);

SqlHierarchicalEnumerable en = new SqlHierarchicalEnumerable();
foreach (DataRow row in childRows)
en.Add(new SqlNode(_owner, row));
return en;
}


/// <summary>
/// Get Parent Row of current row
/// </summary>
public IHierarchyData GetParent()
{
string filter = string.Format("{0}={1}", _owner.DataKeyName, _row[_owner.DataParentKeyName]);
DataRow[] parentRows = _owner.Data.Select(filter);

if (parentRows.Length > 0)
return new SqlNode(_owner, parentRows[0]);
else
return null;

}

/// <summary>
/// Get the list of properties supported by the SqlNode
/// </summary>
public PropertyDescriptorCollection GetProperties()
{
List<PropertyDescriptor> props = new List<PropertyDescriptor>();
foreach (DataColumn col in _owner.Data.Columns)
props.Add(new SqlNodePropertyDescriptor(col.ColumnName));
return new PropertyDescriptorCollection(props.ToArray());
}


// The following properties and methods are required by the
// ICustomTypeDescriptor interface but are not implemented

public System.ComponentModel.AttributeCollection GetAttributes()
{
throw new Exception("Not implemented.");
}

public string GetClassName()
{
throw new Exception("Not implemented.");
}

public string GetComponentName()
{
throw new Exception("Not implemented.");
}

public TypeConverter GetConverter()
{
throw new Exception("Not implemented.");
}

public EventDescriptor GetDefaultEvent()
{
throw new Exception("Not implemented.");
}

public PropertyDescriptor GetDefaultProperty()
{
throw new Exception("Not implemented.");
}

public object GetEditor(Type editorBaseType)
{
throw new Exception("Not implemented.");
}

public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
throw new Exception("Not implemented.");
}

public EventDescriptorCollection GetEvents()
{
throw new Exception("Not implemented.");
}

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
throw new Exception("Not implemented.");
}

public object GetPropertyOwner(PropertyDescriptor pd)
{
throw new Exception("Not implemented.");
}

}
}

[/code]

SqlNodePropertyDescriptor.cs
[code]
using System;
using System.ComponentModel;
using System.Data;


namespace AspNetUnleashed
{

/// <summary>
/// Describes a property of a SqlNode
/// </summary>
public class SqlNodePropertyDescriptor : PropertyDescriptor
{

public SqlNodePropertyDescriptor(string name) : base(name, null) { }


/// <summary>
/// Return the value of a DataColumn represented by
/// a particular SqlNode
/// </summary>
public override object GetValue(object component)
{
SqlNode node = (SqlNode)component;
return ((DataRow)node.Item)[this.Name];
}


// Don't bother to implement any of the other methods or properties
// of this class

public override bool CanResetValue(object component)
{
throw new Exception("Not implemented.");
}

public override Type ComponentType
{
get { throw new Exception("Not implemented."); }
}

public override bool IsReadOnly
{
get { throw new Exception("Not implemented."); }
}

public override Type PropertyType
{
get { throw new Exception("Not implemented."); }
}

public override void ResetValue(object component)
{
throw new Exception("Not implemented.");
}

public override void SetValue(object component, object value)
{
throw new Exception("Not implemented.");
}

public override bool ShouldSerializeValue(object component)
{
throw new Exception("Not implemented.");
}
}
}
[/code]

课本上用的例子是简单SQL,这个比较好写,但我的程序却是多张表中查找数据。所以我只好用存储过程。
Demo
treeView.aspx
[code]
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="treeview.aspx.cs" Inherits="treeview" %>

<%@ Register TagPrefix="user" Namespace="AspNetUnleashed" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" DataSourceID="srcLeftTree" runat="server">
<DataBindings>
<asp:TreeNodeBinding TextField="name" ValueField="id" />
</DataBindings>
</asp:TreeView>
<user:SqlHierarchicalDataSource ID="srcLeftTree" DataKeyName="id" DataParentKeyName="ParentId"
ConnectionString="<%$ ConnectionStrings:ytdl %>" SelectCommand="LeftTree_Data"
SelectCommandType="StoredProcedure" runat="server" />
</div>
</form>
</body>
</html>
[/code]

存储过程:LeftTree_Data
[code]
ALTER PROCEDURE dbo.LeftTree_Data
AS


Select Site_No as id,Site_Name as Name,null as ParentID From YT_Site

union all

select dev_id as id, dev_name as Name ,Site_No as ParentID from YT_Dev where Site_No in (select Site_no from YT_Site)

union all

select WD_ID as id,WD_Name as Name, Dev_id as ParentID from YT_WD where WD_Type=0 and Dev_ID in (
select dev_id from YT_Dev where Site_No in (select Site_no from YT_Site) )

union all

select WD_ID as ID,WD_Name as Name, WD_TO_WD as ParentID from YT_WD where WD_TO_WD in (
select WD_ID from YT_WD where WD_Type=0 and Dev_ID in (
select dev_id from YT_Dev where Site_No in (select Site_no from YT_Site) )
)

[/code]
刚开始时写错了一个地方,null as ParentID From YT_Site 直接写成了 0 as Parentid
结果不能正确显示数据。
查看代码才发现,原来是要为空。
在SqlHierarchicalDataSourceView.cs中有一段代码:
[code]
...
string filter = string.Format("{0} IS NULL", this.DataParentKeyName);
...
[/code]

才发现是要写空。当然,正常情况下也应该是为空的。



2011-5-31 21:01 danny
内容概要:本文围绕“光伏-储能-数据中心”系统的容量优化配置展开研究,旨在通过构建数学模型并结合Matlab编程实现,对园区内光伏发电、储能设备与数据中心算力负荷之间的多能互补关系进行协同优化。研究综合考虑可再生能源出力的波动性、储能系统的充放电特性以及数据中心的动态用电需求与算力调度特性,建立以最小化综合成本、降低碳排放强度、提升能源自给率等为目标的多目标优化模型,并采用先进的智能优化算法(如粒子群、灰狼、鲸鱼等)进行求解,从而确定光伏装机容量与储能系统配置的最优方案。文中提供了完整的Matlab代码实现流程,涵盖数据预处理、模型构建、算法求解与结果可视化全过程,属于综合能源系统与信息基础设施深度融合的前沿交叉课题。; 适合人群:具备电力系统、能源工程、自动化或计算机等相关专业背景,熟悉Matlab编程与基本优化算法原理,从事新能源利用、绿色数据中心、综合能源系统规划与低碳调度等方向的研究生、科研人员及工程技术人员。; 使用场景及目标:① 掌握光伏-储能-数据中心耦合系统的建模思路与多能流协同机制;② 学习基于Matlab的多目标容量优化配置方法与智能算法应用技巧;③ 为工业园区、数字经济园区及绿色数据中心的能源系统规划、节能减排与可持续运行提供科学决策依据和技术解决方案。; 阅读建议:建议结合所提供的Matlab代码,深入理解目标函数设计、约束条件设定及算法实现细节,可通过调整负荷参数、改变气候数据、引入新的优化目标或约束条件等方式进行拓展性实验,以深化对系统特性的认知并提升实际科研与工程应用能力。
内容概要:本文档聚焦于“考虑源网荷储协调的主动配电网优化调度方法”的Matlab代码复现研究,属于电力系统与综合能源系统领域的高水平科研资源。文档系统阐述了融合电源侧、电网侧、负荷侧及储能系统(源-网-荷-储)协同优化的主动配电网调度模型,旨在通过先进的数学优化方法提升电力系统的经济性、可靠性与低碳化水平。研究内容涵盖多时间尺度调度架构、不确定性建模(如新能源出力波动)、鲁棒优化或分布鲁棒优化(DRO)等核心技术,并依托Matlab平台完成模型构建与仿真验证。同时,文档列举了多个前沿研究方向,如光储充一体化、主从博弈、微电网双层优化、电氢耦合系统、电动汽车集群调度等,体现出其在现代智能电网与综合能源系统优化调度中的代表性与前瞻性。; 适合人群:具备电力系统基础理论知识和Matlab编程能力,从事能源、电气工程、自动化、可再生能源等方向研究的研究生、科研人员及工程技术人员,特别适合致力于优化建模、智能算法应用与综合能源系统仿真的研究人员。; 使用场景及目标:① 复现高水平期刊论文中的主动配电网优化调度模型;② 掌握源网荷储协同优化的建模方法与Matlab实现技术;③ 借助所提供的完整代码资源开展二次开发、算法改进或新场景拓展,服务于科研创新、项目申报与学术论文撰写。; 阅读建议:建议结合文档中提及的相关文献与案例,按照目录结构循序渐进地学习,重点理解模型构建的逻辑框架与代码实现的关键细节,并利用附带的百度网盘资源获取全部代码与数据文件,通过动手实践加深对优化算法与工程应用的理解。
内容概要:本文围绕《考虑光伏-储能-数据中心多能互补的园区容量优化配置(Matlab代码实现)》这一技术资源,系统研究了工业园区内光伏发电、储能系统与数据中心之间的能量协同与容量优化问题。通过构建融合可再生能源出力不确定性、负荷波动及数据中心算力负荷特性的数学优化模型,采用Matlab编程实现多能互补系统的容量配置求解,旨在提升能源利用效率、降低运营成本与碳排放,并增强园区能源系统的稳定性与经济性。文中综合运用分布鲁棒机会约束(DRCC)、N-1安全准则、鲁棒优化等先进建模方法,确保方案在复杂运行环境下的可行性与可靠性,适用于综合能源系统(IES)的规划与调度研究。; 适合人群:面向具备电力系统、能源系统或优化算法基础的研究生、科研人员及工程技术人员,尤其适合熟悉Matlab编程及YALMIP、CPLEX等优化工具的专业人士;对于从事智慧园区、绿色数据中心或低碳能源系统研究的人员亦具有较高参考价值。; 使用场景及目标:①支撑科研项目中对含数据中心的智慧园区进行多能互补建模与仿真分析;②用于撰写学位论文、期刊投稿中的案例验证与算法对比;③指导实际工程中光伏与储能容量的科学规划与配置决策;④深入学习分布鲁棒优化、N-1安全约束等高级方法在能源系统中的集成应用。; 阅读建议:建议结合提供的Matlab代码逐模块解析模型实现过程,重点关注目标函数构建、约束条件设定及求解器接口调用逻辑。推荐对照“顶级EI复现”案例,掌握工业级建模规范,并尝试复现关键结果以加深理解。同时可延伸学习文中关联的多时间尺度调度与协同优化策略,全面提升综合能源系统的研究与实践能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值