vote.cs

来自「asp做的新闻系统」· CS 代码 · 共 146 行

CS
146
字号
using System;
using System.Data;
using System.Data.OleDb;   
using System.Windows.Forms;
using System.Web;
using System.Text;

namespace Soholife
{
    public class Vote    //投票调查类
    {   
        //申明属性
        private int i_id;               //系统ID
        private string s_name ;         //用户名,这个字段是保留为了多用户调查的
        private int i_item1;            //投票一
        private int i_item2;            //投票二
        private int i_item3;            //投票三
        private int i_item4;            //投票四
        public int id
        {
            set{i_id=value;}
            get{return i_id;}
        }
        public int Item1
        {
            get{return i_item1;}
            set{i_item1=value;}
        }
        public int Item2
        {
            get{return i_item2;}
            set{i_item2=value;}
        }
        public int Item3
        {
            get{return i_item3;}
            set{i_item3=value;}
        }
        public int Item4
        {
            get{return i_item4;}
            set{i_item4=value;}
        }

        //构造函数
        public Vote(){;}

        //getConn():得到连接对象
        public  OleDbConnection getConn()
        {
            string connstr="Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source= C:\\server\\database\\soholife.mdb";            
            OleDbConnection tempconn= new OleDbConnection(connstr);
            return(tempconn);
        }

        // 描述:往数据表中加入投票信息
        // 输入:index代表当前是哪个被选中,name是为用户扩充程序预留的(本例程不提供配// 套程序)
        // 如果成功返回TRUE,否则返回FALSE
        public bool  AddVote(string index,string name)
        {
            bool retvalue=false;
            if(index.Equals(""))
            {
                return(false);
            }
            //设置SQL更新语句
            string str="item" + index;
            //判断传递的参数,然后调用不同的语句
            string strSql="";
            if (name.Equals(""))
            {
                strSql="update vote set " + str + "=" + str + " +1" ;
            }
            else
            {
                strSql="update vote set " + str + "=" + str + " +1 where name='"  + name + "'";
            }
            OleDbConnection myconn=null;

            try
            {
                myconn= getConn();
                myconn.Open();
                OleDbCommand updatecmd = new OleDbCommand(strSql,myconn) ;
                updatecmd.ExecuteNonQuery() ;
                retvalue=true;
            }
            catch(Exception e)
            {
                //throw(new Exception("作者提示:请确认您的数据库中至少有一条记录。" )) ;
                //加入您自己的错误处理代码
            }
            finally{
                if(myconn!=null) myconn.Close();
            }

            return (retvalue);
        }

        //得到当前投票结果
        //参数:name是为用户扩充程序预留的(本例程不提供配套程序)
        public Boolean GetVote(string name)
        {
            bool retvalue=false;
            OleDbConnection myconn = getConn();     //getConn():得到连接对象
            OleDbDataReader reader=null; 
            myconn.Open();
            string strSql="";
            if (name.Equals(""))
            {
                strSql="select * from vote";
            }
            else
            {
                strSql="select * from vote where name='" + name + "'" ;
            }

            try
            {
                OleDbCommand myCommand = new OleDbCommand(strSql,myconn) ;                       
                reader =myCommand.ExecuteReader() ;   
                if(reader.Read())
                {
                    this.i_id=(int)reader["id"];
                    this.i_item1=(int)reader["item1"];
                    this.i_item2=(int)reader["item2"];
                    this.i_item3=(int)reader["item3"];
                    this.i_item4=(int)reader["item4"];
                }
                retvalue=true;
            }
            catch(Exception e)
            {
                throw(new Exception("作者提示:请确认您的数据库中当前至少有一条记录。")) ;
                return(false);
            }
            finally{
                if (reader!=null)    reader.Close();
                if (myconn!=null)    myconn.Close();
            }

            return (retvalue);
        }
    }   
}

⌨️ 快捷键说明

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