📄 votes.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
namespace Wrox.WebModules.Polls.Data
{
public class VoteDetails
{
public int VoteID;
public int QuestionID;
public int OptionID;
public string UserIP;
public DateTime AddedDate;
}
public class Votes : Wrox.WebModules.Data.DbObject
{
public Votes(string newConnectionString) : base(newConnectionString)
{ }
// return all the votes of the specified question
public DataSet GetVotes(int questionID)
{
// create the parameter
SqlParameter[] parameters = { new SqlParameter("@QuestionID", SqlDbType.Int, 4) };
parameters[0].Value = questionID;
return RunProcedure("sp_Polls_GetVotes", parameters, "Votes");
}
// return the row with all the details of the specified option
public VoteDetails GetDetails(int voteID)
{
// create the parameter
SqlParameter[] parameters = { new SqlParameter("@VoteID", SqlDbType.Int, 4) };
parameters[0].Value = voteID;
using(DataSet votes = RunProcedure("sp_Polls_GetVoteDetails", parameters, "Votes"))
{
VoteDetails details = new VoteDetails();
// if the record was found, set the properties of the class instance
if (votes.Tables[0].Rows.Count > 0)
{
DataRow rowVote = votes.Tables[0].Rows[0];
details.VoteID = (int)rowVote["VoteID"];
details.QuestionID = (int)rowVote["QuestionID"];
details.OptionID = (int)rowVote["OptionID"];
details.UserIP = rowVote["UserIP"].ToString();
details.AddedDate = Convert.ToDateTime(rowVote["AddedDate"]);
}
else
details.VoteID = -1;
return details;
}
}
// return the row with all the details of the specified option
public DataRow GetDetailsRow(int voteID)
{
// create the parameter
SqlParameter[] parameters = { new SqlParameter("@VoteID", SqlDbType.Int, 4) };
parameters[0].Value = voteID;
using(DataSet votes = RunProcedure("sp_Polls_GetVoteDetails", parameters, "Votes"))
{
return votes.Tables[0].Rows[0];
}
}
// return the Vote ID for the specified IP (identifies an user/computer)
public int GetUserVoteID(int questionID, string userIP)
{
int numAffected;
// create the parameters
SqlParameter[] parameters = {
new SqlParameter("@QuestionID", SqlDbType.Int, 4),
new SqlParameter("@UserIP", SqlDbType.VarChar, 15),
new SqlParameter("@VoteID", SqlDbType.Int, 4),
};
// set the values
parameters[0].Value = questionID;
parameters[1].Value = userIP.Trim();
parameters[2].Direction = ParameterDirection.Output;
// run the procedure
RunProcedure("sp_Polls_GetUserVoteID", parameters, out numAffected);
return (int)parameters[2].Value;
}
// add a new vote for the specified question, and log the IP address as well
public int Add(int questionID, int optionID, string userIP)
{
int numAffected;
// create the parameters
SqlParameter[] parameters = {
new SqlParameter("@QuestionID", SqlDbType.Int, 4),
new SqlParameter("@OptionID", SqlDbType.Int, 4),
new SqlParameter("@UserIP", SqlDbType.VarChar, 15),
new SqlParameter("@VoteID", SqlDbType.Int, 4),
};
// set the values
parameters[0].Value = questionID;
parameters[1].Value = optionID;
parameters[2].Value = userIP.Trim();
parameters[3].Direction = ParameterDirection.Output;
// run the procedure
RunProcedure("sp_Polls_InsertVote", parameters, out numAffected);
return (int)parameters[3].Value;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -