直播中
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() ;
}
}
}
}