新闻发布系统的增删改查 源码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data;
namespace DAL
{
   public  class NewsArticleDAL
    {
        static string sql;
        static NewsArticle a;
        static DataSet ds;
        static DataRow dr;


        public static bool Add(NewsArticle a)
        {
            sql = string.Format("insert into NewsArticle (AddDate,Cid,Content,Hits,Title,Writer) values ('{0}','{1}','{2}','{3}','{4}','{5}')", a.AddDate, a.Cid, a.Content, a.Hits, a.Title, a.Writer);
            return DBAccess.ExecSqlCommand(sql);
        }


        public static bool Update(NewsArticle a)
        {
            sql = string.Format("update NewsArticle set AddDate='{0}',Cid='{1}',Content='{2}',Hits='{3}',Title='{4}',Writer='{5}' where id={6}", a.AddDate, a.Cid, a.Content, a.Hits, a.Title, a.Writer,a.Id);
            return DBAccess.ExecSqlCommand(sql);
        }


        public static bool Delete(object id)
        {
            sql = string.Format("Delete NewsArticle where id='{0}'", id);
            return DBAccess.ExecSqlCommand(sql);
        }


        public static NewsArticle Find(object id)
        {
            a = new NewsArticle();
            string sql = string.Format("select * from NewsArticle where id='{0}'", id);
            dr = DBAccess.GetDateRow(sql);
            try
            {
                a.AddDate =Convert.ToDateTime(dr["AddDate"].ToString());
                a.Cid = dr["Cid"];
                a.Content = dr["Content"].ToString();
                a.Hits=Convert.ToInt32(dr["hits"]);
                a.Id = dr["id"];
                a.Title = dr["Title"].ToString();
                a.Writer = dr["Writer"].ToString();
                return a;
            }
            catch
            {
                return null;
            }
        }


        public static IList<NewsArticle> FindALL(object cid)
        {
            List<NewsArticle> list = new List<NewsArticle>();
            if (cid != null && cid.ToString() != "")
            {
                sql = "select * from NewsArticle where cid="+cid + " order by id desc";
            }
            else
            {
                sql = "select * from NewsArticle order by id desc";
            }
            


            ds = DBAccess.GetDataSet(sql);
            if (ds == null)
            {
                return null;
            }
            else
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    NewsArticle a = new NewsArticle();
                    a.AddDate = Convert.ToDateTime(dr["addDate"].ToString());
                    a.Cid = dr["cid"];
                    a.Content = dr["Content"].ToString();
                    a.Hits = Convert.ToInt32(dr["hits"]);
                    a.Id = dr["id"];
                    a.Title = dr["Title"].ToString();
                    a.Writer = dr["Writer"].ToString();
                    list.Add(a);
                }
                return list;
            }
        }


        public static IList<NewsArticle> FindALL(object cid,int num)
        {
            List<NewsArticle> list = new List<NewsArticle>();
            if (cid != null && cid.ToString() != "")
            {
                sql = "select top " + num + " * from NewsArticle where cid=" + cid + " order by id desc";
            }
            else
            {
                sql = "select top " + num + " * from NewsArticle order by id desc";
            }




            ds = DBAccess.GetDataSet(sql);
            if (ds == null)
            {
                return null;
            }
            else
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    NewsArticle a = new NewsArticle();
                    a.AddDate = Convert.ToDateTime(dr["addDate"].ToString());
                    a.Cid = dr["cid"];
                    a.Content = dr["Content"].ToString();
                    a.Hits = Convert.ToInt32(dr["hits"]);
                    a.Id = dr["id"];
                    a.Title = dr["Title"].ToString();
                    a.Writer = dr["Writer"].ToString();
                    list.Add(a);
                }
                return list;
            }
        }


        public static IList<NewsArticle> FindALLByHits()
        {
            List<NewsArticle> list = new List<NewsArticle>();
            sql = "select top 12 * from NewsArticle order by hits desc";
            ds = DBAccess.GetDataSet(sql);
            if (ds == null)
            {
                return null;
            }
            else
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    NewsArticle a = new NewsArticle();
                    a.AddDate = Convert.ToDateTime(dr["addDate"].ToString());
                    a.Cid = dr["cid"];
                    a.Content = dr["Content"].ToString();
                    a.Hits = Convert.ToInt32(dr["hits"]);
                    a.Id = dr["id"];
                    a.Title = dr["Title"].ToString();
                    a.Writer = dr["Writer"].ToString();
                    list.Add(a);
                }
                return list;
            }
        }


    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值