ASP.Net 三层架构简单例子(绑定objectdatasource实现)(数据增删查改)

ASP.Net 三层架构简单例子(绑定objectdatasource实现)(数据增删查改)

第一次写文章,有啥说不明白的见谅哈
三层架构:
(1)表示层(UI层):是表现层,主要是用来展示数据,我的例子是用web展示数据。
(2)业务逻辑层(BLL层):是处理层,主要是用来处理数据,也就是封装一个处理数据的方法,处理数据的具体操作是在DAL层。
(3)数据访问层(DAL层):主要工作是连数据库,获取数据库的数据,编写增删查改的操作。
还有个Model层是用来封装某个数据库的表的定义变量,DAL层引用的时候方便。
它们之间的引用关系
在这里插入图片描述
简单的三层架构例子是在visual studio里实现的,其中Model层DAL层BLL层都对应一个**.CS文件**,UI层对应**.aspx文件**,也就是一个web文件。
首先新建个web项目,在新项目里新建三个文件夹分别代表model,dal,bll层,在每个文件夹新建类文件相应的写类文件(cs文件)。
每个层互相引用的时候 using xxx就可以引用了。
在这里插入图片描述
下面放一个简单例子的代码:

1、Model层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace myWork_stray.Model
{
    public class user
    {
        private int id;
        private string email;
        private string password;
        public user(){}
        public int Id 
        {
            get { return id; }
            set { id = value; }
        }
            public string Email
        {
            get { return email; }
            set { email = value; }
        }
        public string Password
        {
            get { return password; }
            set { password = value; }
        }
    }
}

2、DAL层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using myWork_stray.Model;//引用Model层
namespace myWork_stray.DAL
{
    public class DBhelper//连接数据库,我是用sqlserver的数据库
    {
        private static string connString = "Data Source=USER-20191212UN\\SQLEXPRESS;Initial Catalog=network;Integrated Security=True";
        public static SqlConnection connection = new SqlConnection(connString);


    }

    public class userService
    {
        public userService() { }
        public static List<user> Selectuser()//查
        {
            SqlConnection conn = DBhelper.connection; 
            List<user> ls = new List<user>();
           
           // try { 
                if (conn.State == System.Data.ConnectionState.Closed)
                {
                    conn.Open();
                }
                else
                {
                    conn.Close();
                    conn.Open();
                }
                string sqlString = "select * from user1";
                SqlCommand cmd = new SqlCommand(sqlString, conn);
                SqlDataAdapter sa = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                sa.Fill(ds);
                DataTable table = ds.Tables[0];
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    user u = new user();
                    DataRow row = table.Rows[i];
                    u.Id = Convert.ToInt32(row["id"]);
                    u.Email = row["email"].ToString();
                    u.Password = row["password"].ToString();
                    ls.Add(u);
                }
                conn.Close();
          //  }
           // catch (Exception e)
           // {

           // }
            return ls;
            

        }
        public static void Adduser(user u)//增
        {
            SqlConnection conn = DBhelper.connection;
            string sqlString = "insert into user1(id,email,password) values(@id,@email,@password);";
            SqlCommand cmd = new SqlCommand(sqlString, conn);
            cmd.Parameters.Add(new SqlParameter("@id", u.Id));
            cmd.Parameters.Add(new SqlParameter("@email", u.Email));
            cmd.Parameters.Add(new SqlParameter("@password", u.Password));
            if (conn.State == System.Data.ConnectionState.Closed)
            {
                conn.Open();
            }
            else
            {
                conn.Close();
                conn.Open();
            }
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        public static void Updateuser(user u)//改
        {
            SqlConnection conn = DBhelper.connection;
            string sqlString = "update user1 set id=@id,email=@email,password=@password where id=@id;";
            SqlCommand cmd = new SqlCommand(sqlString, conn);
            cmd.Parameters.Add(new SqlParameter("@id", u.Id));
            cmd.Parameters.Add(new SqlParameter("@email", u.Email));
            cmd.Parameters.Add(new SqlParameter("@password", u.Password));
            if (conn.State == System.Data.ConnectionState.Closed)
            {
                conn.Open();
            }
            else
            {
                conn.Close();
                conn.Open();
            }
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        public static void Deleteuserbyid(int id)//删
        {
            
            SqlConnection conn = DBhelper.connection;
            string sqlString = "DELETE user1 WHERE id= @id";
            SqlCommand cmd = new SqlCommand(sqlString, conn);
            cmd.Parameters.Add(new SqlParameter("@id", id));           
            if (conn.State == System.Data.ConnectionState.Closed)
            {
                conn.Open();
            }
            else
            {
                conn.Close();
                conn.Open();
            }
            cmd.ExecuteNonQuery();
            conn.Close();
        }
       
    }
}

3、BLL层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using myWork_stray.Model;//引用Model层
using myWork_stray.DAL;//引用DAL层
namespace myWork_stray.BLL
{
    public class user_BLL
    {
        public user_BLL()
        {
        }
        public static List<user> Selectuser()
        {
            return userService.Selectuser();
        }
        public static void Adduser(user u)
        {
            userService.Adduser(u);
        }
        public static void Updateuser(user u)
        {
            userService.Updateuser(u);
        }
        public static void Deleteuser(user u)
        {
            userService.Deleteuserbyid(u.Id);
        }
    }
}

我的增删查改里面增查改都没有任何问题,就是删里面它总是空值不知道是因为什么,但是删特定的列又是可以的,有大佬知道是因为啥留言告诉我哈

 public static void Deleteuser(user u)
        {
            userService.Deleteuserbyid(3);//特定的列时可以删除
        }

4、 UI层(aspx文件)

新建aspx文件后你可以从工具箱手动拖个objectdatasource控件,绑定到你自己写的BLL,如果绑定的时候找不到自己写的BLL,可能是因为你没运行整个程序,运行一下整个程序再看看有没有。
选择自己写的BLL:
选择BLL
点下一步 选一下增删查改的方法
在这里插入图片描述然后点完成就绑定了,然后再拖个listview绑到objectdatasource上,运行aspx文件就可以看到数据了。
下面是aspx代码部分

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="user.aspx.cs" Inherits="myWork_stray.Backstage.user" %>

<!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>
        </div>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DataObjectTypeName="myWork_stray.Model.user" DeleteMethod="Deleteuser" InsertMethod="Adduser" SelectMethod="Selectuser" TypeName="myWork_stray.BLL.user_BLL" UpdateMethod="Updateuser"></asp:ObjectDataSource>
        <asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1" InsertItemPosition="LastItem">
            <AlternatingItemTemplate>
                <tr style="background-color:#FFFFFF; color: #284775;">
                    <td>
                        <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />
                        <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
                    </td>
                    <td>
                        <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
                    </td>
                    <td>
                        <asp:Label ID="EmailLabel" runat="server" Text='<%# Eval("Email") %>' />
                    </td>
                    <td>
                        <asp:Label ID="PasswordLabel" runat="server" Text='<%# Eval("Password") %>' />
                    </td>
                </tr>
            </AlternatingItemTemplate>
            <EditItemTemplate>
                <tr style="background-color:#999999;">
                    <td>
                        <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />
                    </td>
                    <td>
                        <asp:TextBox ID="IdTextBox" runat="server" Text='<%# Bind("Id") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="PasswordTextBox" runat="server" Text='<%# Bind("Password") %>' />
                    </td>
                </tr>
            </EditItemTemplate>
            <EmptyDataTemplate>
                <table runat="server" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
                    <tr>
                        <td>未返回数据。</td>
                    </tr>
                </table>
            </EmptyDataTemplate>
            <InsertItemTemplate>
                <tr style="">
                    <td>
                        <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />
                    </td>
                    <td>
                        <asp:TextBox ID="IdTextBox" runat="server" Text='<%# Bind("Id") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="PasswordTextBox" runat="server" Text='<%# Bind("Password") %>' />
                    </td>
                </tr>
            </InsertItemTemplate>
            <ItemTemplate>
                <tr style="background-color:#E0FFFF; color: #333333;">
                    <td>
                        <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />
                        <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
                    </td>
                    <td>
                        <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
                    </td>
                    <td>
                        <asp:Label ID="EmailLabel" runat="server" Text='<%# Eval("Email") %>' />
                    </td>
                    <td>
                        <asp:Label ID="PasswordLabel" runat="server" Text='<%# Eval("Password") %>' />
                    </td>
                </tr>
            </ItemTemplate>
            <LayoutTemplate>
                <table runat="server">
                    <tr runat="server">
                        <td runat="server">
                            <table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                                <tr runat="server" style="background-color:#E0FFFF; color: #333333;">
                                    <th runat="server"></th>
                                    <th runat="server">用户编号</th>
                                    <th runat="server">用户邮箱</th>
                                    <th runat="server">用户密码</th>
                                </tr>
                                <tr id="itemPlaceholder" runat="server">
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr runat="server">
                        <td runat="server" style="text-align: center;background-color: #5D7B9D; font-family: Verdana, Arial, Helvetica, sans-serif;color: #FFFFFF;">
                            <asp:DataPager ID="DataPager1" runat="server">
                                <Fields>
                                    <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
                                </Fields>
                            </asp:DataPager>
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
            <SelectedItemTemplate>
                <tr style="background-color:#E2DED6; font-weight: bold;color: #333333;">
                    <td>
                        <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />
                        <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
                    </td>
                    <td>
                        <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
                    </td>
                    <td>
                        <asp:Label ID="EmailLabel" runat="server" Text='<%# Eval("Email") %>' />
                    </td>
                    <td>
                        <asp:Label ID="PasswordLabel" runat="server" Text='<%# Eval("Password") %>' />
                    </td>
                </tr>
            </SelectedItemTemplate>
        </asp:ListView>
        <br />
    </form>
</body>
</html>

一个简单的三层架构就实现啦,欢迎初学者参考借鉴。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/su_1998/article/details/106659550
今日推荐