给小安同学学习一下多层结构的应用和使用存储过程的方法,仿照Duwamish7的方式,以下只列出了主要的实现代码,命名空间引用等其他内容没有列出,可以跟我索取这个简单的例子!如果兄弟也是刚转到.Net上来,而且想采用多层结构来入门,研究研究这个倒会有点帮助的嘛!我觉得多层结构对于.Net来说并未有助于提高性能,倒是真麻烦。优点嘛,google上写的多的是!
高手就不用看了,有时间的话教教我喽,hoho……
*********************************************************************
共用:Common
商务层:BusinessFacade
数据层:DataAccess
表示层:Web
*********************************************************************
** 存储过程:
*********************************************************************
CREATE PROCEDURE GetCategories
AS
SELECT
NodeId, ParentId, NodeName, OrderId, Menu, Url
FROM News_Categories
ORDER BY OrderId
RETURN 0
GO
*********************************************************************
** Common
** namespace Test.Common.Data
*********************************************************************
public class NewsCategoryData : DataSet
{
public const String NEWS_CATEGORIES_TABLE = "News_Categories";
public const String NODE_ID_FIELD = "NodeId";
//省略相同部分
public NewsCategoryData()
{
//
// Create the tables in the dataset
//
BuildDataTables();
}
private void BuildDataTables()
{
//
// Create the Categories table
// 好象Petshop里也是用如下方法,确实不错!
//
DataTable table = new DataTable(NEWS_CATEGORIES_TABLE);
DataColumnCollection columns = table.Columns;
columns.Add(NODE_ID_FIELD, typeof(System.Int32));
columns.Add(PARENT_ID_FIELD, typeof(System.Int32));
columns.Add(NODE_NAME_FIELD, typeof(System.String));
columns.Add(ORDER_ID_FIELD, typeof(System.Int32));
columns.Add(MENU_FIELD, typeof(System.String));
columns.Add(URL_FIELD, typeof(System.String));
this.Tables.Add(table);
}
}
*********************************************************************
** DataAccess
** 使用不带参数的存储过程的例子
** namespace Test.DataAccess
*********************************************************************
public class NewsCategories
{
private SqlDataAdapter dsCommand;
public NewsCategories()
{
//
// TODO: 在此处添加构造函数逻辑
//
dsCommand = new SqlDataAdapter();
//
// 获取数据库链接
//
dsCommand.SelectCommand = new SqlCommand();
string ConnStr = ConfigurationSettings.AppSettings["ConnectionString"];
dsCommand.SelectCommand.Connection = new SqlConnection(ConnStr);
dsCommand.TableMappings.Add("Table", NewsCategoryData.NEWS_CATEGORIES_TABLE);
}
// Duwamish7中使用的是带参数的存储过程
public NewsCategoryData GetNewsCategories()
{
return FillNewsCategoryData("GetNewsCategories");
}
private NewsCategoryData FillNewsCategoryData(String commandText)
{
NewsCategoryData data = new NewsCategoryData();
SqlCommand command = dsCommand.SelectCommand;
command.CommandText = commandText;
command.CommandType = CommandType.StoredProcedure;
dsCommand.Fill(data);
return data;
}
}
*********************************************************************
** BusinessFacade
** namespace Test.BusinessFacade
*********************************************************************
public class ProductSystem
{
public NewsCategoryData GetNewsCategories()
{
NewsCategories accessNewsCategories = new NewsCategories();
return accessNewsCategories.GetNewsCategories();
}
}
*********************************************************************
** Web 层
** namespace Test.Web.manage
*********************************************************************
private void Page_Load(object sender, System.EventArgs e)
{
//
NewsCategoryData NewscategorySet;
ProductSystem productSystem;
DataView categoryView;
productSystem = new ProductSystem();
NewscategorySet = productSystem.GetNewsCategories();
categoryView = NewscategorySet.Tables[NewsCategoryData.NEWS_CATEGORIES_TABLE].DefaultView;
DataGrid1.DataSource=categoryView;
DataGrid1.DataBind();
}
*********************************************************************

浙公网安备 33010602011771号