C# Web 网站三层结构(1)

跟着传智播客在B站上的视频照着做的

 

 

0.新建web网站项目

1.创建分层文件

dal:数据访问层

BLL:业务逻辑层

型号:模型层

2.在配置文件中添加数据库信息

  <connectionStrings>
    <add name="connstr" connectionString="server=.;uid=sa;pwd=123;database=MMFDB"/>
  </connectionStrings>

3.在Model层中定义实体类UserInfo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    public class UserInfo
    {
        public int Id { get ; set; }
        public string Name { get ; set; }
        public int Password { get ; set; }
         
        public string Email { get; set; }

    }
}

3.5.Dal引用模型层

4.Dal层创建一个提供SQLHelper类文件写SQL语句

使用:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

1.添加引用配置,获取数据库信息

 private static readonly string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;

2.封装一个基本查询方法,返回表类型

 public static DataTable GetDataTable(string sql, CommandType type, params SqlParameter[] pars)
        {
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                using (SqlDataAdapter apter = new SqlDataAdapter(sql, conn))
                {
                    if (pars != null)
                    {
                        apter.SelectCommand.Parameters.AddRange(pars);

                    }
                    apter.SelectCommand.CommandType = type;
                    DataTable da = new DataTable();
                    apter.Fill(da);
                    return da;
                }
            }
        }

 

     3.封装一个受影响行数方法

public static int ExecuteNonquery(string sql,CommandType type,params SqlParameter[]pars)
        {
            using (SqlConnection conn=new SqlConnection(connstr))
            {
                using (SqlCommand cmd=new SqlCommand(sql,conn))
                {
                    if (pars!=null)
                    {
                        cmd.Parameters.AddRange(pars);
                    }
                    cmd.CommandType = type;
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        } 

  5.在Dal层创建一个UserInfo数据操作类,针对UserInfo表

using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class UserInfoDal
    {
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns></returns>
        public List<UserInfo> Getlist()
        {
            string sql = "select * from UserInfo";
            DataTable da= SqlHelper.GetDataTable(sql, CommandType.Text);
            List<UserInfo> list = null;
            if (da.Rows.Count > 0)
            {
                list = new List<UserInfo>();
                UserInfo userinfo = null;
                foreach ( DataTable  row in da.Rows)
                {
                    userinfo = new UserInfo();
                    LoadEntity(userinfo,row)
                    list.Add(userinfo);

                }
            }
            return list;
        }
        private void LoadEntity(UserInfo userinfo, DataRow row)
        {
            userinfo.Name = row["Name"] != DBNull.Value ? row["Name"].ToString() : string.Empty;
            userinfo.Password= row["Password"] != DBNull.Value ? Convert.ToInt32( row["Password"]) :123;
            userinfo.Email= row["Email"] != DBNull.Value ? row["Email"].ToString() : string.Empty;
            userinfo.Id = Convert.ToInt32(row["id"]);
        }
    }
}

6.在BLL业务层中添加一个针对的UserInfo的数据操作类UserInfoService 

添加对型号和德尔的引用

using Model;
using DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class UserInfoService
    {
        UserInfoDal UserInfoDal = new UserInfoDal();
        public List<UserInfo> GetList()
        {
            return UserInfoDal.Getlist();   
        }
    }
}

 

7.在网站项目中新建的HTML模板文件,

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <link href="Content/bootstrap.min.css" rel="stylesheet" />
    <script src="Scripts/bootstrap.min.js"></script>

</head>
<body>
    <table>
        <tr><th>编号</th><th>用户名</th><th>密码</th><th>邮箱</th><th>删除</th><th>编辑</th></tr>
        @tbody
    </table>
</body>
</html>

8.新建一般处理程序进行整合

using Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace WebApplication2
{
    /// <summary>
    /// UserInfoShow 的摘要说明
    /// </summary>
    public class UserInfoShow : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            BLL.UserInfoService userInfoService = new BLL.UserInfoService();
            List<UserInfo>list= userInfoService.GetList();
            StringBuilder sb = new StringBuilder();
            foreach (UserInfo userInfo in list)
            {
                sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>",userInfo.Id,userInfo.Name,userInfo.Password,userInfo.Email);
            }
            string filepath = context.Request.MapPath("UserInfoList.html");
            string fileContent = File.ReadAllText(filepath);
            fileContent= fileContent.Replace("@tbody",sb.ToString());
            context.Response.Write(fileContent);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

 

 

 

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值