⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 options.cs

📁 wrox c#高级编程
💻 CS
字号:
using System;
using System.Data;
using System.Data.SqlClient;

namespace Wrox.WebModules.Polls.Data
{
	public class OptionDetails
	{
		public int OptionID;
		public int QuestionID;
		public string OptionText;
		public int TotalVotes;
		public float Percentage;
	}

	public class Options : Wrox.WebModules.Data.DbObject
	{
		public Options(string newConnectionString) : base(newConnectionString)
		{	}
	
		// return all the options of the specified question
		public DataSet GetOptions(int questionID)
		{
			// create the parameter
			SqlParameter[] parameters = { new SqlParameter("@QuestionID", SqlDbType.Int, 4) };
			parameters[0].Value = questionID;
			
			return RunProcedure("sp_Polls_GetOptions", parameters, "Options");
		}
		
		
		// return the row with all the details of the specified option
		public OptionDetails GetDetails(int optionID)
		{
			// create the parameter
			SqlParameter[] parameters = { new SqlParameter("@OptionID", SqlDbType.Int, 4) };
			parameters[0].Value = optionID;
			
			using(DataSet options = RunProcedure("sp_Polls_GetOptionDetails", parameters, "Options"))
			{
				OptionDetails details = new OptionDetails();
				// if the record was found, set the properties of the class instance
				if (options.Tables[0].Rows.Count > 0)
				{
					DataRow rowOption = options.Tables[0].Rows[0];
					details.OptionID = (int)rowOption["OptionID"];
					details.QuestionID = (int)rowOption["QuestionID"];
					details.OptionText = rowOption["OptionText"].ToString();
					details.TotalVotes = (int)rowOption["TotalVotes"];
					details.Percentage = (float)((System.Decimal)rowOption["Percentage"]);
				}
				else
					details.OptionID = -1;

				return details;
			}
		}

		// return the row with all the details of the specified option
		public DataRow GetDetailsRow(int optionID)
		{
			// create the parameter
			SqlParameter[] parameters = { new SqlParameter("@OptionID", SqlDbType.Int, 4) };
			parameters[0].Value = optionID;
			
			using(DataSet options = RunProcedure("sp_Polls_GetOptionDetails", parameters, "Options"))
			{
				return options.Tables[0].Rows[0];
			}
		}


		// delete the option identified by the specified ID
		public bool Delete(int optionID)
		{
			int numAffected;
		
			// create the parameter
			SqlParameter[] parameters = { new SqlParameter("@OptionID", SqlDbType.Int, 4) };
			parameters[0].Value = optionID;
			
			RunProcedure("sp_Polls_DeleteOption", parameters, out numAffected);

			return (numAffected == 1);
		}


		// update FirstName, LastName and Email of the user identified by the subscription ID
		public bool Update(int optionID, string optionText)
		{
			int numAffected;
		
			// create the parameter
			SqlParameter[] parameters = {
				new SqlParameter("@OptionID", SqlDbType.Int, 4),
				new SqlParameter("@OptionText", SqlDbType.VarChar, 150)
			};
			
			// set the values
			parameters[0].Value = optionID;
			parameters[1].Value = optionText.Trim();

			RunProcedure("sp_Polls_UpdateOption", parameters, out numAffected);

			return (numAffected == 1);
		}


		// add a new subscription
		public int Add(int questionID, string optionText)
		{
			int numAffected;
	
			// create the parameters
			SqlParameter[] parameters = { 
				new SqlParameter("@QuestionID", SqlDbType.Int, 4),
				new SqlParameter("@OptionText", SqlDbType.VarChar, 150),
				new SqlParameter("@OptionID", SqlDbType.Int, 4),
			};	
			
			// set the values
			parameters[0].Value = questionID;
			parameters[1].Value = optionText.Trim();
			parameters[2].Direction = ParameterDirection.Output;

			// run the procedure
			RunProcedure("sp_Polls_InsertOption", parameters, out numAffected);

			return (int)parameters[2].Value;
		}	
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -