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",)
};
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);
}
}
}
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;
}
}
}
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);
}
}
}
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>
后台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==""?"男":"女";
smc.SM_Classes = classes;
smc.SM_Age = int.Parse(age);
smc.SM_Grade = grade; int id= smb.Add(smc);
if (id>)
{
Response.Write("<script>alert('新增成功');window.location='/querydel.aspx'</script>");
Response.End();
}
}
} }
}
小小提下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>
后台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[];
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>");
}
} }
}
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>
后台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[]; name = (string)dt.Rows[]["SM_name"];
grade = (string)dt.Rows[]["SM_Grade"];
classes = (string)dt.Rows[]["SM_Class"];
age = dt.Rows[]["SM_Age"].ToString();
var str = dt.Rows[]["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 > )
{
Response.Write("<script>alert('修改成功');window.location='/querydel.aspx'</script>");
Response.End();
}
else
{
Response.Write("<script>alert('修改失败');window.location='/querydel.aspx'</script>");
Response.End();
}
} }
}
}
一般处理程序 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>)
{
context.Response.Write("<script>alert('删除成功');window.location='/querydel.aspx'</script>");
context.Response.End();
} } public bool IsReusable
{
get
{
return false;
}
}
}
}
这个代码没有什么好要解释的 ,更一般处理程序上一样的,只是可能写法上有所不同,接下我我想我应该写下
ajax异步刷新
ASP.NET动态的网页增删查改的更多相关文章
- asp.net操作xml(增删查改)
asp.net操作xml 1.xml文档Products.xml <?xml version="1.0" encoding="utf-8"?> &l ...
- EasyUI的增删查改(后台ASP.NET)
转自:http://www.cnblogs.com/dedeyi/archive/2013/04/22/3035057.html 某某人曾经跟我说,你们做系统不就是增删查改吗. 是啊,很多时候我们就是 ...
- 4.在MVC中使用仓储模式进行增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-using-the-repository-pattern-in-mvc/ 系列目录: ...
- 5.在MVC中使用泛型仓储模式和工作单元来进行增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...
- 一套手写ajax加一般处理程序的增删查改
倾述下感受:8天16次驳回.这个惨不忍睹. 好了不说了,说多了都是泪. 直接上代码 : 这个里面的字段我是用动软生成的,感觉自己手写哪些字段太浪费时间了,说多了都是泪 ajax.model层的代码: ...
- 极极极极极简的的增删查改(CRUD)解决方案
去年这个时候写过一篇全自动数据表格的文章http://www.cnblogs.com/liuyh/p/5747331.html.文章对自己写的一个js组件做了个概述,很多人把它当作了一款功能相似的纯前 ...
- 在MVC中使用泛型仓储模式和工作单元来进行增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...
- 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性
基于.net的分布式系统限流组件 在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...
- VS 自动创建带增删查改的MVC网站
VS 自动创建带增删查改的MVC网站 MVC.Net教程 废话放在前头,说一下这个文章的缘起某天某妹纸找我,说这个MVC的创建不太会,要记一下controllers.models.还有页面引用的东 ...
随机推荐
- 图的邻接链表实现(c)
参考:算法:C语言实现 一书 实现: #ifndef GRAPH #define GRAPH #include<stdio.h> #include<stdlib.h> stru ...
- 【Chromium中文文档】安全浏览 -- Chrome中的警告都是怎么来的?
安全浏览 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//General_Architecture/SafeBrow ...
- HTTP请求大全
1xx - 信息提示这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个 1xx 响应.100 - 继续101 - 切换协议 2xx - 成功这类状态代码表明服务器成功地接受了客 ...
- linux win7双系统
真恨我自己啊,刚在linux下写了这个博客,因为没有分类,添加了个linux分类.按了F5刷没了.靠,哪里有心情复述啊 一直想装直接装linux系统,现在实现他,以后也要跟上linux的笔记,不然都对 ...
- openstack 升级设计要求的指导原则
不知道其他软件有没有类似的指导原则. Theory of Upgrade Grenade works under the following theory of upgrade. New code s ...
- 面向对象程序设计-C++_课时19const_课时20不可修改的
error C2131: 表达式的计算结果不是常数 #include <iostream> using namespace std; void main() { ; int finalGr ...
- drupal7中CKEditor开启上传图片功能
在drupal建站中,所见即所得编辑器提供了友好的界面.也提高开发效率,而CKEditor是一款非常不错的编辑器.定制性相当高,在这推荐给大家. CKEditor和其它模块(IMCE)搭配下在文字排版 ...
- SPOJ QTREE4 lct
题目链接 这个题已经处于花式tle了,改版后的spoj更慢了.. tle的话就多交几把... #include <iostream> #include <fstream> #i ...
- 仅仅需手动添加一行代码就可以让Laravel4执行在SAE (v. 1.0.0)
Github:https://github.com/chariothy/laravel4-sae (已更新至v1.1.0) laravel4-sae (v. 1.0.0) 仅仅需手动添加一行代码就可以 ...
- [LeetCode] Search a 2D Matrix [25]
题目 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the fo ...