ASP.Net实现海鲜添加(三层架构,异常处理)

2024-01-04 07:20

本文主要是介绍ASP.Net实现海鲜添加(三层架构,异常处理),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

演示功能:

点击启动生成页面

点击添加跳转新界面

 此处设置文本框多行

点击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 SeaFoodTypeModel{private string infoTitle;private string infoID;public string InfoID{get { return infoID; }set { infoID = value; }}private string pubTime;public string PubTime{get { return pubTime; }set { pubTime = value; }}public string InfoTitle
{get { return infoTitle; }set { infoTitle = value; }
}private string infoPic;public string InfoPic
{get { return infoPic; }set { infoPic = value; }
}private string type;public string Type
{get { return type; }set { type = value; }
}private string infoDetail;public string InfoDetail
{get { return infoDetail; }set { infoDetail = value; }
}
private string price;public string Price
{get { return price; }set { price = 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=SeaFoodDB;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) {try{Connect();SqlCommand cmd = new SqlCommand(sql,conn);int temp=     cmd.ExecuteNonQuery();conn.Close();return temp > 0;}catch (Exception ex){return false;    }}public static SqlDataReader Reader(string sql){Connect();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.SeaFoodTypeModel> Zhanshi() {List<Models.SeaFoodTypeModel> list = new List<Models.SeaFoodTypeModel>();string sql = "select * from SeaFoodInfo";SqlDataReader read=   Dal.DBHelper.Reader(sql);while (read.Read()){Models.SeaFoodTypeModel model = new Models.SeaFoodTypeModel();model.InfoID = read["InfoID"].ToString();model.PubTime = read["PubTime"].ToString();model.Type = read["Type"].ToString();model.Price = read["Price"].ToString();model.InfoPic = read["InfoPic"].ToString();model.InfoTitle = read["InfoTitle"].ToString();model.InfoDetail = read["InfoDetail"].ToString();list.Add(model);}return list;}public static bool Tianjia(string InfoTitle, string TypeName, string InfoDetail, string Price){string sql = string.Format("insert SeaFoodInfo values('{0}', 'yu.jpg', '{1}', '{2}', '{3}', GETDATE())", InfoTitle, TypeName, InfoDetail, Price);return DBHelper.NoQuery(sql);}}
}

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.SeaFoodTypeModel> Zhanshi() {return  Dal.DalService.Zhanshi();}public static bool Tianjia(string InfoTitle, string TypeName, string InfoDetail, string Price){return Dal.DalService.Tianjia( InfoTitle,  TypeName,  InfoDetail,  Price);}}
}

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

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.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><a href="Tianjia.aspx">添加海鲜</a><asp:Repeater ID="Repeater1" runat="server"><HeaderTemplate><table border="1"><tr><th>信息编号</th><th>信息标题</th><th>信息图片</th><th>海鲜类型</th><th>信息详情</th><th>价格</th><th>发布时间</th></tr></HeaderTemplate><ItemTemplate><tr><td><%#Eval("InfoID") %></td><td><%#Eval("InfoTitle") %></td><td><img  src="Images/<%#Eval("InfoPic") %>" width="60px" height="50px"/></td><td><%#Eval("Type") %></td><td><%#Eval("InfoDetail") %></td><td><%#Eval("Price") %></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 WebApplication2
{public partial class WebForm1 : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){this.Repeater1.DataSource = Bll.BllManager.Zhanshi();this.Repeater1.DataBind();}}
}

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

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Tianjia.aspx.cs" Inherits="WebApplication2.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="WebForm1.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 ID="TextBox2" runat="server"></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" style="margin-left: 31px"></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 WebApplication2
{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.Tianjia(TextBox1.Text.ToString(), TextBox2.Text.ToString(), TextBox3.Text.ToString(), TextBox4.Text.ToString())){ClientScript.RegisterStartupScript(GetType(),"success", "alert('正确!');location.href='WebForm1.aspx'",true);}else{ClientScript.RegisterStartupScript(GetType(), "success", "alert('错误!')", true);};}}
}

这篇关于ASP.Net实现海鲜添加(三层架构,异常处理)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/568571

相关文章

Spring Security+JWT如何实现前后端分离权限控制

《SpringSecurity+JWT如何实现前后端分离权限控制》本篇将手把手教你用SpringSecurity+JWT搭建一套完整的登录认证与权限控制体系,具有很好的参考价值,希望对大家... 目录Spring Security+JWT实现前后端分离权限控制实战一、为什么要用 JWT?二、JWT 基本结构

Java实现优雅日期处理的方案详解

《Java实现优雅日期处理的方案详解》在我们的日常工作中,需要经常处理各种格式,各种类似的的日期或者时间,下面我们就来看看如何使用java处理这样的日期问题吧,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言一、日期的坑1.1 日期格式化陷阱1.2 时区转换二、优雅方案的进阶之路2.1 线程安全重构2

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

使用Python实现图像LBP特征提取的操作方法

《使用Python实现图像LBP特征提取的操作方法》LBP特征叫做局部二值模式,常用于纹理特征提取,并在纹理分类中具有较强的区分能力,本文给大家介绍了如何使用Python实现图像LBP特征提取的操作方... 目录一、LBP特征介绍二、LBP特征描述三、一些改进版本的LBP1.圆形LBP算子2.旋转不变的LB

Redis消息队列实现异步秒杀功能

《Redis消息队列实现异步秒杀功能》在高并发场景下,为了提高秒杀业务的性能,可将部分工作交给Redis处理,并通过异步方式执行,Redis提供了多种数据结构来实现消息队列,总结三种,本文详细介绍Re... 目录1 Redis消息队列1.1 List 结构1.2 Pub/Sub 模式1.3 Stream 结

C# Where 泛型约束的实现

《C#Where泛型约束的实现》本文主要介绍了C#Where泛型约束的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录使用的对象约束分类where T : structwhere T : classwhere T : ne

将Java程序打包成EXE文件的实现方式

《将Java程序打包成EXE文件的实现方式》:本文主要介绍将Java程序打包成EXE文件的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录如何将Java程序编程打包成EXE文件1.准备Java程序2.生成JAR包3.选择并安装打包工具4.配置Launch4

MySQL索引的优化之LIKE模糊查询功能实现

《MySQL索引的优化之LIKE模糊查询功能实现》:本文主要介绍MySQL索引的优化之LIKE模糊查询功能实现,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前缀匹配优化二、后缀匹配优化三、中间匹配优化四、覆盖索引优化五、减少查询范围六、避免通配符开头七、使用外部搜索引擎八、分

Python实现特殊字符判断并去掉非字母和数字的特殊字符

《Python实现特殊字符判断并去掉非字母和数字的特殊字符》在Python中,可以通过多种方法来判断字符串中是否包含非字母、数字的特殊字符,并将这些特殊字符去掉,本文为大家整理了一些常用的,希望对大家... 目录1. 使用正则表达式判断字符串中是否包含特殊字符去掉字符串中的特殊字符2. 使用 str.isa

Spring Boot 集成 Quartz并使用Cron 表达式实现定时任务

《SpringBoot集成Quartz并使用Cron表达式实现定时任务》本篇文章介绍了如何在SpringBoot中集成Quartz进行定时任务调度,并通过Cron表达式控制任务... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启动 Sprin