一、创建空网站
二、为空网站添加类库和一般处理程序
1.为方案添加新建项目
(1)添加BLL、DAL、Model、Commo类库
(2)分别为类库添加MainInfoBLL.cs、MainInfoDAL.cs、MainInfo.csl类
(3)为DAL类库引入SqlHelper.cs 数据库交互工具类
2.为网站添加一般处理程序:MainList.ashx
设置配置文件web.config
<connectionStrings>
<add name="strConn" connectionString="server=.;pwd=123;uid=sa;database=bjhksj"/>
</connectionStrings>
三、编写代码:
1.主页面展示数据代码
(1)Model 模型
根据数据库Main表为Mode编辑MainInfo.cs模型()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Modle
{
public class MainInfo
{
//抽象字段:ID, title, content, type, Date, people, picUrl
public int ID { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string Type { get; set; }
public DateTime Date { get; set; }
public string People { get; set; }
public string PicUrl { get; set; }
}
}
(2)DAL数据访问层
DAL数据访问层的MainInfoDALcs中添加GetALLMainInfo()方法,查询Main表所有数据并返回LIst<>类型数据。
/// <summary>
/// 访问Main表的所有数据
/// </summary>
/// <returns></returns>
public List<MainInfo> GetALLMainInfo()
{
List<MainInfo> ls = new List<MainInfo>();
string sqlStr = @"Select
ID, title, content, type, Date, people, picUrl
from
[dbo].[HKSJ_Main]";
using (SqlDataReader sdr = SqlHelper.ExecuteReader(sqlStr, CommandType.Text))
{
if (sdr.HasRows)
{
while (sdr.Read())
{
MainInfo mi = new MainInfo();
mi.ID = sdr.GetInt32(0);
mi.Title = sdr.GetString(1);
mi.Content = sdr.GetString(2);
mi.Type = sdr.GetString(3);
mi.Date = sdr.GetDateTime(4);
mi.People = sdr.GetString(5);
mi.PicUrl = sdr.IsDBNull(6) ? null : sdr.GetString(6);
ls.Add(mi);
}
}
}
return ls;
}
(3)BLL业务逻辑处理层
BLL业务逻辑处理层的MainInfoBLL.cs中添加GetALLMainInfoBLL()方法,用于调用DAL层的GetALLMainInfoDal()方法进行业务逻辑处理。
private MainInfoDAL mainInfoDAL = new MainInfoDAL();
/// <summary>
///
/// </summary>
/// <returns></returns>
public List<MainInfo> GetALLMainInfoBLL()
{
return mainInfoDAL.GetALLMainInfoDal();
}
(4)UI展示层
1)html模板创建:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<table>
<tr>
<th>编号</th>
<th>标题</th>
<th>日期</th>
<th>创建人</th>
<th>操作</th>
</tr>
@trbody
</table>
</body>
</html>
2)编辑MainList.ashx中的ProcessRequest方法数据展示:
<%@ WebHandler Language="C#" Class="MainList" %>
using System;
using System.Web;
using BLL;
using Modle;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.IO;
public class MainList : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//context.Response.Write("Hello World");
BLL.MainInfoBLL mainInfoBLL = new MainInfoBLL();
List<MainInfo> ls = mainInfoBLL.GetALLMainInfoBLL(); //获得数据
StringBuilder sb = new StringBuilder();
foreach (var mainInfo in ls) //遍历获得的数据,拼接table表
{
sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td><a href='{0}'>详情</a></td></tr>",
mainInfo.ID,
mainInfo.Title,
mainInfo.Date,
mainInfo.People
);
}
//根据路径获得MainListTemp.html
string tempString = File.ReadAllText(context.Request.MapPath("MainListTemp.html"));
string result = tempString.Replace("@trbody", sb.ToString());
context.Response.Write(result);
}
public bool IsReusable
{
get
{
return false;
}
}
}