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

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

 

 

0.新建web网站项目

1.创建分层文件

dal:数据访问层

BLL:业务逻辑层

型号:模型层

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

  
    
  

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
    {
        /// 
        /// 获取用户列表
        /// 
        /// 
        public List Getlist()
        {
            string sql = "select * from UserInfo";
            DataTable da= SqlHelper.GetDataTable(sql, CommandType.Text);
            List list = null;
            if (da.Rows.Count > 0)
            {
                list = new List();
                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 GetList()
        {
            return UserInfoDal.Getlist();   
        }
    }
}

 

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




    
    
    
    



    
        @tbody
    
编号用户名密码邮箱删除编辑

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

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

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

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            BLL.UserInfoService userInfoService = new BLL.UserInfoService();
            Listlist= userInfoService.GetList();
            StringBuilder sb = new StringBuilder();
            foreach (UserInfo userInfo in list)
            {
                sb.AppendFormat("{0}{1}{2}{3}",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;
            }
        }
    }
}

 

 

 

 

 

你可能感兴趣的:(C#)