ArticleDAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App.Model;
using System.Data.SqlClient;
using System.Data;

namespace App.DAL
{
    partial class ArticleDAL
    {
        public FCParamter GetItems(Article model)
        {
            FCParamter fCParamter = new FCParamter();
            fCParamter.pk = new SqlParameter("id", model.id);
            fCParamter.parameters = new SqlParameter[] {
new SqlParameter("title", model.title)
,new SqlParameter("categoryid", model.categoryid)
,new SqlParameter("cont", model.cont)
,new SqlParameter("description", model.description)
,new SqlParameter("ord", model.ord)
,new SqlParameter("state", model.state)
,new SqlParameter("addtime", model.addtime)
,new SqlParameter("fbase", model.fbase)
,new SqlParameter("sort", model.sort)
,new SqlParameter("userid", model.userid)
,new SqlParameter("username", model.username)
};
            return fCParamter;
        }
        public void Insert(Article model)
        {
            SqlHelper.ExecuteInsertSql("Article", GetItems(model).parameters);
        }
        public void Update(Article model)
        {
            FCParamter fCParamter = GetItems(model);
            SqlHelper.ExecuteUpdateSql("Article", fCParamter.pk, fCParamter.parameters);
        }
        private static Article ToModel(DataRow row)
        {
            Article model = new Article();
            model.id = row.IsNull("id") ? null : (Int32?)row["id"];
            model.title = row.IsNull("title") ? null : (String)row["title"];
            model.categoryid = row.IsNull("categoryid") ? null : (Int32?)row["categoryid"];
            model.cont = row.IsNull("cont") ? null : (String)row["cont"];
            model.description = row.IsNull("description") ? null : (String)row["description"];
            model.ord = row.IsNull("ord") ? null : (Int32?)row["ord"];
            model.state = row.IsNull("state") ? null : (Int32?)row["state"];
            model.addtime = row.IsNull("addtime") ? null : (DateTime?)row["addtime"];
            model.fbase = row.IsNull("fbase") ? null : (Int32?)row["fbase"];
            model.sort = row.IsNull("sort") ? null : (String)row["sort"];
            model.userid = row.IsNull("userid") ? null : (Int32?)row["userid"];
            model.username = row.IsNull("username") ? null : (String)row["username"];
            return model;
        }
        public Article Get(Int32? id)
        {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from Article  where id=@id",
            new SqlParameter("id", id));
            if (dt.Rows.Count > 1)
            { throw new Exception("more than 1 row was found"); }
            if (dt.Rows.Count <= 0) { return null; }
            DataRow row = dt.Rows[0];
            Article model = ToModel(row);
            return model;
        }
        public bool RepeatChk(string wherestr, List<SqlParameter> whereParameters, Int32? id)
        {
            string sql = string.Empty;
            if (id != null)
            {
                sql = " select count(1) as rs  from Article where id <> " + id.ToString() + "" + wherestr;
            }
            else
            {
                sql = " select count(1) as rs  from Article where 1=1" + wherestr;
            }
            return ((int)SqlHelper.ExecuteScalar(sql, whereParameters.ToArray())) > 0;
        }
        public ResultPageingObject SelectByPage(int pageIndex, int pageSize, string wherestr, string orderby, List<SqlParameter> whereParameters)
        {
            ResultPageingObject resultPageingObject = new ResultPageingObject();
            DataSet ds = SqlHelper.SelectByPage("Article", 1, 10, wherestr, orderby, whereParameters);
            resultPageingObject.dt = ds.Tables[0];
            resultPageingObject.count = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            ds.Tables.RemoveAt(1);
            return resultPageingObject;
        }
        public DataTable SelectList(string wherestr, string orderby, List<SqlParameter> whereParameters)
        {
            string extsql = string.Empty;
            if (!string.IsNullOrWhiteSpace(wherestr))
            {
                extsql += " where " + wherestr + " ";
            }
            if (!string.IsNullOrWhiteSpace(orderby))
            {
                extsql += " order by " + orderby + " ";
            }
            return SqlHelper.ExecuteDataTable("select * from Article  " + extsql, whereParameters.ToArray());
        }
        public List<Article> SelectLists(string wherestr, string orderby, List<SqlParameter> whereParameters)
        {
            List<Article> list = new List<Article>();
            DataTable dt = SelectList(wherestr, orderby, whereParameters);
            foreach (DataRow row in dt.Rows)
            {
                list.Add(ToModel(row));
            }
            return list;
        }
    }
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章