您的位置:首页 > 其它

关于,查询列表,增加及其删除

2013-02-21 08:33 190 查看
1、SQLHelper  //DataTable  //执行查询  public static DataTable MyDatatable(string sql,params sqlparameter[] ps)  {   DataTable dt=new DataTable();   using(sqlconnection conn=new sqlconnection(connStr))   {    using(sqlcommand cmd=new sqlcommand(sql,conn))    {     cmd.Parameters.AddRange(ps);     using(sqldataAdapter sda=new sqlDataAdapter(cmd))     {      sda.Fill(dt);     }    }   }   return dt;  }

//ExecuteNonQuery()  //执行增删改  public static int ExecuteNonQuery(string sql,params sqlParameter[] ps)  {   using(sqlConnection conn=new sqlConnection(connStr))   {    using(sqlCommand cmd=new sqlCommand(sql,conn))    {     cmd.Parameters.AddRange(ps);    }    conn.Open();    return cmd.ExecuteNonQuery();   }  }     2、DAL //查询所有  public List<Photos> GetAllPhotos()  {   List<Photos> list=new list<Photos>();   string sql="select * from photos order by PTime desc";   DataTable dt=SQLHelper.MyDatatable(sql);   //把二维表转换为对象;   foreach(DataRow dr in dt.Rows)   {    Photos p=RowToPhotos();    list.Add(p);   }   return list;  }  //把行转换为对象  private Photos RowToPhotos(DataRow dr)  {   Photos p=new Photos();   p.PClicks=Convert.ToInt32(dr["PClicks"]);   p.PDes=dr["PDes"].ToString();   p.PDown=Convert.ToInt32("PDown");   p.PId=Convert.ToDateTime(dr["PId"]);   p.PTitle=dr["PTitle"].ToString();   return p;  } //增加   public int Add(Photos p)         {             string sql = "insert into photos(PTypeId, PUserId, PTitle, PUrl, PDes) values(@PTypeId, @PUserId, @PTitle, @PUrl, @PDes)";

SqlParameter[] param = {                                     new SqlParameter("@PTypeId",p.PTypeId),                                     new SqlParameter("@PUserId",p.PUserId),                                     new SqlParameter("@PTitle",p.PTitle),                                     new SqlParameter("@PUrl",p.PUrl),                                     new SqlParameter("@PDes",p.PDes)                                    };

return SQLHelper.ExecuteNonQuery(sql, param);         } //删除  public int Delete(int pid)  {   string sql="delete from photos where pid=@pid";   return SQLHelper.ExecuteNonQuery(sql);  } //修改 3、BLL //查询   photosDAL dal=new photosDAL();   public List<Photos> GetAllPhotos()   {  return dal.GetAllPhotos();   } //增加    public bool Add(Photos p)         {             return dal.Add(p) > 0 ? true : false;         } //删除  public bool Delete(int pid)  {   return dal.Delete(pid)>0?true:false;  } //修改 4、UI //查询   //1、读取静态页面    string path=context.Request.MapPath("aa.html");    string html=File.ReadAllText(path);   //2、读取数据库中的数据   PhotosBll bll=new PhotosBll();   List<Photos> list=bll.GetAllPhotos();//读取数据库中的图片数据   //3、拼表格   StringBuilder sb=new StringBuilder();   sb.Append("<table id='photos' cellspacing='0' cellpadding='0'>");    for(int i=1;i<list.count;i++)    {     sb.Append("<tr>");     sb.Append("<td>"+(i+1)+"</td>");//序号     sb.Append("<td>"+list[i].PTitle+"</td>");//标题     sb.Append("<td>"+list[i].PUrl+"</td>");//图片     sb.Append("<td>"+list[i].PClicks+"</td>");//点击次数     sb.Append("<td>编辑<a href='Delete.ashx?pid="+list[i].PId+"' onclick='return confirm(\"确认删除?\")'>删除</a> </td>")      sb.Append("</tr>");    }   sb.Append("</table>");//表格的结束   //4、替换   html.html.Replace("@table",sb.ToString());   //5、输出   context.Responst.write(html);    //增加  新建一个一般处理程序add.ashx  //加载静态页面  string path=context.Request.MapPath("add.html");  string html=File.ReadAllText(path);  //2.判断页面是否是第一次加载  string viewstate=context.Request.Form["viewstate"];  if(string.IsNullOrEmpty(viewstate))//如果是第一次加载  {   html=html.Replace("@title","").Replace("@url","").Replace("@des","");  }  else  {   Photos p=new Photos();   //赋值   p.PDes=context.Request.Form["txtDes"];   p.PTitle=context.Request.Form[txtTitle]   p.PUrl=context.Request.Form["txtUrl"];   //调用bll的对象   photosBLL bll=new photosBLL();   if(bll.Add(p))   {    context.Response.Write("<script>alert('添加成功');location.href='photoslist.ashx'</script>")   }   else   {   html=html.Replace("@title",p.PTitle).Replace("@url",p.PUrl).Replace("@des",p.Pdes);    context.Response.Write("<script>alert('添加失败')</script>")   }  } //删除  新建一个一般处理程序(Delete.ashx)  string s=context.Request.QueryString["pid"];  int pid;  if(int.TryParse(s,out pid))  {   PhotosBll bll=new photosbll();   if(bll.Delete(pid))   {        context.Response.Write("<script>alert('删除成功');window.location.href='PhotosList'</script>")   }   else   {    context.Response.Write("<script>alert('删除成功');window.location.href='PhotosList'</script>")   }  }  else  {   context.Response.Write("参数错误");  }   //修改


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐