首先,我們要來定義一個數(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ā)。