从一个舆论调查的制作谈面向对象的编程思路(二)
2024-07-21 02:16:41
供稿:网友
首先,我们要来定义一个数据库表,以保存舆论调查的想关数据,看下面这个表:
/*新闻调查表*/
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
好了,数据库建好了,我可以从上面的survey基类中继承出具体的子类,比如说我现在要做一个同足球相
关的调查,那就做这么一个类:
namespace football
{
using system;
using myclass.util ;
using system.data.sql ;
using system.collections ;
/// <summary>
/// 足球舆论调查
/// </summary>
/// <remarks>
/// 从myclass.util.survey类继承而来
/// </remarks>
public class footballsurvey : myclass.util.survey
{
public footballsurvey()
{
}
/// <summary>
/// 重载父类虚函数
/// </summary>
/// <param name="a_intid">该调查的数据库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) ;
//先取调查
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("数据库中无此调查的纪录")) ;
}
//清空调查项
m_arritems.clear() ;
//取调查项
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("数据库中没有该调查相关的调查项
")) ;
}
//清场
myreader.close() ;
myconn.close() ;
}
catch(exception e)
{
throw(new exception("从数据库中读取调查失败:" +
e.tostring())) ;
}
}
/// <summary>
/// 将调查保存到数据库
/// </summary>
/// <param name="m_strsurveyid">调查编号 </param>
/// <remarks>
/// 如果m_strsurveyid不为空,则删除原纪录,用当前调查编号保存新的调查,
/// 否则就生成一个新的调查编号
/// </remarks>
public override void savetodatabase(string m_strsurveyid)
{
//如果没有标题或调查项则抛出异常
if (this.m_arritems.count == 0 || this.m_strtitle == "")
{
throw(new exception("没有调查标题或标题项")) ;
}
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 //如果已有,则删除该条纪录
{
strsurveyid = m_strsurveyid ;
//删除原有纪录
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("保存调查时出错:" + e.tostring())) ;
}
}
/// <summary>
/// 投票
/// </summary>
/// <param name="a_intid"> </param>
public override void vote(int a_intid)
{
//该项计数加一
((surveyitem)m_arritems[a_intid]).count += 1 ;
//数据库中改变
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("更新调查项失败:" + e.tostring())) ;
}
}
/// <summary>
/// 调查列表
/// </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("从数据库中取出调查失败:" +
e.tostring())) ;
}
//返回结果
return arrresult ;
}
/// <summary>
/// 取得激活的调查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("没有激活的调查")) ;
}
myreader.close() ;
}
catch(exception e)
{
throw(new exception("从数据库中取出调查失败:" +
e.tostring())) ;
}
finally
{
myconn.close() ;
}
//返回结果
return strresult ;
}
/// <summary>
/// 激活调查
/// </summary>
/// <param name="a_strsurveyid">调查编号 </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("保存激活调查到数据库出错:" +
exp.tostring()));
}
finally
{
myconn.close() ;
}
}
}
},欢迎访问网页设计爱好者web开发。