这段时间开发ASP.NETMVC应用程序,从数据库获取数据之后,需要把记录转换为数据集在视图中显示。我们需要把DataTable转换为List<T>或者DataRow转换为T。
本篇中可以学习到相关的知识,数据库方面,创建表,添加数据,存储过程等。MVC方面,创建model,创建Entity,Utility写在一个目录中,控制器创建视图操作,以及行为操作。
先从数据库:
SQL代码:
CREATE TABLE [dbo].[Cookbook]
(
[ID] INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Name] NVARCHAR(30) NULL,
[Author] NVARCHAR(30) NULL,
[CreateDate] DATETIME NULL,
[ImagePath] NVARCHAR(100) NULL
)
GO INSERT INTO [dbo].[Cookbook] VALUES
(N'糯香土豆','Leo Yang','2015-07-12','Content/Image/Cookbook/0.jpg'),
(N'紫菜蛋花汤','Leo Yang','2015-07-12','Content/Image/Cookbook/1.jpg'),
(N'橄榄菜炒通心菜','Leo Yang','2015-07-12','Content/Image/Cookbook/2.jpg'),
(N'蒜瓣炒番薯叶','Leo Yang','2015-07-12','Content/Image/Cookbook/3.jpg'),
(N'肉片炒豆角','Leo Yang','2015-07-12','Content/Image/Cookbook/4.jpg'),
(N'香葱炒土豆','Leo Yang','2015-07-12','Content/Image/Cookbook/5.jpg')
GO CREATE PROCEDURE [dbo].[usp_Cookbook_GetAll]
AS
SELECT [ID],[Name],[Author],[CreateDate],[ImagePath] FROM [dbo].[Cookbook]
GO CREATE PROCEDURE [dbo].[usp_Cookbook_GetByPk]
(
@ID INT
)
AS
SELECT [ID],[Name],[Author],[CreateDate],[ImagePath] FROM [dbo].[Cookbook] WHERE [ID] = @ID
GO
创建model:
下面是本篇重点部分,是将DataTable转换为List<T>或者DataRow转换为T:
创建Entity:
创建控制的操作:
下面创建2个视图:
另一个视图:
演示,演示中所选择图片来源于Insus.NET的微博(新浪,腾讯)和QQ空间:
http://weibo.com/104325017
http://t.qq.com/leo_insus
http://user.qzone.qq.com/104325017/main