ASP.NET动态的网页增删查改

动态页面的增删查改,不多说了,直接上代码

跟前面的一般处理程序一样我上用的同一套三层,只是UI层的东西不一样,在纠结着要不要重新在上一次以前上过的代码:

纠结来纠结去,最后我觉得还上上吧,毕竟不上为我省服务器的空间:哈哈哈,有点小坏..

dal层 (数据层)代码分别helper and studentmanagement_dal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentManagement.DAL
{
    using StudentManagement.Model;
    using System.Data.SqlClient;
    using System.Data;
  public  class StudentManagement_DAL
    {
      
      //新增
      public int Add(SM_Class sc) {
          string str = "insert SM_Class values(@SM_name,@SM_Grade,@SM_Class,@SM_Gender,@SM_Age,@SM_OutTime,@SM_Istf)";
    
          SqlParameter[] sqlpmt = new SqlParameter[]{
          new SqlParameter("@SM_name",sc.SM_Name),
          new SqlParameter("@SM_Grade",sc.SM_Grade),
          new SqlParameter("@SM_Class",sc.SM_Classes),
          new SqlParameter("@SM_Gender",sc.SM_Gender),
          new SqlParameter("@SM_Age",sc.SM_Age),
          new SqlParameter("@SM_OutTime",sc.SM_OutTime),
          new  SqlParameter("@SM_Istf",1)
          };
           return     HelperSQL.ExecuteCommand(str,sqlpmt); 
      }

      //软删除
      public int Deleter(int ID) {
          string str = "Update SM_Class set SM_Istf=0 where SM_id=@ID";
          SqlParameter[] sqlpmt = new SqlParameter[]{
          new SqlParameter("@ID",ID)
          };
          return HelperSQL.ExecuteCommand(str, sqlpmt);    
      }
      /// <summary>
      /// 查询所有数据
      /// </summary>
      /// <returns></returns>
      public DataSet QuerySM() {
          string str = "select * from SM_Class where SM_Istf=1 ";
          return HelperSQL.GetDataSet(str);
      }
      /// <summary>
      /// 更据id查询
      /// </summary>
      /// <param name="id"></param>
      /// <returns></returns>
      public DataSet QuerySM(int id) {
          string str = "select * from SM_Class where SM_id=@id";
          SqlParameter[] sqlpmt = new SqlParameter[]{
          new SqlParameter ("@id",id)
          };
          return HelperSQL.GetDataSet(str,sqlpmt);
      }
      //更新
      public int UpdateSM(SM_Class model) { 
      string str="UPDATE SM_Class SET  SM_name = @SM_name ,  SM_Grade = @SM_Grade ,SM_Class = @SM_Class ,SM_Gender = @SM_Gender ,SM_Age = @SM_Age  where SM_Id=@SM_Id  ";
      SqlParameter[] sqlpmt = new SqlParameter[]{
          new SqlParameter("@SM_name",model.SM_Name),
          new SqlParameter("@SM_Grade",model.SM_Grade),
          new SqlParameter("@SM_Class",model.SM_Classes),
          new SqlParameter("@SM_Gender",model.SM_Gender),
          new SqlParameter("@SM_Age",model.SM_Age),
        new SqlParameter ("@SM_Id",model.SM_ID)
          };
      return HelperSQL.ExecuteCommand(str, sqlpmt);
      }
    }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentManagement.DAL
{
    //需要系统配置;系统设定;系统设置;查看系统配置程序集
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Data;
   public class HelperSQL
    {
        //在ui的配置文件中配置AppSettings
       public static string str = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];

       private static SqlConnection connection;
       public static SqlConnection Connection {
           get {
               //判断是否有这个连接没有
               if (connection == null) {
                   //创建连接
                   connection = new SqlConnection(str);
                   //打开连接
                   connection.Open();
               }
                   //判断连接是否是关闭的
               else if(connection.State==System.Data.ConnectionState.Closed){
                   //打开连接
                   connection.Open();
               }
                   //判断连接是否中断
               else if (connection.State == System.Data.ConnectionState.Broken) {
                   //先关闭连接
                   connection.Close();
                   //打开连接
                   connection.Open();
               }
               //返回连接
               return connection;
           }
       }

       public static int ExecuteCommand(string strsql)
       {
           //传入数据库命令和连接
           SqlCommand sqlcmd = new SqlCommand(strsql, Connection);
           //执行语句返回受影响的行数
           int result = sqlcmd.ExecuteNonQuery();
           return result;
       }
       public static int ExecuteCommand(string str, params SqlParameter[] values) {
           //传入数据库命令和连接
           SqlCommand sqlcmd = new SqlCommand(str, Connection);
           //增加参数
           sqlcmd.Parameters.AddRange(values);
           return sqlcmd.ExecuteNonQuery();
       }

       public static DataSet GetDataSet(string str) {
           //创建一个内存缓存
           DataSet ds = new DataSet();
           //传入命令和连接
           SqlCommand sqlcmd = new SqlCommand(str,Connection);
           //创建一个桥接器
           SqlDataAdapter sqlda = new SqlDataAdapter(sqlcmd);
           //写入内存
           sqlda.Fill(ds);
           //返回
           return ds;
       }

       public static DataSet GetDataSet(string str, params SqlParameter[] values)
       {
           //创建一个内存缓存
           DataSet ds = new DataSet();
           //传入命令和连接
           SqlCommand sqlcmd = new SqlCommand(str, Connection);
           //增加参数
           sqlcmd.Parameters.AddRange(values);
           //打开桥连接
           SqlDataAdapter sqlda = new SqlDataAdapter(sqlcmd);
           //写入到内存
           sqlda.Fill(ds);
           //返回
           return ds;
       }

       public static DataSet StoredProcedure(string strName, params IDataParameter[] parmenters)
       {
           using (SqlConnection connection = new SqlConnection(str))
           {
               //创建一个内存缓存
               DataSet ds = new DataSet();
               //创建一个桥连接
               SqlDataAdapter sqlda = new SqlDataAdapter();
               //告诉桥连接这个是存储过程
               sqlda.SelectCommand = StoredProcedureCommand(connection, strName, parmenters);
               //写入内存
               sqlda.Fill(ds);
              //返回
               return ds;
           }
       }
       private static SqlCommand StoredProcedureCommand(SqlConnection sqlcc, string strName, IDataParameter[] parmenters) {
           //传入存储过程名称和连接数据库命令
           SqlCommand sqlcmd = new SqlCommand(strName, sqlcc);
           //告诉数据库这个是存储过程
           sqlcmd.CommandType = CommandType.StoredProcedure;
           foreach (var item in parmenters)
           {
               if (item !=null)
               {
                   //判断的参数是输入输出或者参数是输入并且参数不能为空
                   if ((item.Direction==ParameterDirection.InputOutput||
                  
                       item.Direction==ParameterDirection.Input  )&&
                  
                       (item.Value==null))
                   {
                       //该类区分空值(空对象)和未初始化值
                       item.Value = DBNull.Value;
                   }
                   //加入这个参数
                   sqlcmd.Parameters.Add(item);
               }      
           }
           //返回连接和命令
           return sqlcmd;
       }
   }
}
View Code

bll层(业务逻辑层)代码分别studentmanagement_bll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentManagement.BLL
{
    using System.Data;
    using StudentManagement.Model;
   public class StudentManagement_BLL
    {
       StudentManagement.DAL.StudentManagement_DAL smd = new DAL.StudentManagement_DAL();
      
       //新增
       public int Add(SM_Class sc)
       {

           return smd.Add(sc);
       }

       //软删除
       public int Deleter(int ID)
       {
           return smd.Deleter(ID);
       }
       /// <summary>
       /// 查询所有数据
       /// </summary>
       /// <returns></returns>
       public DataSet QuerySM()
       {
           return smd.QuerySM();
       }
       /// <summary>
       /// 查询id号的数据
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
       public DataSet QuerySM(int id) {
           return smd.QuerySM(id);
       }
       //更新
       public int UpdateSM(SM_Class model)
       {
           return smd.UpdateSM(model);
       }
    }
}
View Code

ui层 代码分别 asp.net 动态页面add.aspx  and querydel.aspx and update.aspx 一般处理程序 del.ashx

add.aspx 前台页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="add.aspx.cs" Inherits="StudentManagement.UI.addupdate" %>

<!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>
    <table >
        <tr>
            <th>姓名</th>
            <td><input type="text" name="name" id="name" /></td>
            </tr>
        <tr>
            <th>班级</th>
            <td><input type="text" name="grade" id="Text1" /></td>
            </tr>
        <tr>
            <th>年级</th>
            <td><input type="text" name="class" id="Text2" /></td>
            </tr>
        <tr>
            <th>性别</th>
            <td>
                <input type="radio" name="gender" id="Text3" value="1" /><input type ="radio" name="gender" id="Text4" value="0" /></td>
            </tr>
        <tr>
            <th>年龄</th>
            <td><input type="text" name="age" id="Text5" /></td>
            </tr>
    
        <tr>
            <td>
                <input type="submit" value ="提交" />
                <input type="reset" value="重置" />
            </td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>
View Code

后台cs文件

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

namespace StudentManagement.UI
{
    using StudentManagement.BLL;
    using StudentManagement.Model;
    using System.Data;
    public partial class addupdate : System.Web.UI.Page
    {
        StudentManagement_BLL smb = new StudentManagement_BLL();
        SM_Class smc = new SM_Class();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.HttpMethod.ToLower()=="post")
            {
                string name = Request.Form["name"];
                string grade = Request.Form["grade"];
                string classes = Request.Form["class"];
                string gender = Request.Form["gender"];             
                string age = Request.Form["age"];
                
                smc.SM_Name = name;
                smc.SM_OutTime = DateTime.Now;
                smc.SM_Gender = gender=="1"?"":"";
                smc.SM_Classes = classes;
                smc.SM_Age = int.Parse(age);
                smc.SM_Grade = grade;

               int id= smb.Add(smc);
               if (id>0)
               {
                  Response.Write("<script>alert('新增成功');window.location='/querydel.aspx'</script>");
                 Response.End();
               }
            }
        }

    }
}
View Code

小小提下code-behind技术

页面与代码分离,asp.net程序需要呈现一个页面分为*.aspx和*.cs这个两个文件,即代码分离技术,实现了html代码和服务器逻辑代码的分离,这样方便代码编写、整理及调试。

querydel.aspx前台页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="querydel.aspx.cs" Inherits="StudentManagement.UI.queryadd" %>

<!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" method="get">
    <div>
    <table>
        <tr>
            <th>id</th>
            <th>姓名</th>
            <th>班级</th>
            <th>年级</th>
            <th>性别</th>
            <th>年级</th>
            <th>时间</th>
            <th>操作</th>
        </tr>
      <%=sb.ToString() %>
    </table>
    </div>
    </form>
</body>
</html>
View Code

后台cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace StudentManagement.UI
{
    using StudentManagement.Model;
    using StudentManagement.BLL;
    
    public partial class queryadd : System.Web.UI.Page
    {
       
     public   System.Text.StringBuilder sb = new System.Text.StringBuilder();
        StudentManagement_BLL smb = new StudentManagement_BLL();
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (Request.HttpMethod.ToLower() == "get")
            {
                if (Request.QueryString["id"]==null)
                {
                    Query();

                }
             
            }
          
        }

        private void Query()
        {
           DataSet ds= smb.QuerySM();
           DataTable dt = ds.Tables[0];
           foreach (DataRow item in dt.Rows)
           {
               sb.Append(" <tr>");
               sb.Append(" <td>" + item["SM_id"] + "</td>");
               sb.Append(" <td>" + item["SM_name"] + "</td>");
               sb.Append(" <td>" + item["SM_Grade"] + "</td>");
               sb.Append(" <td>" + item["SM_Class"] + "</td>");
               sb.Append(" <td>" + item["SM_Gender"] + "</td>");
               sb.Append(" <td>" + item["SM_Age"] + "</td>");
               sb.Append(" <td>" + item["SM_OutTime"] + "</td>");
               sb.Append(" <td><a href=\"del.ashx?id="+ item["SM_id"] +"\">删除</a>|<a href=\"update.aspx?id="+ item["SM_id"] +"\">编辑</a>|<a href='add.aspx'>新增</a></td>");
               sb.Append(" </tr>");
           }
        }

    
    }
}
View Code

update.aspx前台页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="update.aspx.cs" Inherits="StudentManagement.UI.update" %>

<!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" method="post">
        <input type="hidden" value ="<%=id %>" name="id" id="id"/>
    <div>
        <table>
     <tr>
            <th>姓名</th>
            <td><input type="text" name="name" id="name" value ="<%=name %>" /></td>
            </tr>
        <tr>
            <th>班级</th>
            <td><input type="text" name="grade" id="Text1"  value="<%=grade %>"/></td>
            </tr>
        <tr>
            <th>年级</th>
            <td><input type="text" name="class" id="Text2"  value="<%=classes %>"/></td>
            </tr>
        <tr>
            <th>性别</th>
            <td>
                <input type="radio" name="gender" id="Text3" "<%=radio1 %>" />男
                <input type ="radio" name="gender" id="Text4" <%=radio0 %> />女
            </td>
            </tr>
        <tr>
            <th>年龄</th>
            <td><input type="text" name="age" id="Text5"  value="<%=age %>"/></td>
            </tr>
    
        <tr>
            <td>
                <input type="submit" value ="提交" />
                <input type="reset" value="重置" />
            </td>
        </tr>

            </table>
    </div>
    </form>
</body>
</html>
View Code

后台cs文件

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

namespace StudentManagement.UI
{
    using StudentManagement.BLL;
    using StudentManagement.Model;
    using System.Data;
    public partial class update : System.Web.UI.Page
    {
        StudentManagement_BLL smb = new StudentManagement_BLL();
        SM_Class sc = new SM_Class();
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        protected string name;
        protected string grade;
        protected string classes;
        protected string age;
        protected string radio1;
        protected string radio0;
        protected string id;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.HttpMethod.ToLower() == "get")
            {

                string stype = Request.QueryString["id"];
                id = stype;
                DataSet ds = smb.QuerySM(int.Parse(stype));
                DataTable dt = ds.Tables[0];


                name = (string)dt.Rows[0]["SM_name"];
                grade = (string)dt.Rows[0]["SM_Grade"];
                classes = (string)dt.Rows[0]["SM_Class"];
                age = dt.Rows[0]["SM_Age"].ToString();
                var str = dt.Rows[0]["SM_Gender"].ToString();

                string num = "";
                if (str == num)
                {

                    radio1 = "value=\"男\" checked=\"checked\"";
                    radio0 = "value=\"女\" ";
                }
                else
                {
                    radio0 = "value=\"女\" checked=\"checked\"";
                    radio1 = "value=\"男\" ";
                }

            }
            else
            {
                string id = Request.Form["id"];
                string name = Request.Form["name"];
                string grade = Request.Form["grade"];
                string classes = Request.Form["class"];
                string gender = Request.Form["gender"];
                string age = Request.Form["age"];
                sc.SM_Age = int.Parse(age);
                sc.SM_Name = name;
                sc.SM_Grade = grade;
                sc.SM_Classes = classes;
                sc.SM_Gender = gender;
                sc.SM_OutTime = DateTime.Now;
                sc.SM_ID = int.Parse(id);

                int i = smb.UpdateSM(sc);
                if (i > 0)
                {
                    Response.Write("<script>alert('修改成功');window.location='/querydel.aspx'</script>");
                   Response.End();
                }
                else
                {
                    Response.Write("<script>alert('修改失败');window.location='/querydel.aspx'</script>");
                  Response.End();
                }
            }
              
       
        }
    }
}
View Code

一般处理程序 del.ashx

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

namespace StudentManagement.UI
{
    /// <summary>
    /// auqd 的摘要说明
    /// </summary>
    public class auqd : IHttpHandler
    {
        StudentManagement.BLL.StudentManagement_BLL smb = new BLL.StudentManagement_BLL();

        public void ProcessRequest(HttpContext context)
        {
//MIME (Multipurpose Internet Mail Extensions) 是描述消息内容类型的因特网标准。
//MIME 消息能包含文本、图像、音频、视频以及其他应用程序专用的数据。
//context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
        //获取url 传过来的type 值
            string stype = context.Request.QueryString["type"];
            //
            string sid = context.Request.QueryString["id"];

            int id=smb.Deleter(int.Parse(sid));
            if (id>0)
            {
                context.Response.Write("<script>alert('删除成功');window.location='/querydel.aspx'</script>");
                context.Response.End();
            }
            
           
        }

       

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
View Code

这个代码没有什么好要解释的 ,更一般处理程序上一样的,只是可能写法上有所不同,接下我我想我应该写下

ajax异步刷新

 

转载于:https://www.cnblogs.com/fleas/p/4191852.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值