using System; using System.Data; using System.Text; using System.Data.SqlClient; using System.Collections.Generic; namespace Basic.DAL { /// /// 相册 /// public partial class albums { /// /// 增加一条数据 /// public int Add(Basic.Model.albums model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into tb_albums("); strSql.Append("paixu,guid,big_img,small_img,remark)"); strSql.Append(" values ("); strSql.Append("@paixu,@guid,@big_img,@small_img,@remark)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@paixu", SqlDbType.Int,10), new SqlParameter("@guid", SqlDbType.NVarChar,50), new SqlParameter("@big_img", SqlDbType.NVarChar,255), new SqlParameter("@small_img", SqlDbType.NVarChar,255), new SqlParameter("@remark", SqlDbType.NVarChar,500)}; parameters[0].Value = model.paixu; parameters[1].Value = model.guid; parameters[2].Value = model.big_img; parameters[3].Value = model.small_img; parameters[4].Value = model.remark; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return 0; } else { return Convert.ToInt32(obj); } } /// /// 更新一条数据 /// public bool Update(Basic.Model.albums model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update tb_albums set "); strSql.Append("paixu=@paixu,"); strSql.Append("guid=@guid,"); strSql.Append("big_img=@big_img,"); strSql.Append("small_img=@small_img,"); strSql.Append("remark=@remark"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@paixu", SqlDbType.Int,10), new SqlParameter("@guid", SqlDbType.NVarChar,50), new SqlParameter("@big_img", SqlDbType.NVarChar,255), new SqlParameter("@small_img", SqlDbType.NVarChar,255), new SqlParameter("@remark", SqlDbType.NVarChar,500), new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = model.paixu; parameters[1].Value = model.guid; parameters[2].Value = model.big_img; parameters[3].Value = model.small_img; parameters[4].Value = model.remark; parameters[5].Value = model.id; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return true; } else { return false; } } /// /// 删除一条数据 /// public bool Delete(int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("delete from tb_albums"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = id; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return true; } else { return false; } } /// /// 得到一个对象实体 /// public Basic.Model.albums GetModel(int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 id,paixu,guid,big_img,small_img,remark from tb_albums"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = id; Basic.Model.albums model = new Basic.Model.albums(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["id"].ToString())) { model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["paixu"].ToString())) { model.paixu = int.Parse(ds.Tables[0].Rows[0]["paixu"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["guid"].ToString())) { model.guid = ds.Tables[0].Rows[0]["guid"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["big_img"].ToString())) { model.big_img = ds.Tables[0].Rows[0]["big_img"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["small_img"].ToString())) { model.small_img = ds.Tables[0].Rows[0]["small_img"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["remark"].ToString())) { model.remark = ds.Tables[0].Rows[0]["remark"].ToString(); } return model; } else { return null; } } /// /// 获得数据列表 /// public List GetList(string _guid) { List modelList = new List(); StringBuilder strSql = new StringBuilder(); strSql.Append("select * "); strSql.Append(" from tb_albums "); strSql.Append(" where guid='" + _guid + "'"); DataTable dt = DbHelperSQL.Query(strSql.ToString()).Tables[0]; int rowsCount = dt.Rows.Count; if (rowsCount > 0) { Basic.Model.albums model; for (int n = 0; n < rowsCount; n++) { model = new Basic.Model.albums(); if (dt.Rows[n]["id"] != null && dt.Rows[n]["id"].ToString() != "") { model.id = int.Parse(dt.Rows[n]["id"].ToString()); } if (dt.Rows[n]["paixu"] != null && dt.Rows[n]["paixu"].ToString() != "") { model.paixu = int.Parse(dt.Rows[n]["paixu"].ToString()); } if (dt.Rows[n]["guid"] != null && dt.Rows[n]["guid"].ToString() != "") { model.guid = dt.Rows[n]["guid"].ToString(); } if (dt.Rows[n]["big_img"] != null && dt.Rows[n]["big_img"].ToString() != "") { model.big_img = dt.Rows[n]["big_img"].ToString(); } if (dt.Rows[n]["small_img"] != null && dt.Rows[n]["small_img"].ToString() != "") { model.small_img = dt.Rows[n]["small_img"].ToString(); } if (dt.Rows[n]["remark"] != null && dt.Rows[n]["remark"].ToString() != "") { model.remark = dt.Rows[n]["remark"].ToString(); } modelList.Add(model); } } return modelList; } /// /// 查找不存在的图片并删除已删除的图片及数据 /// public void DeleteList(List models, string _guid) { StringBuilder idList = new StringBuilder(); if (models != null) { foreach (Basic.Model.albums modelt in models) { if (modelt.id > 0) { idList.Append(modelt.id + ","); } } } string id_list = Basic.Tools.Utils.DelLastChar(idList.ToString(), ","); StringBuilder strSql = new StringBuilder(); strSql.Append("select id,big_img,small_img from tb_albums where guid='" + _guid + "'"); if (!string.IsNullOrEmpty(id_list)) { strSql.Append(" and id not in(" + id_list + ")"); } Basic.BasicPage bp = new BasicPage(); DataSet ds = bp.SelectDataBase("tb_albums", strSql.ToString()); foreach (DataRow dr in ds.Tables[0].Rows) { bool result = Delete(int.Parse(dr["id"].ToString()));//删除数据 if (result) { Basic.Tools.Utils.DeleteFile("../.." + dr["big_img"].ToString()); //删除原图 Basic.Tools.Utils.DeleteFile("../.." + dr["small_img"].ToString()); //删除缩略图 } } } /// /// 删除数据和图片 /// public void Delete(string _guid) { StringBuilder strSql = new StringBuilder(); strSql.Append("select id,big_img,small_img from tb_albums where guid='" + _guid + "'"); Basic.BasicPage bp = new BasicPage(); DataSet ds = bp.SelectDataBase("tb_albums", strSql.ToString()); foreach (DataRow dr in ds.Tables[0].Rows) { bool result = Delete(int.Parse(dr["id"].ToString()));//删除数据 if (result) { Basic.Tools.Utils.DeleteFile("../../" + dr["big_img"].ToString()); //删除原图 Basic.Tools.Utils.DeleteFile("../../" + dr["small_img"].ToString()); //删除缩略图 } } } } }