国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

從一個輿論調(diào)查的制作談面向?qū)ο蟮木幊趟悸罚ǘ?/h1>
2024-07-21 02:16:41
字體:
供稿:網(wǎng)友
首先,我們要來定義一個數(shù)據(jù)庫表,以保存輿論調(diào)查的想關(guān)數(shù)據(jù),看下面這個表:
/*新聞?wù){(diào)查表*/
if exists(select * from sysobjects where id = object_id('survey'))
   drop table survey
go

create table survey
(
  id        int         identity primary key    not null ,
  surveyid    varchar(20)    default ""         not null ,
  kind        tinyint        default 0        not null ,
  title        nvarchar(100)    default ""        not null ,
  description    nvarchar(255)    default ""        not null ,
  amount    int        default 0         not null ,
  begintime    datetime    default getdate()    not null ,
  endtime    datetime    default getdate()    not null ,
  active    bit        default 0        not null
)        
go

好了,數(shù)據(jù)庫建好了,我可以從上面的survey基類中繼承出具體的子類,比如說我現(xiàn)在要做一個同足球相

關(guān)的調(diào)查,那就做這么一個類:

namespace football
{
    using system;
    using myclass.util ;
    using system.data.sql ;
    using system.collections ;
    /// <summary>
    ///    足球輿論調(diào)查
    /// </summary>
    /// <remarks>
    /// 從myclass.util.survey類繼承而來
    /// </remarks>
    public class footballsurvey : myclass.util.survey
    {


        public footballsurvey()
        {
        }


        /// <summary>
        /// 重載父類虛函數(shù)
        /// </summary>
        /// <param name="a_intid">該調(diào)查的數(shù)據(jù)庫id </param>
        public override void loadfromdatabase(string a_strid)
        {
            myclass.util.myconnection myconn = new myconnection() ;
            sqlcommand mycommand = new sqlcommand() ;
            mycommand.commandtext = "up_getsurvey" ;
            mycommand.commandtype = system.data.commandtype.storedprocedure ;

            try
            {
                myconn.open() ;
                mycommand.activeconnection = myconn ;
                mycommand.parameters.add(new sqlparameter("@a_strsurveyid" ,

sqldatatype.varchar , 20)) ;
                mycommand.parameters["@a_strsurveyid"].value = a_strid ;
                sqldatareader myreader ;
                mycommand.execute(out myreader) ;

                //先取調(diào)查
                if (myreader.read())
                {
                    this.m_strtitle = myreader["title"].tostring() ;
                    this.m_inthits = (int)myreader["amount"] ;
                    this.m_strid = a_strid ;
                    this.m_datbegintime =

(datetime)myreader["begintime"] ;
                    this.m_datendtime = (datetime)myreader["endtime"] ;
                }
                else
                {
                    throw(new exception("數(shù)據(jù)庫中無此調(diào)查的紀(jì)錄")) ;
                }

                //清空調(diào)查項
                m_arritems.clear() ;

                //取調(diào)查項
                if (myreader.hasmorerows)
                {
                    while(myreader.read())
                    {
                        surveyitem item = new surveyitem() ;
                        item.text = myreader["title"].tostring() ;
                        item.id = (int)myreader["id"] ;
                        item.count = (int)myreader["amount"] ;
                        item.description =

myreader["description"].tostring() ;
                        additem(item) ;
                    }
                }
                else
                {
                    throw(new exception("數(shù)據(jù)庫中沒有該調(diào)查相關(guān)的調(diào)查項

")) ;
                }

                //清場
                myreader.close() ;
                myconn.close() ;
            }
            catch(exception e)
            {
                throw(new exception("從數(shù)據(jù)庫中讀取調(diào)查失敗:" +

e.tostring())) ;
            }
        }


        /// <summary>
        /// 將調(diào)查保存到數(shù)據(jù)庫
        /// </summary>
        /// <param name="m_strsurveyid">調(diào)查編號 </param>
        /// <remarks>
        /// 如果m_strsurveyid不為空,則刪除原紀(jì)錄,用當(dāng)前調(diào)查編號保存新的調(diào)查,
        /// 否則就生成一個新的調(diào)查編號
        /// </remarks>
        public override void savetodatabase(string m_strsurveyid)
        {
            //如果沒有標(biāo)題或調(diào)查項則拋出異常
            if (this.m_arritems.count == 0 || this.m_strtitle == "")
            {
                throw(new exception("沒有調(diào)查標(biāo)題或標(biāo)題項")) ;
            }

            myclass.util.myconnection myconn = new myconnection() ;
            sqlcommand mycommand = new sqlcommand() ;
            mycommand.commandtype = system.data.commandtype.text ;
            
            try
            {
                myconn.open() ;
                mycommand.activeconnection = myconn ;

                //如果沒有surveyid則生成surveyid
                string strsurveyid ;
                if(m_strsurveyid == "")
                {
                    strsurveyid = datetime.now.year.tostring()
                                +

datetime.now.month.tostring()
                                +

datetime.now.hour.tostring()
                                +

datetime.now.minute.tostring()
                                +

datetime.now.second.tostring()
                                +

datetime.now.millisecond.tostring() ;
                }
                else    //如果已有,則刪除該條紀(jì)錄
                {
                    strsurveyid = m_strsurveyid ;
                    //刪除原有紀(jì)錄
                    mycommand.commandtext = "delete from survey where

surveyid='" + strsurveyid + "'" ;
                    mycommand.executenonquery() ;
                }

                string strsql = "insert into survey(surveyid , kind , title)

values ('"
                                + strsurveyid +"', 0 , '" +

m_strtitle + "')/r/n" ;
                for (int i = 0 ; i < m_arritems.count ; i ++)
                {
                    strsql += "insert into survey(surveyid , kind ,

title) values('"
                        + strsurveyid + "' , 1 , '"
                        + ((surveyitem)m_arritems[i]).text +

"')/r/n" ;
                }
                //插庫
                mycommand.commandtext = strsql ;
                mycommand.executenonquery() ;

                //清場
                myconn.close() ;
            }
            catch(exception e)
            {
                throw(new exception("保存調(diào)查時出錯:" +  e.tostring())) ;
            }
            
        }

        /// <summary>
        /// 投票
        /// </summary>
        /// <param name="a_intid"> </param>
        public override void vote(int a_intid)
        {
            //該項計數(shù)加一
            ((surveyitem)m_arritems[a_intid]).count += 1 ;

            //數(shù)據(jù)庫中改變
            myconnection myconn = new myconnection() ;
            sqlcommand mycommand = new sqlcommand() ;
            mycommand.commandtext = "update survey set amount=amount+1 where

id="
                                    +

((surveyitem)m_arritems[a_intid]).id.tostring() ;

            mycommand.commandtype = system.data.commandtype.text ;

            try
            {
                myconn.open() ;
                mycommand.activeconnection = myconn ;
                mycommand.executenonquery() ;
                myconn.close() ;
            }
            catch(exception e)
            {
                throw(new exception("更新調(diào)查項失敗:" + e.tostring())) ;
            }
        
        }

        /// <summary>
        /// 調(diào)查列表
        /// </summary>
        public static arraylist surveylist()
        {
            arraylist arrresult = new arraylist() ;
            myclass.util.myconnection myconn = new myconnection() ;
            sqlcommand mycommand = new sqlcommand() ;
            mycommand.commandtext = "select id , surveyid , title from survey

where kind=0 order by surveyid desc" ;
            mycommand.commandtype = system.data.commandtype.text ;

            try
            {
                myconn.open() ;
                mycommand.activeconnection = myconn ;
                sqldatareader myreader ;
                mycommand.execute(out myreader) ;
                while (myreader.read())
                {
                    footballsurvey mysurvey = new footballsurvey() ;
                    mysurvey.title = myreader["title"].tostring() ;
                    mysurvey.surveyid = myreader["surveyid"].tostring()

;
                    arrresult.add(mysurvey) ;
                }

                myreader.close() ;
                myconn.close() ;

            }
            catch(exception e)
            {
                throw(new exception("從數(shù)據(jù)庫中取出調(diào)查失敗:" +

e.tostring())) ;
            }

            //返回結(jié)果
            return arrresult ;
        }

        /// <summary>
        /// 取得激活的調(diào)查id
        /// </summary>
        public static string getactivesurveyid()
        {
            string strresult = "" ;

            myclass.util.myconnection myconn = new myconnection() ;
            sqlcommand mycommand = new sqlcommand() ;
            mycommand.commandtext = "select top 1 id , surveyid from survey

where active=1 order by id desc" ;
            mycommand.commandtype = system.data.commandtype.text ;

            try
            {
                myconn.open() ;
                mycommand.activeconnection = myconn ;
                sqldatareader myreader ;
                mycommand.execute(out myreader) ;
                if (myreader.read())
                {
                    strresult = myreader["surveyid"].tostring() ;
                }
                else
                {
                    throw(new exception("沒有激活的調(diào)查")) ;
                }

                myreader.close() ;

            }
            catch(exception e)
            {
                throw(new exception("從數(shù)據(jù)庫中取出調(diào)查失敗:" +

e.tostring())) ;
            }
            finally
            {
                myconn.close() ;
            }

            //返回結(jié)果
            return strresult ;

        }

        /// <summary>
        /// 激活調(diào)查
        /// </summary>
        /// <param name="a_strsurveyid">調(diào)查編號 </param>
        public static void activesurvey(string a_strsurveyid)
        {
            myclass.util.myconnection myconn = new myclass.util.myconnection() ;
            sqlcommand mycommand = new sqlcommand() ;
            mycommand.commandtext = "update survey set active=0 ;"
                                    + "update survey set

active=1 where surveyid='" + a_strsurveyid + "'" ;
            mycommand.commandtype = system.data.commandtype.text ;

            try
            {
                myconn.open() ;
                mycommand.activeconnection = myconn ;
                mycommand.executenonquery() ;
            }
            catch(exception exp)
            {
                throw(new exception("保存激活調(diào)查到數(shù)據(jù)庫出錯:" +

exp.tostring()));
            }
            finally
            {
                myconn.close() ;
            }
        }

    }
},歡迎訪問網(wǎng)頁設(shè)計愛好者web開發(fā)。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

主站蜘蛛池模板: 皋兰县| 商南县| 玉田县| 普陀区| 灵山县| 宁陕县| 邳州市| 惠水县| 黄梅县| 南宁市| 响水县| 义马市| 屯昌县| 三亚市| 顺昌县| 临沭县| 丰都县| 册亨县| 清水河县| 萍乡市| 保靖县| 韩城市| 阿拉尔市| 岗巴县| 肃宁县| 吴江市| 乌拉特中旗| 叙永县| 怀远县| 平利县| 若羌县| 建德市| 丹凤县| 文昌市| 南溪县| 新蔡县| 裕民县| 余干县| 渭源县| 镶黄旗| 探索|