当前位置: 首页 > news >正文

ASP.Net实现新闻添加查询(三层架构,含照片)

目录

演示功能:

点击启动生成页面

 点击搜索模糊查询

点击添加跳转新界面

​编辑

点击Button添加

步骤:

1、建文件

​编辑

2、添加引用关系

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

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

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

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

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

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

9、ui表现层添加界面前端部分

10、ui表现层添加界面后端部分


演示功能:

点击启动生成页面

 点击搜索模糊查询

点击添加跳转新界面

 此处设置文本框多行

点击Button添加

步骤:

1、建文件

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

建照片文件夹用于展示图片,数据库存地址 

2、添加引用关系

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

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Models
{public   class NewsModels{private string newsID;public string NewsID{get { return newsID; }set { newsID = value; }}private string newsTitle;public string NewsTitle{get { return newsTitle; }set { newsTitle = value; }}private string newsContent;public string NewsContent{get { return newsContent; }set { newsContent = value; }}private string type;public string Type{get { return type; }set { type = value; }}private string publisher;public string Publisher{get { return publisher; }set { publisher = value; }}private string pian;public string Pian{get { return pian; }set { pian = value; }}private string pubTime;public string PubTime{get { return pubTime; }set { pubTime = value; }}}
}

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

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=NewsDB;uid=sa;pwd=123123";public static SqlConnection conn = null;public static void Conncet() {if (conn==null){conn=new SqlConnection(connstr);}conn.Close();conn.Open();}public static bool NoQuery(string sql) {Conncet();SqlCommand cmd = new SqlCommand(sql,conn);int temp=  cmd.ExecuteNonQuery();conn.Close();return temp > 0;}public static SqlDataReader Reader(string sql) {Conncet();SqlCommand cmd = new SqlCommand(sql, conn);return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);}}
}

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace DAL
{public   class DalService{public static List<Models.NewsModels> Zhanshi() {List<Models.NewsModels> list = new List<Models.NewsModels>();string sql = "select * from News";SqlDataReader read=    DBHelper.Reader(sql);while (read.Read()){Models.NewsModels model=new Models.NewsModels();model.NewsID = read["NewsID"].ToString();model.NewsTitle = read["NewsTitle"].ToString();model.Pian = read["Pian"].ToString();model.Publisher = read["Publisher"].ToString();model.Type = read["Type"].ToString();model.PubTime = read["PubTime"].ToString();model.NewsContent = read["NewsContent"].ToString();list.Add(model);}  return list;}public static List<Models.NewsModels>  Cha(string title){List<Models.NewsModels> list = new List<Models.NewsModels>();string sql = string.Format("select * from News where NewsTitle like '%{0}%'",title);SqlDataReader read = DBHelper.Reader(sql);while (read.Read()){Models.NewsModels model = new Models.NewsModels();model.NewsID = read["NewsID"].ToString();model.NewsTitle = read["NewsTitle"].ToString();model.Pian = read["Pian"].ToString();model.Publisher = read["Publisher"].ToString();model.Type = read["Type"].ToString();model.PubTime = read["PubTime"].ToString();model.NewsContent = read["NewsContent"].ToString();list.Add(model);}return list;}public static bool  Jia(string title,string content,string type,string publisher){string sql=string.Format("insert News values('{0}','{1}','{2}','{3}','Jellyfish.jpg',GETDATE())",title,content,type,publisher);if (DBHelper.NoQuery(sql)){return true;}else{return false;}}}
}

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

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.NewsModels> Zhanshi() {return DAL.DalService.Zhanshi(); }public static bool Jia(string title, string content, string type, string publisher) {return DAL.DalService.Jia(title,content,type,publisher);}public static List<Models.NewsModels> Cha(string title) {return DAL.DalService.Cha(title);}}
}

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

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="Index.Index" %><!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><a href="TianJia.aspx">添加新闻</a><br />    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Button ID="Button1" runat="server" Text="查询" OnClick="Button1_Click" /><asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand"><HeaderTemplate><table border="1" ><tr><th>新闻ID</th><th>新闻标题	</th> <th>新闻内容	</th> <th>新闻照片</th><th>新闻类型	</th> <th>发布人</th> <th>发布时间</th></tr></HeaderTemplate><ItemTemplate><tr><td ><%#Eval("NewsID") %></td><td ><%#Eval("NewsTitle") %>                      </td><td >                     <%#Eval("NewsContent") %>                     </td><td >                        <img src="Img/<%#Eval("Pian") %>" height="50px" width="50px"/>                      </td><td><%#Eval("Type") %>  </td><td >                      <%#Eval("Publisher") %>                       </td>                    <td ><%#Eval("PubTime") %></td></tr></ItemTemplate><FooterTemplate></table></FooterTemplate></asp:Repeater></div></form>
</body>
</html>

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

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 Index : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){List<Models.NewsModels> list = BLL.BllManager.Zhanshi();Repeater1.DataSource = list;Repeater1.DataBind();}}protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e){}protected void Button1_Click(object sender, EventArgs e){List<Models.NewsModels> list = BLL.BllManager.Cha(TextBox1.Text.ToString());Repeater1.DataSource = list;Repeater1.DataBind();}}
}

9、ui表现层添加界面前端部分

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TianJia.aspx.cs" Inherits="Index.TianJia" %><!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><a href="Index.aspx">返回</a><br />     <asp:Label ID="Label1" runat="server" Text="标题"></asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="标题不能为空" ControlToValidate="TextBox1"></asp:RequiredFieldValidator><br />           <asp:Label ID="Label2" runat="server" Text="内容"></asp:Label><asp:TextBox Height="100px" ID="TextBox2" runat="server" TextMode="MultiLine"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="内容不能为空" ControlToValidate="TextBox2"></asp:RequiredFieldValidator><br />     <asp:Label ID="Label3" runat="server" Text="类型"></asp:Label><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="类型不能为空" ControlToValidate="TextBox3"></asp:RequiredFieldValidator><br />     <asp:Label ID="Label4" runat="server" Text="发布人"></asp:Label><asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="发布人不能为空" ControlToValidate="TextBox4"></asp:RequiredFieldValidator><br />         <asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" /></div></form>
</body>
</html>

10、ui表现层添加界面后端部分

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 TianJia : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;}protected void Button1_Click(object sender, EventArgs e){if (BLL.BllManager.Jia(TextBox1.Text.ToString(), TextBox2.Text.ToString(), TextBox3.Text.ToString(), TextBox4.Text.ToString())){ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('成功添加');location.href='Index.aspx'",true);}else{ClientScript.RegisterStartupScript(this.GetType(), "fail", "alert('失败');", true);};}}
}

相关文章:

  • react使用useState更新数组失败
  • 关于“Python”的核心知识点整理大全44
  • 数据探查系列:如何进行有意义的探索性数据分析(EDA)
  • 『JavaScript』JavaScript事件类型详解:全面解析各类用户交互行为
  • Ubuntu中fdisk磁盘分区并挂载、扩容逻辑卷
  • mapboxgl 中热力图的实现以及给热力图点增加鼠标移上 popup 效果
  • 【云原生】Kubernetes Operator模式
  • 基于飞浆OCR的文本框box及坐标中心点检测JSON格式保存文本
  • 系列十(实战)、发送 接收批量消息(Java操作RocketMQ)
  • 图像处理-周期噪声
  • 云计算:OpenStack 配置二层物理网卡为三层桥的接口
  • 文件监控-IT安全管理软件
  • 鸿蒙(HarmonyOS)项目方舟框架(ArkUI)之线性布局容器Row组件
  • OpenHarmony城市技术论坛武汉站:探索大模型时代的终端操作系统创新
  • 二叉树的非递归遍历|前中后序遍历
  • 《Java8实战》-第四章读书笔记(引入流Stream)
  • CSS盒模型深入
  • javascript 总结(常用工具类的封装)
  • JS学习笔记——闭包
  • Python利用正则抓取网页内容保存到本地
  • Python连接Oracle
  • Sass 快速入门教程
  • Storybook 5.0正式发布:有史以来变化最大的版本\n
  • vue.js框架原理浅析
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 记一次删除Git记录中的大文件的过程
  • 名企6年Java程序员的工作总结,写给在迷茫中的你!
  • 模仿 Go Sort 排序接口实现的自定义排序
  • 用Canvas画一棵二叉树
  • 用Python写一份独特的元宵节祝福
  • 优秀架构师必须掌握的架构思维
  • 《TCP IP 详解卷1:协议》阅读笔记 - 第六章
  • 宾利慕尚创始人典藏版国内首秀,2025年前实现全系车型电动化 | 2019上海车展 ...
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • ​ 无限可能性的探索:Amazon Lightsail轻量应用服务器引领数字化时代创新发展
  • ​【C语言】长篇详解,字符系列篇3-----strstr,strtok,strerror字符串函数的使用【图文详解​】
  • ​如何在iOS手机上查看应用日志
  • #define
  • #pragam once 和 #ifndef 预编译头
  • #pragma pack(1)
  • (1/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (12)目标检测_SSD基于pytorch搭建代码
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (个人笔记质量不佳)SQL 左连接、右连接、内连接的区别
  • (六)vue-router+UI组件库
  • (七)c52学习之旅-中断
  • (四)JPA - JQPL 实现增删改查
  • (完整代码)R语言中利用SVM-RFE机器学习算法筛选关键因子
  • (转)机器学习的数学基础(1)--Dirichlet分布
  • .babyk勒索病毒解析:恶意更新如何威胁您的数据安全
  • .NET Core、DNX、DNU、DNVM、MVC6学习资料
  • .NET Core日志内容详解,详解不同日志级别的区别和有关日志记录的实用工具和第三方库详解与示例
  • .net oracle 连接超时_Mysql连接数据库异常汇总【必收藏】
  • .net web项目 调用webService
  • .NET 中 GetHashCode 的哈希值有多大概率会相同(哈希碰撞)