From the creation of a public opinion survey to talk about object-oriented programming ideas (2)

Source: Internet
Author: User

First, we need to define a database table to save the data that the public opinion survey wants to close. See the following table:
/* News survey */
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

Well, after the database is created, I can inherit the specific subclass from the survey base class above. For example, I want to create a football match

The investigation, then we will do such a class:

Namespace Football
{
Using System;
Using MyClass. Util;
Using System. Data. SQL;
Using System. Collections;
/// <Summary>
/// Football public opinion survey
/// </Summary>
/// <Remarks>
/// Inherited from the MyClass. Util. Survey class
/// </Remarks>
Public class FootballSurvey: MyClass. Util. Survey
{


Public FootballSurvey ()
{
}


/// <Summary>
/// Overload the parent class virtual function
/// </Summary>
/// <Param name = "a_intID"> id of the database under investigation </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 );

// Take the investigation first
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 ("no record for this survey in the Database "));
}

// Clear the survey items
M_arrItems.Clear ();

// Retrieve survey items
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 ("the database does not have any items related to this survey.

"));
}

// Clear the scene
MyReader. Close ();
MyConn. Close ();
}
Catch (Exception e)
{
Throw (new Exception ("failed to read from database:" +

E. ToString ()));
}
}


/// <Summary>
/// Save the survey to the database
/// </Summary>
/// <Param name = "m_strSurveyID"> Survey No. </param>
/// <Remarks>
/// If m_strSurveyID is not empty, delete the original record and use the current investigation number to save the new investigation,
/// Otherwise, a new survey number is generated.
/// </Remarks>
Public override void SaveToDatabase (string m_strSurveyID)
{
// If there is no title or investigation item, an exception is thrown.
If (this. m_arrItems.Count = 0 | this. m_strTitle = "")
{
Throw (new Exception ("no survey title or title item "));
}

MyClass. Util. MyConnection myConn = new MyConnection ();
SQLCommand myCommand = new SQLCommand ();
MyCommand. CommandType = System. Data. CommandType. Text;

Try
{
MyConn. Open ();
MyCommand. ActiveConnection = myConn;

// Generate a surveyid if no surveyid exists
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 // Delete this record if it already exists
{
StrSurveyID = m_strSurveyID;
// Delete the original record
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 +

")";
}
// Insert a database
MyCommand. CommandText = strSql;
MyCommand. ExecuteNonQuery ();

// Clear the scene
MyConn. Close ();
}
Catch (Exception e)
{
Throw (new Exception ("error saving investigation:" + e. ToString ()));
}

}

/// <Summary>
/// Vote
/// </Summary>
/// <Param name = "a_intID"> </param>
Public override void Vote (int a_intID)
{
// Add one to the count
(SurveyItem) m_arrItems [a_intID]). Count + = 1;

// Changes in the database
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 ("failed to update the survey item:" + e. ToString ()));
}

}

/// <Summary>
/// Survey list
/// </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 ("Retrieving from database failed:" +

E. ToString ()));
}

// Return results
Return arrResult;
}

/// <Summary>
/// Obtain the active survey 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 ("inactive investigation "));
}

MyReader. Close ();

}
Catch (Exception e)
{
Throw (new Exception ("Retrieving from database failed:" +

E. ToString ()));
}
Finally
{
MyConn. Close ();
}

// Return results
Return strResult;

}

/// <Summary>
/// Activate the survey
/// </Summary>
/// <Param name = "a_strSurveyID"> Survey No. </param>
Public static void ActiveSurvey (string a_strSurveyID)
{
MyClass. Util. MyConnection myCo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.