using System; using System.Data; using System.Text; using System.Data.SqlClient; namespace Basic.DAL { /// /// 频道 /// public partial class channel { /// /// 增加一条数据 /// public int Add(Basic.Model.channel model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into tb_channel("); strSql.Append("class_id,title,guid,parent_id,class_list,class_layer,paixu,url,path,content,seo_title,seo_keywords,seo_description)"); strSql.Append(" values ("); strSql.Append("@class_id,@title,@guid,@parent_id,@class_list,@class_layer,@paixu,@url,@path,@content,@seo_title,@seo_keywords,@seo_description)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@class_id", SqlDbType.Int,10), new SqlParameter("@title", SqlDbType.NVarChar,100), new SqlParameter("@guid", SqlDbType.NVarChar,50), new SqlParameter("@parent_id", SqlDbType.Int,10), new SqlParameter("@class_list", SqlDbType.NVarChar,500), new SqlParameter("@class_layer", SqlDbType.Int,10), new SqlParameter("@paixu", SqlDbType.Int,10), new SqlParameter("@url", SqlDbType.NVarChar,255), new SqlParameter("@path", SqlDbType.NVarChar,255), new SqlParameter("@content", SqlDbType.NText,1073741823), new SqlParameter("@seo_title", SqlDbType.NVarChar,255), new SqlParameter("@seo_keywords", SqlDbType.NVarChar,255), new SqlParameter("@seo_description", SqlDbType.NVarChar,255)}; parameters[0].Value = model.class_id; parameters[1].Value = model.title; parameters[2].Value = model.guid; parameters[3].Value = model.parent_id; parameters[4].Value = model.class_list; parameters[5].Value = model.class_layer; parameters[6].Value = model.paixu; parameters[7].Value = model.url; parameters[8].Value = model.path; parameters[9].Value = model.content; parameters[10].Value = model.seo_title; parameters[11].Value = model.seo_keywords; parameters[12].Value = model.seo_description; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return 0; } else { return Convert.ToInt32(obj); } } /// /// 更新一条数据 /// public bool Update(Basic.Model.channel model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update tb_channel set "); strSql.Append("class_id=@class_id,"); strSql.Append("title=@title,"); strSql.Append("guid=@guid,"); strSql.Append("parent_id=@parent_id,"); strSql.Append("class_list=@class_list,"); strSql.Append("class_layer=@class_layer,"); strSql.Append("paixu=@paixu,"); strSql.Append("url=@url,"); strSql.Append("path=@path,"); strSql.Append("content=@content,"); strSql.Append("seo_title=@seo_title,"); strSql.Append("seo_keywords=@seo_keywords,"); strSql.Append("seo_description=@seo_description"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@class_id", SqlDbType.Int,10), new SqlParameter("@title", SqlDbType.NVarChar,100), new SqlParameter("@guid", SqlDbType.NVarChar,50), new SqlParameter("@parent_id", SqlDbType.Int,10), new SqlParameter("@class_list", SqlDbType.NVarChar,500), new SqlParameter("@class_layer", SqlDbType.Int,10), new SqlParameter("@paixu", SqlDbType.Int,10), new SqlParameter("@url", SqlDbType.NVarChar,255), new SqlParameter("@path", SqlDbType.NVarChar,255), new SqlParameter("@content", SqlDbType.NText,1073741823), new SqlParameter("@seo_title", SqlDbType.NVarChar,255), new SqlParameter("@seo_keywords", SqlDbType.NVarChar,255), new SqlParameter("@seo_description", SqlDbType.NVarChar,255), new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = model.class_id; parameters[1].Value = model.title; parameters[2].Value = model.guid; parameters[3].Value = model.parent_id; parameters[4].Value = model.class_list; parameters[5].Value = model.class_layer; parameters[6].Value = model.paixu; parameters[7].Value = model.url; parameters[8].Value = model.path; parameters[9].Value = model.content; parameters[10].Value = model.seo_title; parameters[11].Value = model.seo_keywords; parameters[12].Value = model.seo_description; parameters[13].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_channel"); 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.channel GetModel(int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 id,class_id,title,guid,parent_id,class_list,class_layer,paixu,url,path,content,seo_title,seo_keywords,seo_description from tb_channel"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int,4)}; parameters[0].Value = id; Basic.Model.channel model = new Basic.Model.channel(); 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]["class_id"].ToString())) { model.class_id = int.Parse(ds.Tables[0].Rows[0]["class_id"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["title"].ToString())) { model.title = ds.Tables[0].Rows[0]["title"].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]["parent_id"].ToString())) { model.parent_id = int.Parse(ds.Tables[0].Rows[0]["parent_id"].ToString()); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["class_list"].ToString())) { model.class_list = ds.Tables[0].Rows[0]["class_list"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["class_layer"].ToString())) { model.class_layer = int.Parse(ds.Tables[0].Rows[0]["class_layer"].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]["url"].ToString())) { model.url = ds.Tables[0].Rows[0]["url"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["path"].ToString())) { model.path = ds.Tables[0].Rows[0]["path"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["content"].ToString())) { model.content = ds.Tables[0].Rows[0]["content"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["seo_title"].ToString())) { model.seo_title = ds.Tables[0].Rows[0]["seo_title"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["seo_keywords"].ToString())) { model.seo_keywords = ds.Tables[0].Rows[0]["seo_keywords"].ToString(); } if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["seo_description"].ToString())) { model.seo_description = ds.Tables[0].Rows[0]["seo_description"].ToString(); } return model; } else { return null; } } /// /// 取得所有类别列表 /// /// 父ID /// 分类ID /// public DataTable GetList(int parent_id, int class_id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select * from tb_channel"); strSql.Append(" where class_id=" + class_id + " order by paixu desc,id asc"); DataSet ds = DbHelperSQL.Query(strSql.ToString()); DataTable oldData = ds.Tables[0] as DataTable; if (oldData == null) { return null; } //复制结构 DataTable newData = oldData.Clone(); //调用迭代组合成DAGATABLE GetChilds(oldData, newData, parent_id, class_id); return newData; } /// /// 从内存中取得所有下级类别列表(自身迭代) /// private void GetChilds(DataTable oldData, DataTable newData, int parent_id, int channel_id) { DataRow[] dr = oldData.Select("parent_id=" + parent_id); for (int i = 0; i < dr.Length; i++) { //添加一行数据 DataRow row = newData.NewRow(); row["id"] = int.Parse(dr[i]["id"].ToString()); row["class_id"] = int.Parse(dr[i]["class_id"].ToString()); row["title"] = dr[i]["title"].ToString(); row["guid"] = dr[i]["guid"].ToString(); row["parent_id"] = int.Parse(dr[i]["parent_id"].ToString()); row["class_list"] = dr[i]["class_list"].ToString(); row["class_layer"] = int.Parse(dr[i]["class_layer"].ToString()); row["paixu"] = int.Parse(dr[i]["paixu"].ToString()); row["url"] = dr[i]["url"].ToString(); row["path"] = dr[i]["path"].ToString(); row["content"] = dr[i]["content"].ToString(); row["seo_title"] = dr[i]["seo_title"].ToString(); row["seo_keywords"] = dr[i]["seo_keywords"].ToString(); row["seo_description"] = dr[i]["seo_description"].ToString(); newData.Rows.Add(row); //调用自身迭代 this.GetChilds(oldData, newData, int.Parse(dr[i]["id"].ToString()), channel_id); } } /// /// 修改一列数据 /// public void UpdateField(int id, string strValue) { StringBuilder strSql = new StringBuilder(); strSql.Append("update tb_channel set " + strValue); strSql.Append(" where id=" + id); DbHelperSQL.ExecuteSql(strSql.ToString()); } } }