在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
动态页面的增删查改,不多说了,直接上代码 跟前面的一般处理程序一样我上用的同一套三层,只是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); } } } 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=="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(); } } } } } 小小提下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[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>" + |
请发表评论