UserManage.asmx.cs
上传用户:jxqhsy
上传日期:2020-12-31
资源大小:1793k
文件大小:35k
源码类别:

SilverLight

开发平台:

HTML/CSS

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Services;
  6. using System.Data.SqlClient;
  7. using System.Configuration;
  8. using System.Text;
  9. namespace SFGS.Web
  10. {
  11.     /// <summary>
  12.     /// UserManage 的摘要说明
  13.     /// </summary>
  14.     [WebService(Namespace = "http://tempuri.org/")]
  15.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  16.     [System.ComponentModel.ToolboxItem(false)]
  17.     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
  18.     // [System.Web.Script.Services.ScriptService]
  19.     public class UserManage : System.Web.Services.WebService
  20.     {
  21.         /// <summary>
  22.         /// 创建管理员用户
  23.         /// </summary>
  24.         /// <param name="userName"></param>
  25.         /// <param name="userPwd"></param>
  26.         /// <param name="beizhu"></param>
  27.         /// <returns></returns>
  28.         [WebMethod]
  29.         public bool AddUser(string userName, string userPwd, string beizhu)
  30.         {
  31.             try
  32.             {
  33.                 int count = 0;
  34.                 DAL.DBHelper db = new DAL.DBHelper();
  35.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("select * from Admin where UserName='" + userName.ToString().Replace("'", "''") + "'");
  36.                 System.Data.Common.DbDataReader dr = db.ExecuteReader(cmd);
  37.                 while (dr.Read() == true)
  38.                 {
  39.                     count++;
  40.                 }
  41.                 dr.Close();
  42.                 dr.Dispose();
  43.                 cmd.Dispose();
  44.                 if (count > 0) return false;//如果已存用户返回false;
  45.                 cmd = db.GetSqlStringCommond(
  46.                     "INSERT INTO Admin VALUES ('" +
  47.                     userName.ToString().Replace("'", "''") + "','" +
  48.                     userPwd.ToString().Replace("'", "''") + "','" +
  49.                     beizhu.ToString().Replace("'", "''") + "')");
  50.                 db.ExecuteNonQuery(cmd);
  51.                 db.Close();
  52.                 cmd.Dispose();
  53.                 return true;
  54.             }
  55.             catch (Exception ex)
  56.             {
  57.                 return false;
  58.             }
  59.         }
  60.         /// <summary>
  61.         /// 管理员登录
  62.         /// </summary>
  63.         /// <returns></returns>
  64.         [WebMethod]
  65.         public bool LoginAdmin(string userName, string userPwd)
  66.         {
  67.             try
  68.             {
  69.                 int count = 0;
  70.                 DAL.DBHelper db = new DAL.DBHelper();
  71.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("select * from Admin where UserName='" + userName.ToString().Replace("'", "''") + "' AND PassWord='" + userPwd.ToString().Replace("'", "''") + "'");
  72.                 System.Data.Common.DbDataReader dr = db.ExecuteReader(cmd);
  73.                 while (dr.Read() == true)
  74.                 {
  75.                     count++;
  76.                 }
  77.                 if (count > 0)
  78.                 {
  79.                     return true;
  80.                 }
  81.                 else
  82.                 {
  83.                     return false;
  84.                 }
  85.                 dr.Close();
  86.                 dr.Dispose();
  87.                 db.Close();
  88.                 cmd.Dispose();
  89.             }
  90.             catch (Exception ex)
  91.             {
  92.                 return false;
  93.             }
  94.         }
  95.         /// <summary>
  96.         /// 更新用户表
  97.         /// </summary>
  98.         /// <param name="userName"></param>
  99.         /// <param name="userPwd"></param>
  100.         /// <returns></returns>
  101.         [WebMethod]
  102.         public bool UpdateUser(string userName, string userPwd)
  103.         {
  104.             try
  105.             {
  106.                 int count = 0;
  107.                 DAL.DBHelper db = new DAL.DBHelper();
  108.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("select * from Admin where UserName='" + userName.ToString().Replace("'", "''") + "'");
  109.                 System.Data.Common.DbDataReader dr = db.ExecuteReader(cmd);
  110.                 while (dr.Read() == true)
  111.                 {
  112.                     count++;
  113.                 }
  114.                 dr.Close();
  115.                 dr.Dispose();
  116.                 cmd.Dispose();
  117.                 if (count <= 0) return false;//如果用户不存在则返回false;
  118.                 cmd = db.GetSqlStringCommond("update admin set PassWord='" + userPwd.ToString().Replace("'", "''") + "' where UserName='" + userName.ToString().Replace("'", "''") + "'");
  119.                 int number = db.ExecuteNonQuery(cmd);
  120.                 db.Close();
  121.                 cmd.Dispose();
  122.                 if (number > 0)
  123.                     return true;
  124.                 else
  125.                     return false;
  126.             }
  127.             catch (System.Exception ex)
  128.             {
  129.                 return false;
  130.             }
  131.         }
  132.         /// <summary>
  133.         /// 删除指定用户
  134.         /// </summary>
  135.         /// <param name="userName"></param>
  136.         /// <returns></returns>
  137.         [WebMethod]
  138.         public bool DeleteUser(string userName)
  139.         {
  140.             try
  141.             {
  142.                 int count = 0;
  143.                 DAL.DBHelper db = new DAL.DBHelper();
  144.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("select * from Admin where UserName='" + userName.ToString().Replace("'", "''") + "'");
  145.                 System.Data.Common.DbDataReader dr = db.ExecuteReader(cmd);
  146.                 while (dr.Read() == true)
  147.                 {
  148.                     count++;
  149.                 }
  150.                 dr.Close();
  151.                 dr.Dispose();
  152.                 cmd.Dispose();
  153.                 if (count <= 0) return false;//如果用户不存在则返回false;
  154.                 cmd = db.GetSqlStringCommond("delete from Admin where UserName='" + userName.ToString().Replace("'", "''") + "'");
  155.                 int number = db.ExecuteNonQuery(cmd);
  156.                 db.Close();
  157.                 cmd.Dispose();
  158.                 if (number > 0)
  159.                     return true;
  160.                 else
  161.                     return false;
  162.             }
  163.             catch (System.Exception ex)
  164.             {
  165.                 return false;
  166.             }
  167.         }
  168.         /// <summary>
  169.         /// 更新题目表
  170.         /// </summary>
  171.         /// <param name="ID"></param>
  172.         /// <param name="SurveyID"></param>
  173.         /// <param name="text"></param>
  174.         /// <param name="OptionA"></param>
  175.         /// <param name="OptionB"></param>
  176.         /// <param name="OptionC"></param>
  177.         /// <param name="OptionD"></param>
  178.         /// <returns></returns>
  179.         [WebMethod]
  180.         public bool UpdateQuestion(string ID, string SurveyID, string text, string OptionA, string OptionB, string OptionC, string OptionD)
  181.         {
  182.             try
  183.             {
  184.                 int count = 0;
  185.                 DAL.DBHelper db = new DAL.DBHelper();
  186.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("select * from Question where ID=" + ID.ToString().Replace("'", "''"));
  187.                 System.Data.Common.DbDataReader dr = db.ExecuteReader(cmd);
  188.                 while (dr.Read() == true)
  189.                 {
  190.                     count++;
  191.                 }
  192.                 dr.Close();
  193.                 dr.Dispose();
  194.                 cmd.Dispose();
  195.                 if (count <= 0) return false;//如果问题不存在返回false;
  196.                 cmd = db.GetSqlStringCommond("update Question set " +
  197.                                              "SurveyID=" + SurveyID.ToString().Replace("'", "''") + "," +
  198.                                              "Text='" + text.ToString().Replace("'", "''") + "'," +
  199.                                              "OptionA='" + OptionA.ToString().Replace("'", "''") + "'," +
  200.                                              "OptionB='" + OptionB.ToString().Replace("'", "''") + "'," +
  201.                                              "OptionC='" + OptionC.ToString().Replace("'", "''") + "'," +
  202.                                              "OptionD='" + OptionD.ToString().Replace("'", "''") + "' " +
  203.                                              "where ID=" + ID.ToString().Replace("'", "''")
  204.                                              );
  205.                 int number = db.ExecuteNonQuery(cmd);
  206.                 db.Close();
  207.                 cmd.Dispose();
  208.                 if (number > 0)
  209.                     return true;
  210.                 else
  211.                     return false;
  212.             }
  213.             catch (System.Exception ex)
  214.             {
  215.                 return false;
  216.             }
  217.         }
  218.         /// <summary>
  219.         /// 添加新问题
  220.         /// </summary>
  221.         /// <param name="SurveyID"></param>
  222.         /// <param name="text"></param>
  223.         /// <param name="OptionA"></param>
  224.         /// <param name="OptionB"></param>
  225.         /// <param name="OptionC"></param>
  226.         /// <param name="OptionD"></param>
  227.         /// <returns></returns>
  228.         [WebMethod]
  229.         public bool AddQuestion(string SurveyID, string text, string OptionA, string OptionB, string OptionC, string OptionD)
  230.         {
  231.             try
  232.             {
  233.                 int count = 0;
  234.                 DAL.DBHelper db = new DAL.DBHelper();
  235.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("select * from Survey where ID=" + SurveyID.ToString().Replace("'", "''"));
  236.                 System.Data.Common.DbDataReader dr = db.ExecuteReader(cmd);
  237.                 while (dr.Read() == true)
  238.                 {
  239.                     count++;
  240.                 }
  241.                 dr.Close();
  242.                 dr.Dispose();
  243.                 dr = null;
  244.                 cmd.Dispose();
  245.                 if (count <= 0) return false;//如果调查表中不存在ID为SurveyID的记录则返回false;
  246.                 count = 0;
  247.                 cmd = db.GetSqlStringCommond("select * from Question where SurveyID=" + SurveyID.ToString().Replace("'", "''") + " and Text='" + text.ToString().Replace("'", "''") + "'");
  248.                 dr = db.ExecuteReader(cmd);
  249.                 while (dr.Read() == true)
  250.                 {
  251.                     count++;
  252.                 }
  253.                 dr.Close();
  254.                 dr.Dispose();
  255.                 cmd.Dispose();
  256.                 if (count > 0) return false;//如果问题已存在则返回false;
  257.                 cmd = db.GetSqlStringCommond("insert into Question values(" +
  258.                                               SurveyID.ToString().Replace("'", "''") + ",'" +
  259.                                               text.ToString().Replace("'", "''") + "','" +
  260.                                               OptionA.ToString().Replace("'", "''") + "','" +
  261.                                               OptionB.ToString().Replace("'", "''") + "','" +
  262.                                               OptionC.ToString().Replace("'", "''") + "','" +
  263.                                               OptionD.ToString().Replace("'", "''") + "')");
  264.                 int number = db.ExecuteNonQuery(cmd);
  265.                 db.Close();
  266.                 cmd.Dispose();
  267.                 if (number > 0)
  268.                     return true;
  269.                 else
  270.                     return false;
  271.             }
  272.             catch (System.Exception ex)
  273.             {
  274.                 return false;
  275.             }
  276.         }
  277.         /// <summary>
  278.         /// 删除指定题目
  279.         /// </summary>
  280.         /// <param name="ID"></param>
  281.         /// <returns></returns>
  282.         [WebMethod]
  283.         public bool DeleteQuestion(string ID)
  284.         {
  285.             try
  286.             {
  287.                 int count = 0;
  288.                 DAL.DBHelper db = new DAL.DBHelper();
  289.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("select * from Question where ID=" + ID.ToString().Replace("'", "''"));
  290.                 System.Data.Common.DbDataReader dr = db.ExecuteReader(cmd);
  291.                 while (dr.Read() == true)
  292.                 {
  293.                     count++;
  294.                 }
  295.                 dr.Close();
  296.                 dr.Dispose();
  297.                 dr = null;
  298.                 cmd.Dispose();
  299.                 if (count <= 0) return false;//如果调查表中不存在ID为SurveyID的记录则返回false;
  300.                 cmd = db.GetSqlStringCommond("delete from Question where ID=" + ID.ToString().Replace("'", "''"));
  301.                 int number = db.ExecuteNonQuery(cmd);
  302.                 db.Close();
  303.                 cmd.Dispose();
  304.                 if (number > 0)
  305.                     return true;
  306.                 else
  307.                     return false;
  308.             }
  309.             catch (System.Exception ex)
  310.             {
  311.                 return false;
  312.             }
  313.         }
  314.         /// <summary>
  315.         /// 读取指定调查的问题
  316.         /// </summary>
  317.         /// <param name="SurveyID"></param>
  318.         /// <returns></returns>
  319.         [WebMethod]
  320.         public string ReadQuestion(string SurveyID)
  321.         {
  322.             try
  323.             {
  324.                 int count = 0;
  325.                 DAL.DBHelper db = new DAL.DBHelper();
  326.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("select * from Question where SurveyID=" + SurveyID.ToString().Replace("'", "''"));
  327.                 System.Data.DataSet ds = db.ExecuteDataSet(cmd);
  328.                 if (ds.Tables[0].Rows.Count <= 0)
  329.                 {
  330.                     return "";
  331.                 }
  332.                 System.Text.StringBuilder sb = new StringBuilder();
  333.                 sb.Append("<?xml version="1.0" encoding="utf-8" ?>");
  334.                 sb.Append("<Questions>");
  335.                 foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
  336.                 {
  337.                     sb.Append("<Question>");
  338.                     sb.Append("<ID>");
  339.                     sb.Append(dr["ID"].ToString());
  340.                     sb.Append("</ID>");
  341.                     sb.Append("<SurveyID>");
  342.                     sb.Append(dr["SurveyID"].ToString());
  343.                     sb.Append("</SurveyID>");
  344.                     sb.Append("<Text>");
  345.                     sb.Append(dr["Text"].ToString());
  346.                     sb.Append("</Text>");
  347.                     sb.Append("<OptionA>");
  348.                     sb.Append(dr["OptionA"].ToString());
  349.                     sb.Append("</OptionA>");
  350.                     sb.Append("<OptionB>");
  351.                     sb.Append(dr["OptionB"].ToString());
  352.                     sb.Append("</OptionB>");
  353.                     sb.Append("<OptionC>");
  354.                     sb.Append(dr["OptionC"].ToString());
  355.                     sb.Append("</OptionC>");
  356.                     sb.Append("<OptionD>");
  357.                     sb.Append(dr["OptionD"].ToString());
  358.                     sb.Append("</OptionD>");
  359.                     sb.Append("</Question>");
  360.                 }
  361.                 sb.Append("</Questions>");
  362.                 ds.Dispose();
  363.                 cmd.Dispose();
  364.                 db.Close();
  365.                 return sb.ToString();
  366.             }
  367.             catch (System.Exception ex)
  368.             {
  369.                 return "";
  370.             }
  371.         }
  372.         /// <summary>
  373.         /// 更新指定ID的调查表
  374.         /// </summary>
  375.         /// <param name="ID"></param>
  376.         /// <param name="Name"></param>
  377.         /// <param name="Description"></param>
  378.         /// <param name="Date"></param>
  379.         /// <param name="IsCurrentSurvey"></param>
  380.         /// <returns></returns>
  381.         [WebMethod]
  382.         public bool UpdateSurvey(string ID, string Name, string Description, string Date, string IsCurrentSurvey)
  383.         {
  384.             try
  385.             {
  386.                 int count = 0;
  387.                 DAL.DBHelper db = new DAL.DBHelper();
  388.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("select * from Survey where ID=" + ID.ToString().Replace("'", "''"));
  389.                 System.Data.Common.DbDataReader dr = db.ExecuteReader(cmd);
  390.                 while (dr.Read() == true)
  391.                 {
  392.                     count++;
  393.                 }
  394.                 dr.Close();
  395.                 dr.Dispose();
  396.                 cmd.Dispose();
  397.                 cmd = null;
  398.                 if (count <= 0) return false;
  399.                 cmd = db.GetSqlStringCommond("update Survey set " +
  400.                                                "Name='" + Name.ToString().Replace("'", "''") + "'," +
  401.                                                "Description='" + Description.ToString().Replace("'", "''") + "'," +
  402.                                                "Date='" + Date.ToString().Replace("'", "''") + "'," +
  403.                                                "IsCurrentSurvey='" + IsCurrentSurvey.ToString().Replace("'", "''") + "'" +
  404.                                                " where ID=" + ID.ToString().Replace("'", "''"));
  405.                 int number = db.ExecuteNonQuery(cmd);
  406.                 if (number > 0)
  407.                     return true;
  408.                 else
  409.                     return false;
  410.             }
  411.             catch (System.Exception ex)
  412.             {
  413.                 return false;
  414.             }
  415.         }
  416.         /// <summary>
  417.         /// 添加新的调查
  418.         /// </summary>
  419.         /// <param name="Name"></param>
  420.         /// <param name="Description"></param>
  421.         /// <param name="Date"></param>
  422.         /// <param name="IsCurrentSurvey"></param>
  423.         /// <returns></returns>
  424.         [WebMethod]
  425.         public bool AddSurvey(string Name, string Description, string Date, string IsCurrentSurvey)
  426.         {
  427.             try
  428.             {
  429.                 int count = 0;
  430.                 DAL.DBHelper db = new DAL.DBHelper();
  431.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("select * from Survey where Name='" + Name.ToString().Replace("'", "''") + "'");
  432.                 System.Data.Common.DbDataReader dr = db.ExecuteReader(cmd);
  433.                 while (dr.Read() == true)
  434.                 {
  435.                     count++;
  436.                 }
  437.                 dr.Close();
  438.                 dr.Dispose();
  439.                 cmd.Dispose();
  440.                 cmd = null;
  441.                 if (count > 0) return false;//如果该记录已经存在
  442.                 cmd = db.GetSqlStringCommond("insert into Survey values(" +
  443.                                              "'" + Name.ToString().Replace("'", "''") + "'," +
  444.                                              "'" + Description.ToString().Replace("'", "''") + "'," +
  445.                                              "'" + Date.ToString().Replace("'", "''") + "'," +
  446.                                              "'" + IsCurrentSurvey.ToString().Replace("'", "''") + "')" +
  447.                                              "");
  448.                 int number = db.ExecuteNonQuery(cmd);
  449.                 db.Close();
  450.                 cmd.Dispose();
  451.                 if (number > 0)
  452.                     return true;
  453.                 else
  454.                     return false;
  455.             }
  456.             catch (System.Exception ex)
  457.             {
  458.                 return false;
  459.             }
  460.         }
  461.         /// <summary>
  462.         /// 删除指定ID的调查记录,及其关联的题目记录
  463.         /// </summary>
  464.         /// <param name="ID"></param>
  465.         /// <returns></returns>
  466.         [WebMethod]
  467.         public bool DeleteSurvey(string ID)
  468.         {
  469.             try
  470.             {
  471.                 int count = 0;
  472.                 DAL.DBHelper db = new DAL.DBHelper();
  473.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("select * from Survey where ID=" + ID.ToString().Replace("'", "''"));
  474.                 System.Data.Common.DbDataReader dr = db.ExecuteReader(cmd);
  475.                 while (dr.Read() == true)
  476.                 {
  477.                     count++;
  478.                 }
  479.                 dr.Close();
  480.                 dr.Dispose();
  481.                 cmd.Dispose();
  482.                 cmd = null;
  483.                 if (count <= 0) return false;
  484.                 cmd = db.GetSqlStringCommond("delete from Survey where ID=" + ID.ToString().Replace("'", "''"));
  485.                 int number1 = db.ExecuteNonQuery(cmd);
  486.                 cmd.Dispose();
  487.                 cmd = db.GetSqlStringCommond("delete from Question where SurveyID=" + ID.ToString().Replace("'", "''"));
  488.                 int number2 = db.ExecuteNonQuery(cmd);
  489.                 db.Close();
  490.                 cmd.Dispose();
  491.                 if (number1 > 0 || number2 > 0)
  492.                 {
  493.                     return true;
  494.                 }
  495.                 else
  496.                 {
  497.                     return false;
  498.                 }
  499.             }
  500.             catch (System.Exception ex)
  501.             {
  502.                 return false;
  503.             }
  504.         }
  505.         /// <summary>
  506.         /// 取指定ID的调查
  507.         /// </summary>
  508.         /// <param name="ID"></param>
  509.         /// <returns></returns>
  510.         [WebMethod]
  511.         public string ReadSurvey(string ID)
  512.         {
  513.             try
  514.             {
  515.                 int count = 0;
  516.                 DAL.DBHelper db = new DAL.DBHelper();
  517.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("select * from Survey where ID=" + ID.ToString().Replace("'", "''"));
  518.                 System.Data.DataSet ds = db.ExecuteDataSet(cmd);
  519.                 if (ds.Tables[0].Rows.Count <= 0)
  520.                 {
  521.                     return "";
  522.                 }
  523.                 System.Text.StringBuilder sb = new StringBuilder();
  524.                 sb.Append("<?xml version="1.0" encoding="utf-8" ?>");
  525.                 sb.Append("<Surveys>");
  526.                 foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
  527.                 {
  528.                     sb.Append("<Survey>");
  529.                     sb.Append("<ID>");
  530.                     sb.Append(dr["ID"].ToString());
  531.                     sb.Append("</ID>");
  532.                     sb.Append("<Name>");
  533.                     sb.Append(dr["Name"].ToString());
  534.                     sb.Append("</Name>");
  535.                     sb.Append("<Description>");
  536.                     sb.Append(dr["Description"].ToString());
  537.                     sb.Append("</Description>");
  538.                     sb.Append("<Date>");
  539.                     sb.Append(dr["Date"].ToString());
  540.                     sb.Append("</Date>");
  541.                     sb.Append("<IsCurrentSurvey>");
  542.                     sb.Append(dr["IsCurrentSurvey"].ToString());
  543.                     sb.Append("</IsCurrentSurvey>");
  544.                     sb.Append("</Survey>");
  545.                 }
  546.                 sb.Append("</Surveys>");
  547.                 ds.Dispose();
  548.                 cmd.Dispose();
  549.                 db.Close();
  550.                 return sb.ToString();
  551.             }
  552.             catch (System.Exception ex)
  553.             {
  554.                 return "";
  555.             }
  556.         }
  557.         /// <summary>
  558.         /// 取指定ID的调查
  559.         /// </summary>
  560.         /// <param name="ID"></param>
  561.         /// <returns></returns>
  562.         [WebMethod]
  563.         public string ReadAllSurvey()
  564.         {
  565.             try
  566.             {
  567.                 int count = 0;
  568.                 string response = "";
  569.                 DAL.DBHelper db = new DAL.DBHelper();
  570.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("select * from Survey");
  571.                 System.Data.DataSet ds = db.ExecuteDataSet(cmd);
  572.                 if (ds.Tables[0].Rows.Count <= 0)
  573.                 {
  574.                     return "";
  575.                 }
  576.                 System.Text.StringBuilder sb = new StringBuilder();
  577.                 sb.Append("<?xml version="1.0" encoding="utf-8" ?>");
  578.                 sb.Append("<Surveys>");
  579.                 foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
  580.                 {
  581.                     cmd = db.GetSqlStringCommond("select count(*) from Response where QuestionID =(select TOP 1 ID from Question where SurveyID =" + dr["ID"].ToString() + ")");
  582.                     System.Data.DataSet ds1= db.ExecuteDataSet(cmd);
  583.                     if (ds1.Tables[0].Rows.Count<=0)
  584.                     {
  585.                         response = "0";
  586.                     }
  587.                     else
  588.                     {
  589.                         response = ds1.Tables[0].Rows[0][0].ToString();
  590.                     }
  591.                     sb.Append("<Survey>");
  592.                     sb.Append("<ID>");
  593.                     sb.Append(dr["ID"].ToString());
  594.                     sb.Append("</ID>");
  595.                     sb.Append("<Name>");
  596.                     sb.Append(dr["Name"].ToString());
  597.                     sb.Append("</Name>");
  598.                     sb.Append("<Description>");
  599.                     sb.Append(dr["Description"].ToString());
  600.                     sb.Append("</Description>");
  601.                     sb.Append("<Responses>");
  602.                     sb.Append(response);
  603.                     sb.Append("</Responses>");
  604.                     sb.Append("<Date>");
  605.                     sb.Append(dr["Date"].ToString());
  606.                     sb.Append("</Date>");
  607.                     sb.Append("<IsCurrentSurvey>");
  608.                     sb.Append(dr["IsCurrentSurvey"].ToString());
  609.                     sb.Append("</IsCurrentSurvey>");
  610.                     sb.Append("</Survey>");
  611.                 }
  612.                 sb.Append("</Surveys>");
  613.                 ds.Dispose();
  614.                 cmd.Dispose();
  615.                 db.Close();
  616.                 return sb.ToString();
  617.             }
  618.             catch (System.Exception ex)
  619.             {
  620.                 return "";
  621.             }
  622.         }
  623.         [WebMethod]
  624.         public bool UpdateResponse()
  625.         {
  626.             return false;
  627.         }
  628.         /// <summary>
  629.         /// 添加记录
  630.         /// </summary>
  631.         /// <param name="QuestionID"></param>
  632.         /// <param name="Selection"></param>
  633.         /// <returns></returns>
  634.         [WebMethod]
  635.         public bool AddResponse(string QuestionID, string Selection)
  636.         {
  637.             try
  638.             {
  639.                 int count = 0;
  640.                 DAL.DBHelper db = new DAL.DBHelper();
  641.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("select * from Question where ID=" + QuestionID.ToString());
  642.                 System.Data.Common.DbDataReader dr = db.ExecuteReader(cmd);
  643.                 while (dr.Read() == true)
  644.                 {
  645.                     count++;
  646.                 }
  647.                 dr.Close();
  648.                 dr.Dispose();
  649.                 cmd.Dispose();
  650.                 cmd = null;
  651.                 if (count <= 0) return false;
  652.                 cmd = db.GetSqlStringCommond("insert into Response values(" +
  653.                                                                           QuestionID.ToString() + ",'" +
  654.                                                                           Selection.ToString().Replace("'", "''") +
  655.                                                                           "')");
  656.                 int number = db.ExecuteNonQuery(cmd);
  657.                 db.Close();
  658.                 cmd.Dispose();
  659.                 if (number > 0) return true;
  660.                 else return false;
  661.             }
  662.             catch (System.Exception ex)
  663.             {
  664.                 return false;
  665.             }
  666.         }
  667.         /// <summary>
  668.         /// 删除指定ID的记录
  669.         /// </summary>
  670.         /// <param name="ID"></param>
  671.         /// <returns></returns>
  672.         [WebMethod]
  673.         public bool DeleteResponse(string ID)
  674.         {
  675.             try
  676.             {
  677.                 DAL.DBHelper db = new DAL.DBHelper();
  678.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("delete from Response where ID=" + ID.ToString());
  679.                 int number = db.ExecuteNonQuery(cmd);
  680.                 db.Close();
  681.                 cmd.Dispose();
  682.                 if (number > 0) return true;
  683.                 else return false;
  684.             }
  685.             catch (System.Exception ex)
  686.             {
  687.                 return false;
  688.             }
  689.         }
  690.         [WebMethod]
  691.         public string PercentResponse(string SurveyID)
  692.         {
  693.             try
  694.             {
  695.                 DAL.DBHelper db = new DAL.DBHelper();
  696.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("select * from Question where SurveyID=" + SurveyID.ToString());
  697.                 System.Data.DataSet ds = db.ExecuteDataSet(cmd);
  698.                 if (ds.Tables[0].Rows.Count <= 0)
  699.                 {
  700.                     return "";//如果没有找到,则返回空
  701.                 }
  702.                 System.Text.StringBuilder sb = new StringBuilder();
  703.                 sb.Append("<?xml version="1.0" encoding="utf-8" ?>");
  704.                 sb.Append("<Percents>");
  705.                 foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
  706.                 {
  707.                     int count = 0;
  708.                     int countA = 0;
  709.                     int countB = 0;
  710.                     int countC = 0;
  711.                     int countD = 0;
  712.                     System.Data.DataSet tmpDs;
  713.                     sb.Append("<Percent>");
  714.                     sb.Append("<ID>");
  715.                     sb.Append(dr["ID"].ToString());
  716.                     sb.Append("</ID>");
  717.                     cmd = db.GetSqlStringCommond("select * from Response where QuestionID=" + dr["ID"].ToString());
  718.                     tmpDs = db.ExecuteDataSet(cmd);
  719.                     count = tmpDs.Tables[0].Rows.Count;
  720.                     if (count <= 0)
  721.                     {
  722.                         sb.Append("<A>0</A><B>0</B><C>0</C><D>0</D>");
  723.                     }
  724.                     else
  725.                     {
  726.                         cmd = db.GetSqlStringCommond("select * from Response where QuestionID=" + dr["ID"].ToString() + " and (Selection='a' or Selection='A')");
  727.                         tmpDs = db.ExecuteDataSet(cmd);
  728.                         countA = tmpDs.Tables[0].Rows.Count;
  729.                         cmd = db.GetSqlStringCommond("select * from Response where QuestionID=" + dr["ID"].ToString() + " and (Selection='b' or Selection='B')");
  730.                         tmpDs = db.ExecuteDataSet(cmd);
  731.                         countB = tmpDs.Tables[0].Rows.Count;
  732.                         cmd = db.GetSqlStringCommond("select * from Response where QuestionID=" + dr["ID"].ToString() + " and (Selection='c' or Selection='C')");
  733.                         tmpDs = db.ExecuteDataSet(cmd);
  734.                         countC = tmpDs.Tables[0].Rows.Count;
  735.                         cmd = db.GetSqlStringCommond("select * from Response where QuestionID=" + dr["ID"].ToString() + " and (Selection='d' or Selection='D')");
  736.                         tmpDs = db.ExecuteDataSet(cmd);
  737.                         countD = tmpDs.Tables[0].Rows.Count;
  738.                         sb.Append("<A>");
  739.                         sb.Append(Convert.ToString((float)countA / count * 100));
  740.                         sb.Append("</A>");
  741.                         sb.Append("<B>");
  742.                         sb.Append(Convert.ToString((float)countB / count * 100));
  743.                         sb.Append("</B>");
  744.                         sb.Append("<C>");
  745.                         sb.Append(Convert.ToString((float)countC / count * 100));
  746.                         sb.Append("</C>");
  747.                         sb.Append("<D>");
  748.                         sb.Append(Convert.ToString((float)countD / count * 100));
  749.                         sb.Append("</D>");
  750.                     }
  751.                     sb.Append("</Percent>");
  752.                 }
  753.                 sb.Append("</Percents>");
  754.                 return sb.ToString();
  755.             }
  756.             catch (System.Exception ex)
  757.             {
  758.                 return "";
  759.             }
  760.         }
  761.         /// <summary>
  762.         /// 读当前调查名
  763.         /// </summary>
  764.         /// <returns></returns>
  765.         [WebMethod]
  766.         public string ReadCurrentSurvey()
  767.         {
  768.             try
  769.             {
  770.                 DAL.DBHelper db = new DAL.DBHelper();
  771.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("select TOP 1 Name from Survey where IsCurrentSurvey='1'");
  772.                 System.Data.DataSet ds = db.ExecuteDataSet(cmd);
  773.                 if (ds.Tables[0].Rows.Count<=0)
  774.                 {
  775.                     return "";
  776.                 }
  777.                 return ds.Tables[0].Rows[0]["Name"].ToString();
  778.             }
  779.             catch (System.Exception ex)
  780.             {
  781.                 return "";
  782.             }            
  783.         }
  784.         /// <summary>
  785.         /// 根据调查标题设置当前调查
  786.         /// </summary>
  787.         /// <param name="Name"></param>
  788.         /// <returns></returns>
  789.         [WebMethod]
  790.         public bool WriteCurrentSurvey(string Name)
  791.         {
  792.             try
  793.             {
  794.                 DAL.DBHelper db = new DAL.DBHelper();
  795.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("update Survey set IsCurrentSurvey='0'");
  796.                 db.ExecuteNonQuery(cmd);
  797.                 cmd = db.GetSqlStringCommond("update Survey set IsCurrentSurvey='1' where Name='" + Name.ToString().Replace("'", "''") + "'");
  798.                 int number=db.ExecuteNonQuery(cmd);
  799.                 cmd.Dispose();
  800.                 db.Close();
  801.                 if (number>0)
  802.                 {
  803.                     return true;
  804.                 }
  805.                 else
  806.                 {
  807.                     return false;
  808.                 }
  809.             }
  810.             catch (System.Exception ex)
  811.             {
  812.                 return false;
  813.             }
  814.         }
  815.         /// <summary>
  816.         /// 联系我们
  817.         /// </summary>
  818.         /// <param name="userName"></param>
  819.         /// <param name="userPwd"></param>
  820.         /// <param name="beizhu"></param>
  821.         /// <returns></returns>
  822.         [WebMethod]
  823.         public bool ContactUS(string name, string age, string query)
  824.         {
  825.             try
  826.             {
  827.                 int count = 0;
  828.                 DAL.DBHelper db = new DAL.DBHelper();
  829.                 System.Data.Common.DbCommand cmd = null;
  830.                 if (count > 0) return false;//如果已存用户返回false;
  831.                 cmd = db.GetSqlStringCommond(
  832.                     "INSERT INTO ContactUS VALUES ('" +
  833.                     name.ToString().Replace("'", "''") + "','" +
  834.                     age.ToString().Replace("'", "''") + "','" +
  835.                     query.ToString().Replace("'", "''") + "')");
  836.                 int i = db.ExecuteNonQuery(cmd);
  837.                 db.Close();
  838.                 cmd.Dispose();
  839.                 return true;
  840.             }
  841.             catch (Exception ex)
  842.             {
  843.                 return false;
  844.             }
  845.         }
  846.         [WebMethod]
  847.         public string ReadMaxSurvey()
  848.         {
  849.             try
  850.             {
  851.                 int count = 0;
  852.                 DAL.DBHelper db = new DAL.DBHelper();
  853.                 System.Data.Common.DbCommand cmd = db.GetSqlStringCommond("select max(id) as id from Survey");
  854.                 System.Data.DataSet ds = db.ExecuteDataSet(cmd);
  855.                 if (ds.Tables[0].Rows.Count < 1)
  856.                 {
  857.                     return "";
  858.                 }
  859.                 ds.Dispose();
  860.                 cmd.Dispose();
  861.                 db.Close();
  862.                 string id = "";
  863.                 try
  864.                 {
  865.                     id = ds.Tables[0].Rows[0]["id"].ToString();
  866.                 }
  867.                 catch
  868.                 {
  869.                     return "";
  870.                 }
  871.                 return id;
  872.             }
  873.             catch (System.Exception ex)
  874.             {
  875.                 return "false";
  876.             }
  877.         } 
  878.     }
  879. }