欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net三层架构增删改查

程序员文章站 2022-03-25 13:33:06
数据库 链接数据库Web.config Model层managementModel类 DAL层添加引用 Model层添加程序集引用 using System.Configuration;managementDAL类 DBHelper类 BLL层添加引用 Model层添加引用 DAL层 UI 层添加引 ......

数据库

use master
if exists (select * from sysdatabases where name='bond')
drop database bond
create database bond
on primary
(
name='bond_data',
filename='f:\asp\理财代销\management\bond.mdf',
filegrowth=20%,
size=10mb
)
log on
(
name='bond_log',
filename='f:\asp\理财代销\management\bond_log.ldf',
size=3mb,
maxsize=20mb
)




use bond
--基金类型表(左用)
if exists (select * from sys.objects where name='jjlx')
drop table jjlx
create table jjlx
(
id int primary key identity(1,1),                     --id
jjlx varchar(50) not null                            --基金类型
)

--基金类型表增加存储过程
if exists(select * from sys.objects where name='jjlx_add')
drop procedure jjlx_add
go
create proc jjlx_add
@jjlx varchar(50) 
as
insert into jjlx values (@jjlx)
go
--基金类型表查询存储过程
if exists(select * from sys.objects where name='p_jjlx')
drop procedure p_jjlx
go
create proc p_jjlx
as
select * from jjlx
go
--基金类型表修改存储过程
if exists(select * from sys.objects where name='jjlx_gai')
drop procedure jjlx_gai
go
create proc jjlx_gai
@id int,
@jjlx varchar(50)
as
update jjlx set jjlx=@jjlx where  id=@id 
go
--基金类型表删除存储过程
if exists(select * from sys.objects where name='jjlx_delete')
drop procedure jjlx_delete
go
create proc jjlx_delete
@id int,
@jjlx varchar(50)
as
delete from jjlx where id=@id and jjlx=@jjlx
go

链接数据库
web.config

<connectionstrings>
  <add name="conn" connectionstring="server=.;database=bond;integrated security=true" />
 </connectionstrings>

model层
managementmodel类

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;

namespace managementmodel
{
    public class jjlxs//基金类型表
    {
        public int id { set; get; }//id
        public string jjlx { set; get; } //基金类型

    }
}

dal层
添加引用 model层
添加程序集引用 using system.configuration;
managementdal类

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using system.data.sqlclient;
using system.data;
using managementmodel;
namespace managementdal
{
    public class jjlxdal
    {
        dbhelper db = new dbhelper();
        /// <summary>
        /// 查询基金类型
        /// </summary>
        /// <returns></returns>
        public dataset searchjjlx()
        {
            string sql = "p_jjlx";
            return db.search(sql);
        }
        /// <summary>
        /// 增加基金类型
        /// </summary>
        /// <param name="stu"></param>
        /// <returns></returns>
        public int insertjjlx(jjlxs stujjlx)
        {
            string sql = "jjlx_add";
            sqlparameter[] para ={
                           new sqlparameter("@jjlx",stujjlx.jjlx)   
                           };
            return db.iud(sql, para);
        }
        /// <summary>
        /// 修改基金类型
        /// </summary>
        /// <param name="stu"></param>
        /// <returns></returns>
        public int udatejjlx(jjlxs stujjlx)
        {
            string sql = "jjlx_gai";
            sqlparameter[] para ={
                           new sqlparameter("@id",stujjlx.id),
                           new sqlparameter("@jjlx",stujjlx.jjlx)
                           };
            return db.iud(sql, para);
        }
        /// <summary>
        /// 删除基金类型
        /// </summary>
        /// <param name="stu"></param>
        /// <returns></returns>
        public int deletejjlx(jjlxs stujjlx)
        {
            string sql = "jjlx_delete";
            sqlparameter[] para ={
                          new sqlparameter("@id",stujjlx.id),
                           new sqlparameter("@jjlx",stujjlx.jjlx)
                           };
            return db.iud(sql, para);
        }
    }
}

dbhelper类

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using system.data;
using system.data.sqlclient;
using system.configuration;
namespace managementdal
{
    public class dbhelper
    {
        public static string conn = configurationmanager.connectionstrings["conn"].tostring();
        /// <summary>
        /// 增删改的方法
        /// </summary>
        /// <param name="sql">增删改的存储过程</param>
        /// <param name="param">存储过程使用的参数</param>
        /// <returns></returns>
        public int iud(string sql, sqlparameter[] param)
        {
            int count = 0;
            sqlconnection con = new sqlconnection(conn);
            con.open();
            sqlcommand com = new sqlcommand(sql, con);
            com.commandtype = commandtype.storedprocedure;
            com.parameters.addrange(param);
            count = com.executenonquery();
            con.close();
            return count;
        }
        /// <summary>
        /// 查询返回dataset
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public dataset search(string sql)
        {
            dataset ds = new dataset();
            sqlconnection con = new sqlconnection(conn);
            sqldataadapter adapter = new sqldataadapter(sql, con);
            adapter.fill(ds);
            return ds;
        }
      
    }
}

bll层
添加引用 model层
添加引用 dal层

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using managementdal;
using managementmodel;
using system.data;
namespace managementbll
{
   public  class jjlxbll
    {
       jjlxdal dal = new jjlxdal();
       /// <summary>
       /// 查询基金类型
       /// </summary>
       /// <returns></returns>
       public dataset searchjjlx() {
           return dal.searchjjlx();
       }
       /// <summary>
       /// 增加基金类型
       /// </summary>
       /// <param name="stu"></param>
       /// <returns></returns>
       public bool insertjjlx(jjlxs stujjlx)
       {
           bool flag = false;
           if (stujjlx.jjlx.length != 0)
           {
               int count = dal.insertjjlx(stujjlx);
               if (count > 0)
               {
                   flag = true;
               }
           }
           return flag;
       }
       /// <summary>
       /// 修改基金类型
       /// </summary>
       /// <param name="stujjlx"></param>
       /// <returns></returns>
       public bool udatejjlx(jjlxs stujjlx)
       {
           bool flag = false;
           if (stujjlx.jjlx.length != 0&&stujjlx.id!=0)
           {
               int count = dal.udatejjlx(stujjlx);
               if (count > 0)
               {
                   flag = true;
               }
           }
           return flag;
       }
       /// <summary>
       /// 删除基金类型
       /// </summary>
       /// <param name="stujjlx"></param>
       /// <returns></returns>
       public bool deletejjlx(jjlxs stujjlx)
       {
           bool flag = false;
           if (stujjlx.jjlx.length != 0 && stujjlx.id != 0)
           {
               int count = dal.deletejjlx(stujjlx);
               if (count > 0)
               {
                   flag = true;
               }
           }
           return flag;
       }
    }
}

ui 层
添加引用 model层
添加引用 bll层
基金类型.aspx

<%@ page language="c#" autoeventwireup="true" codebehind="基金类型表.aspx.cs" inherits="management.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">
        <asp:label id="label2" runat="server" text="类型id:"></asp:label>
&nbsp;
        <asp:textbox id="txtid" runat="server"></asp:textbox>
    <div>
    
    </div>
        <asp:label id="label1" runat="server" text="基金类型:"></asp:label>
        <asp:textbox id="txtjjlx" runat="server"></asp:textbox>
        <br />
        <br />
        <asp:button id="btnadd" runat="server" onclick="btnadd_click" text="增加" />
        <asp:button id="btndelete" runat="server" onclick="btndelete_click" text="删除" />
        <asp:button id="btngai" runat="server" onclick="btngai_click" text="修改" />
        <br />
        <table border="1">
            <tr><th>类型id</th><th>基金类型</th></tr>
        <asp:repeater id="repjjlx" runat="server">
            <itemtemplate>
                <tr>
                    <td><%#eval("id") %></td>
                    <td><%#eval ("jjlx") %></td>
                </tr>
            </itemtemplate>
        </asp:repeater>
        </table>
    </form>
</body>
</html>

基金类型.aspx.cs
基金类型.aspx.cs

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols; 
using managementbll;
using system.data;
using managementmodel;
namespace management
{
    public partial class index : system.web.ui.page
    {
        jjlxbll bll = new jjlxbll();
        protected void page_load(object sender, eventargs e)
        {
            bind();
        }
        public void bind() {

            this.repjjlx.datasource = bll.searchjjlx().tables[0];
            this.repjjlx.databind();
        }
        protected void btnadd_click(object sender, eventargs e)
        {
            jjlxs stujjlx = new jjlxs {jjlx=txtjjlx.text };
            if (bll.insertjjlx(stujjlx))
            {
                bind();
                response.write("<script>alert('增加成功!')</script>");
            }
            else {
                response.write("<script>alert('增加失败!')</script>");
            }
        }

        protected void btndelete_click(object sender, eventargs e)
        {
            jjlxs stujjlx = new jjlxs();
            stujjlx.id = convert.toint32(txtid.text);
            stujjlx.jjlx = txtjjlx.text;
            if (bll.deletejjlx(stujjlx))
            {
                bind();
                response.write("<script>alert('删除成功!')</script>");
            }
            else
            {
                response.write("<script>alert('删除失败!')</script>");
            }
        }

        protected void btngai_click(object sender, eventargs e)
        {
            jjlxs stujjlx = new jjlxs();
            stujjlx.id = convert.toint32(txtid.text);
            stujjlx.jjlx = txtjjlx.text;
            if (bll.udatejjlx(stujjlx))
            {
                bind();
                response.write("<script>alert('修改成功!')</script>");
            }
            else
            {
                response.write("<script>alert('修改失败!')</script>");
            }
            
        }
    }
}