top
Loading...
從一個輿論調查的制作談面向對象的編程思路(二)
首先,我們要來定義一個數據庫表,以保存輿論調查的想關數據,看下面這個表:
/*新聞調查表*/
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 + "')" ;
for (int i = 0 ; i < m_arrItems.Count ; i ++)
{
strSql += "insert into survey(surveyid , kind ,

title) values('"
+ strSurveyID + "' , 1 , '"
+ ((SurveyItem)m_arrItems[i]).Text +

"')" ;
}
//插庫
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() ;
}
}

}
}

北斗有巢氏 有巢氏北斗