ASP.Net实现商品照片显示(三层架构)

演示功能:

点击启动生成页面

步骤:

1、建文件

下图是三层架构列表,Models里面有模拟数据库中列的类,DAL中有DBHelper和service,BLL中有BllManager文件用于ui界面直接调用,其中img文件用户储存照片,数据库中直接存照片地址

2、添加引用关系

DAL引用Models文件,BLL引用DAL和Models文件,主文件WebApplication1引用Bll和Models

3、根据数据库中的列写Models下的XueshengModels类

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models
{
   public  class NewModels
    {
        private string newID;

        public string NewID
        {
            get { return newID; }
            set { newID = value; }
        }
        private string newTitle;

        public string NewTitle
        {
            get { return newTitle; }
            set { newTitle = value; }
        }
        private string newsContent;

        public string NewsContent
        {
            get { return newsContent; }
            set { newsContent = value; }
        }
        private string typeType;

        public string TypeType
        {
            get { return typeType; }
            set { typeType = value; }
        }
        private string publisher;

        public string Publisher
        {
            get { return publisher; }
            set { publisher = value; }
        }
        private string pubTime;

        public string PubTime
        {
            get { return pubTime; }
            set { pubTime = value; }
        }
        private string imgUrl;

        public string ImgUrl
        {
            get { return imgUrl; }
            set { imgUrl = value; }
        }

    }
}

4、DAL下的DBHelper(对数据库进行操作)

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace DAL
{
   public class DBHelper
    {
       public static string connstr = "server=.;database=Info;uid=sa;pwd=123123";
       public static SqlConnection conn = null;
       public static void Connect() {
           if ( conn==null)
           {
               conn = new SqlConnection(connstr);
           }
           conn.Close();
           conn.Open();

       }
       public static bool NoQuery(string sql) {
           Connect();
           SqlCommand cmd = new SqlCommand(sql,conn);
        int temp=   cmd.ExecuteNonQuery();
           conn.Close();
           return temp>0;
       }
       public static SqlDataReader Reader(string sql)
       {
           Connect();
           SqlCommand cmd = new SqlCommand(sql, conn);
           return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
       }
   }
}

5、DAL数据访问层下的service文件

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace DAL
{
  public  class SeviceDAL
    {
      public static List<Models.NewModels> Query() {
          string sql = "select * from New";
         SqlDataReader reader= DBHelper.Reader(sql);
         List<Models.NewModels> list = new List<Models.NewModels>();
         while (reader.Read())
         {
           Models.NewModels model=new Models.NewModels();
           model.NewID = reader["NewID"].ToString();
           model.NewsContent = reader["NewsContent"].ToString();
           model.NewTitle = reader["NewTitle"].ToString();
           model.Publisher = reader["Publisher"].ToString();
           model.TypeType = reader["TypeType"].ToString();
           model.PubTime = reader["PubTime"].ToString();
           model.ImgUrl = reader["ImgUrl"].ToString();
           list.Add(model);

         }
         return list;


      }
    }
}

6、BLL业务逻辑层下调用DAL的文件

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bll
{
   public  class BllManager
    {
       public static List<Models.NewModels> Query() {
           return DAL.SeviceDAL.Query();
       }
    }
}

7、ui表现层主界面前端部分

cs 复制代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Index.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
                <table border="1">
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                     <td><%#Eval("NewID") %></td>
                         <td><%#Eval("NewTitle") %></td>
                     <td><%#Eval("NewsContent") %></td>
                     <td><img src='Img/<%#Eval("ImgUrl") %>' width="60px" height="50px"/></td>
                     <td><%#Eval("TypeType") %></td>
                     <td><%#Eval("Publisher") %></td>
                     <td><%#Eval("PubTime") %></td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

8、ui表现层主界面后端部分

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Index
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
      List<Models.NewModels> list=     Bll.BllManager.Query();
      this.Repeater1.DataSource = list;
      this.Repeater1.DataBind();
        }
    }
}
相关推荐
计算机安禾3 小时前
【数据库系统原理】第19篇:计算机存储层次结构与数据库文件的物理组织
数据库·oracle
JAVA面经实录9174 小时前
操作系统面试题
java·服务器·数据库·计算机网络·面试
摇滚侠4 小时前
mariadb-libs 被 mysql-community-libs-5.7.28-1.el7.x86_64 取代
数据库·mysql·mariadb
DIY源码阁5 小时前
JavaSwing饮品管理系统 - MySQL版
java·数据库·mysql·eclipse
专注搞钱5 小时前
GPT-4o写设备Recipe:从3小时到10分钟
数据库·人工智能·gpt·半导体
东风破1376 小时前
达梦数据库实战:备份恢复与数据迁移全攻略(实例初始化、服务注册、路径迁移)
数据库·chrome
SelectDB技术团队6 小时前
2026 SelectDB AI 产品发布会:Agent Native 数据基础设施能力全景发布
数据库·人工智能·agent·apache doris·selectdb
爱吃羊的老虎6 小时前
【数据库】模块一:数据库基础与关系代数
数据库
dishugj7 小时前
iSCSI + Multipath + ASM:Oracle RAC 共享存储技术链详解
数据库·oracle
yoothey7 小时前
MySQL事务机制解析 - 面试高分知识点
数据库·mysql·面试