利用c#制作简单的留言板
2024-07-21 02:20:21
供稿:网友
 
首先要感谢bigeagle的帮助,这个也是参考她的bbs做成的
留言板分三个模块:列出留言列表、显示详细内容、发表留言
notepage.cs
namespace notpage
{
    using system;
    using system.data.sql ;
    using system.data ;
    using system.collections ;
    
        
    ////////////////////////////////////////////////////////////////////
    //
    // class name :       留言板
    //
    // description:       构造一个留言板对象
    //
    // date:              2000/06/06
    //
    // 作者:              天啦
    /// ////////////////////////////////////////////////////////////////
    /// <summary>
    ///    summary description for notepage.
    /// </summary>
    
     
    public class notepage
    {
        //私有变量
    
        private int       n_intid    ;            //id编号
        private string n_strtitle ;            //主题
        private string n_strauthor ;        //留言人
        private string n_strcontent ;        //留言内容
        private datetime n_datetime ;        //留言时间
        
        //属性
    
        public int id
        {
            get
            {
                return n_intid ;
            }
            set 
            {
                n_intid = value;
            }
        }
        public string title 
        {
            get
            {
                return n_strtitle ;
            }
            set
            {
                n_strtitle = value;
            }
        }
        public string author
        {
            get
            {
                return n_strauthor ;
            }
            set
            {
                n_strauthor = value ;
            }
        }
        public string content
        {
            get
            {
                return n_strcontent ;
            }
            set
            {
                n_strcontent = value ;
            }
        }
        public datetime adddate
        {
            
            get
            {
                return n_datetime;
            }
            set
            {
                n_datetime = value;
            }
        }
        //构造函数
          public notepage()
        {
            //
            // todo: add constructor logic here
            //
            this.n_intid = 0 ;
            this.n_strtitle = "" ;
            this.n_strauthor = "" ;
            this.n_strcontent = "" ;
            this.n_datetime = system.datetime.now;
            
        }
        /// <summary>
        /// 
        /// 取得留言的内容
        /// 
        /// </summary>
        /// <param name="a_intid"> </param>
        public notepage gettopic(int a_intid)
        {
            //
            // todo: add constructor logic here
            //
            
            //读取数据库
            myconn myconn = new myconn();
            
            sqlcommand mycommand = new sqlcommand() ;
            mycommand.activeconnection = myconn ;
            mycommand.commandtext = "n_gettopicinfo" ;    //调用存储过程
            mycommand.commandtype = commandtype.storedprocedure ;
            mycommand.parameters.add(new sqlparameter("@a_inttopicid" , sqldatatype.int)) ;
            mycommand.parameters["@a_inttopicid"].value = a_intid ;
            notepage objnp = new notepage();
            try
            {    
                
                myconn.open() ;
                sqldatareader myreader ;
                mycommand.execute(out myreader) ;
                if (myreader.read())
                {
                    objnp.id = (int)myreader["id"] ;
                    objnp.title = (string)myreader["title"] ;
                    objnp.author  = (string)myreader["author"] ;
                    objnp.content = (string)myreader["content"];
                    objnp.adddate = (datetime)myreader["adddate"];
                }
                
                
                //清场
                myreader.close();
                myconn.close() ;
            }
            catch(exception e)
            {
                throw(new exception("取贴子失败:" + e.tostring())) ;
            }
            return objnp;
            
        }
        /// <summary>
        /// 
        /// 目的:将留言的内容入库
        /// 
        /// 利用构造函数来传递信息
        /// 
        /// </summary>
        /// <param name="n_topic"> </param>
        public bool addtopic(notepage n_topic)
        {
            //
            // todo: add constructor logic here
            //
            
            //读取数据库
            myconn myconn = new myconn();
            
            sqlcommand mycommand = new sqlcommand() ;
            mycommand.activeconnection = myconn ;
            mycommand.commandtext = "n_addtopic" ;    //调用存储过程
            mycommand.commandtype = commandtype.storedprocedure ;
            mycommand.parameters.add(new sqlparameter("@a_strtitle" , sqldatatype.varchar,100)) ;
            mycommand.parameters["@a_strtitle"].value = n_topic.title ;
            mycommand.parameters.add(new sqlparameter("@a_strauthor" , sqldatatype.varchar,50)) ;
            mycommand.parameters["@a_strauthor"].value = n_topic.author ;
            mycommand.parameters.add(new sqlparameter("@a_strcontent" , sqldatatype.varchar,2000)) ;
            mycommand.parameters["@a_strcontent"].value = n_topic.content ;
            try
            {    
                
                myconn.open() ;
                mycommand.executenonquery() ;
                                
                //清场
                
                myconn.close() ;
            }
            catch(exception e)
            {
                throw(new exception("取贴子失败:" + e.tostring())) ;
            }
            return true;
                
        
        }
        /// <summary>
        /// 取的贴子列表
        /// </summary>
        /// <remarks>
        /// 返回一个topic数组
        /// </remarks>
        public arraylist gettopiclist()
        {
            //定义一个forum数组做为返回值
            arraylist arrforumlist =new arraylist() ;
            //从数据库中读取留言列表
            myconn myconn = new myconn();
            sqlcommand mycommand = new sqlcommand() ;
            mycommand.activeconnection = myconn ;
            mycommand.commandtext = "n_gettopiclist" ;    //调用存储过程
            mycommand.commandtype = commandtype.storedprocedure ;
            try
            {
                myconn.open() ;
                sqldatareader myreader ;
                mycommand.execute(out myreader) ;
                for (int i = 0 ; myreader.read() ; i++)
                {
                    notepage objitem = new notepage() ;
                    objitem.id = myreader["id"].tostring().toint32() ;
                    objitem.title = myreader["title"].tostring() ;
                    objitem.author = myreader["author"].tostring() ;
                    objitem.adddate = myreader["adddate"].tostring().todatetime();    
                    objitem.content = myreader["content"].tostring();
                                                        
                    arrforumlist.add(objitem) ;
                }
                
                //清场
                myreader.close();
                myconn.close() ;
            }
            catch(sqlexception e)
            {
                throw(new exception("数据库出错:" + e.tostring())) ;
                //return null ;
            }
            return arrforumlist ;
        }
            
    }
}