• 调用数据库 缓存解决


    写了个页面 由于每次调用都需要连接远程的服务器,特别浪费资源,创建了aspx

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="HotProduct.ascx.cs" Inherits="resource_ascx_HotProduct" %>
    <%@ OutputCache Duration="36000" VaryByParam="none" %>

    <div class="listProduct">
    <div class="listImagetop"></div>
    <div class="imgProduct">
    <asp:Literal ID="ltrMyResult" runat="server" />
    </div>
    <div class="listImageBottom"></div>
    </div>

    cs代码

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Data;
    using System.Text;


    public partial class resource_ascx_HotProduct : System.Web.UI.UserControl {
    protected void Page_Load(object sender, EventArgs e) {
    //获取图片
    using (SqlConnection con1 = new SqlConnection(@"数据连接")) {
    string strSql = string.Format("SELECT TOP 4 Id,name,Ext_ImageUrl1 FROM shop_Product WHERE (CategoryId = 36) ORDER BY CategoryId DESC");
    SqlCommand cmd = new SqlCommand(strSql, con1);
    cmd.Connection = con1;
    con1.Open();

    DataTable dtImg = new DataTable();
    SqlDataAdapter sma = new SqlDataAdapter(cmd);
    sma.Fill(dtImg);

    StringBuilder tp = new StringBuilder();

    for (int i = 0; i < dtImg.Rows.Count; i++) {
    tp.AppendFormat("<div class='imgProduct01'><a href='http://www.jiankang800.com/shop/show_{2}' target='_blank'><img src='http://www.jiankang800.com{1}' border=0 />{0}</a></div>", dtImg.Rows[i][1], dtImg.Rows[i][2], dtImg.Rows[i][0]);
    }
    ltrMyResult.Text = tp.ToString();
    }

    }
    }

    然后写在模板页的:

    <%@ Register Src="/resource/ascx/hotproduct.ascx" TagName="HotProduct" TagPrefix="anylen" %>   //注册

    <anylen:HotProduct runat="server" />// body部分

  • 相关阅读:
    lua module
    lua require
    lua io
    lua table2
    lua table1
    【leetcode】魔术排列
    【leetcode】速算机器人
    【leetcode】黑白方格画
    【leetcode】根据数字二进制下 1 的数目排序
    【leetcode】插入区间
  • 原文地址:https://www.cnblogs.com/cheshui/p/2385172.html
Copyright © 2020-2023  润新知