You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
10 KiB

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
namespace Basic.DAL
{
/// <summary>
/// 相册
/// </summary>
public partial class albums
{
/// <summary>
/// 增加一条数据
/// </summary>
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);
}
}
/// <summary>
/// 更新一条数据
/// </summary>
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;
}
}
/// <summary>
/// 删除一条数据
/// </summary>
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;
}
}
/// <summary>
/// 得到一个对象实体
/// </summary>
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;
}
}
/// <summary>
/// 获得数据列表
/// </summary>
public List<Basic.Model.albums> GetList(string _guid)
{
List<Basic.Model.albums> modelList = new List<Basic.Model.albums>();
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;
}
/// <summary>
/// 查找不存在的图片并删除已删除的图片及数据
/// </summary>
public void DeleteList(List<Basic.Model.albums> 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()); //删除缩略图
}
}
}
/// <summary>
/// 删除数据和图片
/// </summary>
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()); //删除缩略图
}
}
}
}
}